cef/tests/cefclient/browser/urlrequest_test.cc

191 lines
5.5 KiB
C++
Raw Normal View History

// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "cefclient/browser/urlrequest_test.h"
#include <string>
#include "include/base/cef_bind.h"
#include "include/base/cef_callback.h"
#include "include/base/cef_logging.h"
#include "include/cef_urlrequest.h"
#include "include/wrapper/cef_helpers.h"
namespace client {
namespace urlrequest_test {
namespace {
const char kTestUrl[] = "http://tests/urlrequest";
const char kTestMessageName[] = "URLRequestTest";
// Implementation of CefURLRequestClient that stores response information. Only
// accessed on the UI thread.
class RequestClient : public CefURLRequestClient {
public:
// Callback to be executed on request completion.
typedef base::Callback<void(CefURLRequest::ErrorCode /*error_code*/,
const std::string& /*download_data*/)> Callback;
explicit RequestClient(const Callback& callback)
: callback_(callback) {
CEF_REQUIRE_UI_THREAD();
DCHECK(!callback_.is_null());
}
void Detach() {
CEF_REQUIRE_UI_THREAD();
if (!callback_.is_null())
callback_.Reset();
}
void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE {
CEF_REQUIRE_UI_THREAD();
if (!callback_.is_null()) {
callback_.Run(request->GetRequestError(), download_data_);
callback_.Reset();
}
}
void OnUploadProgress(CefRefPtr<CefURLRequest> request,
int64 current,
int64 total) OVERRIDE {
}
void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
int64 current,
int64 total) OVERRIDE {
}
void OnDownloadData(CefRefPtr<CefURLRequest> request,
const void* data,
size_t data_length) OVERRIDE {
CEF_REQUIRE_UI_THREAD();
download_data_ += std::string(static_cast<const char*>(data), data_length);
}
bool GetAuthCredentials(bool isProxy,
const CefString& host,
int port,
const CefString& realm,
const CefString& scheme,
CefRefPtr<CefAuthCallback> callback) OVERRIDE {
return false;
}
private:
Callback callback_;
std::string download_data_;
IMPLEMENT_REFCOUNTING(RequestClient);
DISALLOW_COPY_AND_ASSIGN(RequestClient);
};
// Handle messages in the browser process. Only accessed on the UI thread.
class Handler : public CefMessageRouterBrowserSide::Handler {
public:
Handler() {
CEF_REQUIRE_UI_THREAD();
}
~Handler() {
CancelPendingRequest();
}
// Called due to cefQuery execution in urlrequest.html.
bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64 query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback) OVERRIDE {
CEF_REQUIRE_UI_THREAD();
// Only handle messages from the test URL.
const std::string& url = frame->GetURL();
if (url.find(kTestUrl) != 0)
return false;
const std::string& message_name = request;
if (message_name.find(kTestMessageName) == 0) {
const std::string& url = message_name.substr(sizeof(kTestMessageName));
CancelPendingRequest();
DCHECK(!callback_.get());
DCHECK(!urlrequest_.get());
callback_ = callback;
// Create a CefRequest for the specified URL.
CefRefPtr<CefRequest> request = CefRequest::Create();
request->SetURL(url);
request->SetMethod("GET");
// Callback to be executed on request completion.
// It's safe to use base::Unretained() here because there is only one
// RequestClient pending at any given time and we explicitly detach the
// callback in the Handler destructor.
const RequestClient::Callback& callback =
base::Bind(&Handler::OnRequestComplete, base::Unretained(this));
// Create and start the new CefURLRequest.
Add support for complete isolation of storage and permissions (cache, cookies, localStorage, access grants, etc) on a per-request-context basis (issue #1044). - CefRequestContext instances can be configured using a new CefRequestContextSettings structure passed to CefRequestContext::CreateContext. - Scheme registration is now per-request-context using new CefRequestContext::RegisterSchemeHandlerFactory and ClearSchemeHandlerFactories methods. - Cookie managers are now per-request-context by default and can be retrieved using a new CefRequestContext::GetDefaultCookieManager method. - CefURLRequest::Create now accepts an optional CefRequestContext argument for associating a URL request with a context (browser process only). - The CefRequestContextHandler associated with a CefRequestContext will not be released until all objects related to that context have been destroyed. - When the cache path is empty an in-memory cache ("incognito mode") will be used for storage and no data will be persisted to disk. - Add CefSettings.user_data_path which specifies the location where user data such as spell checking dictionary files will be stored on disk. - Add asynchronous callbacks for all CefCookieManager methods. - Add PK_LOCAL_APP_DATA and PK_USER_DATA path keys for retrieving user directories via CefGetPath. - cefclient: Add "New Window" test that creates a new window unrelated to existing windows. When used in combination with `--request-context-per-browser` the new window will be given a new and isolated request context. git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@2040 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
2015-03-02 21:25:14 +01:00
urlrequest_ = CefURLRequest::Create(request,
new RequestClient(callback),
NULL);
return true;
}
return false;
}
private:
// Cancel the currently pending URL request, if any.
void CancelPendingRequest() {
CEF_REQUIRE_UI_THREAD();
if (urlrequest_.get()) {
// Don't execute the callback when we explicitly cancel the request.
static_cast<RequestClient*>(urlrequest_->GetClient().get())->Detach();
urlrequest_->Cancel();
urlrequest_ = NULL;
}
if (callback_.get()) {
// Must always execute |callback_| before deleting it.
callback_->Failure(ERR_ABORTED, test_runner::GetErrorString(ERR_ABORTED));
callback_ = NULL;
}
}
void OnRequestComplete(CefURLRequest::ErrorCode error_code,
const std::string& download_data) {
CEF_REQUIRE_UI_THREAD();
if (error_code == ERR_NONE)
callback_->Success(download_data);
else
callback_->Failure(error_code, test_runner::GetErrorString(error_code));
callback_ = NULL;
urlrequest_ = NULL;
}
CefRefPtr<Callback> callback_;
CefRefPtr<CefURLRequest> urlrequest_;
DISALLOW_COPY_AND_ASSIGN(Handler);
};
} // namespace
void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) {
handlers.insert(new Handler());
}
} // namespace urlrequest_test
} // namespace client