cef/tests/ceftests/extensions/extension_test_handler.cc

241 lines
7.6 KiB
C++
Raw Normal View History

// Copyright (c) 2017 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 "tests/ceftests/extensions/extension_test_handler.h"
Implement NetworkService request interception/handling (see issue #2622). Implementation notes: - Chromium change: CookieMonster::SetCookieableSchemes needs to be called immediately after the CookieMonster is created in NetworkContext:: ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and NetworkContextParams.cookieable_schemes member (set from ProfileNetworkContextService::CreateNetworkContextParams) to support that. - Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant that exposes additional NetworkService request information. - GetResourceResponseFilter is not yet implemented. API changes: - Resource-related callbacks have been moved from CefRequestHandler to a new CefResourceRequestHandler interface which is returned via the GetResourceRequestHandler method. If the CefRequestHandler declines to handle a resource it can optionally be handled by the CefRequestContextHandler, if any, associated with the loading context. - The OnProtocolExecution callback has been moved from CefRequestHandler to CefResourceRequestHandler and will be called if a custom scheme request is unhandled. - Cookie send/save permission callbacks have been moved from CefRequestHandler and CefResourceHandler to CefResourceRequestHandler. - New methods added to CefResourceHandler that better match NetworkService execution sequence expectations. The old methods are now deprecated. - New methods added to CefRequest and CefResponse. Known behavior changes with the NetworkService implementation: - Modifying the |new_url| parameter in OnResourceRedirect will no longer result in the method being called an additional time (likely a bug in the old implementation). - Modifying the request URL in OnResourceResponse would previously cause a redirect. This behavior is now deprecated because the NetworkService does not support this functionality when using default network loaders. Temporary support has been added in combination with CefResourceHandler usage only. - Other changes to the request object in OnResourceResponse will now cause the request to be restarted. This means that OnBeforeResourceLoad, etc, will be called an additional time with the new request information. - CefResponse::GetMimeType will now be empty for non-200 responses. - Requests using custom schemes can now be handled via CefResourceRequestHandler with the same callback behavior as builtin schemes. - Redirects of custom scheme requests will now be followed as expected. - Default handling of builtin schemes can now be disabled by setting |disable_default_handling| to true in GetResourceRequestHandler. - Unhandled requests (custom scheme or builtin scheme with default handling disabled) will fail with an CefResponse::GetError value of ERR_UNKNOWN_URL_SCHEME. - The CefSchemeHandlerFactory::Create callback will now include cookie headers. To test: - Run `cefclient --enable-network-service`. All resources should load successfully (this tests the transparent proxy capability). - All tests pass with NetworkService disabled. - The following tests pass with NetworkService enabled: - CookieTest.* - FrameTest.* (excluding .*Nav) - NavigationTest.* (excluding .Redirect*) - RequestHandlerTest.* - RequestContextTest.Basic* - RequestContextTest.Popup* - RequestTest.* - ResourceManagerTest.* - ResourceRequestHandlerTest.* (excluding .Filter*) - SchemeHandlerTest.* - StreamResourceHandlerTest.*
2019-04-24 04:50:25 +02:00
#include "include/cef_request_context_handler.h"
#include "tests/ceftests/test_suite.h"
#include "tests/ceftests/test_util.h"
ExtensionTestHandler::ExtensionTestHandler(
RequestContextType request_context_type)
: request_context_type_(request_context_type), create_main_browser_(true) {
// Verify supported flag combinations.
if (request_context_on_disk()) {
EXPECT_TRUE(request_context_is_custom());
}
if (request_context_load_with_handler()) {
EXPECT_FALSE(request_context_load_without_handler());
}
if (request_context_load_without_handler()) {
EXPECT_TRUE(request_context_with_handler());
EXPECT_FALSE(request_context_load_with_handler());
}
}
ExtensionTestHandler::~ExtensionTestHandler() {
if (!request_context_temp_dir_.IsEmpty()) {
// Temporary directory will be deleted on shutdown.
request_context_temp_dir_.Take();
}
}
void ExtensionTestHandler::RunTest() {
2023-01-02 23:59:03 +01:00
if (create_main_browser_) {
OnAddMainBrowserResources();
2023-01-02 23:59:03 +01:00
}
CefRefPtr<CefRequestContextHandler> rc_handler;
if (request_context_with_handler()) {
class Handler : public CefRequestContextHandler {
public:
explicit Handler(ExtensionTestHandler* test_handler)
: test_handler_(test_handler) {}
void OnRequestContextInitialized(
CefRefPtr<CefRequestContext> request_context) override {
if (test_handler_->create_main_browser()) {
// Load extensions after the RequestContext has been initialized by
// creation of the main browser.
test_handler_->OnLoadExtensions();
}
}
private:
ExtensionTestHandler* test_handler_;
IMPLEMENT_REFCOUNTING(Handler);
};
rc_handler = new Handler(this);
}
if (request_context_is_custom()) {
CefRequestContextSettings settings;
if (request_context_on_disk()) {
// Create a new temporary directory.
EXPECT_TRUE(request_context_temp_dir_.CreateUniqueTempDirUnderPath(
CefTestSuite::GetInstance()->root_cache_path()));
CefString(&settings.cache_path) = request_context_temp_dir_.GetPath();
}
request_context_ = CefRequestContext::CreateContext(settings, rc_handler);
} else {
request_context_ = CefRequestContext::CreateContext(
CefRequestContext::GetGlobalContext(), rc_handler);
}
if (request_context_load_with_handler()) {
class Handler : public CefRequestContextHandler {
public:
Handler() {}
private:
IMPLEMENT_REFCOUNTING(Handler);
};
loader_request_context_ =
CefRequestContext::CreateContext(request_context_, new Handler());
} else if (request_context_load_without_handler()) {
loader_request_context_ =
CefRequestContext::CreateContext(request_context_, nullptr);
} else {
loader_request_context_ = request_context_;
}
if (create_main_browser_) {
OnCreateMainBrowser();
} else {
// Creation of the extension browser will trigger initialization of the
// RequestContext, so just load the extensions now.
OnLoadExtensions();
}
// Time out the test after a reasonable period of time.
SetTestTimeout();
}
void ExtensionTestHandler::DestroyTest() {
OnDestroyTest();
ReleaseRequestContexts();
RoutingTestHandler::DestroyTest();
}
void ExtensionTestHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
RoutingTestHandler::OnAfterCreated(browser);
if (create_main_browser() && !request_context_with_handler() &&
GetBrowserId() == browser->GetIdentifier()) {
// When the RequestContext doesn't have a handler we won't get a
// notification for RequestContext initialization. Instead use main browser
// creation to indicate that the RequestContext has been initialized.
OnLoadExtensions();
}
}
void ExtensionTestHandler::OnExtensionLoadFailed(cef_errorcode_t result) {
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
EXPECT_TRUE(false); // Not reached.
}
// CefMessageRouterBrowserSide::Handler methods:
bool ExtensionTestHandler::OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64 query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback) {
2023-01-02 23:59:03 +01:00
if (OnMessage(browser, request)) {
return true;
2023-01-02 23:59:03 +01:00
}
EXPECT_FALSE(true) << "Unexpected message: " << request.ToString();
return false;
}
// static
CefRefPtr<CefDictionaryValue> ExtensionTestHandler::CreateDefaultManifest(
const std::vector<std::string>& api_permissions) {
CefRefPtr<CefDictionaryValue> manifest = CefDictionaryValue::Create();
manifest->SetString("name", "An extension");
manifest->SetString("description", "An extension description");
manifest->SetString("version", "1.0");
manifest->SetInt("manifest_version", 2);
CefRefPtr<CefListValue> permissions = CefListValue::Create();
permissions->SetSize(api_permissions.size() + 2);
size_t idx = 0;
2023-01-02 23:59:03 +01:00
for (; idx < api_permissions.size(); ++idx) {
permissions->SetString(idx, api_permissions[idx]);
2023-01-02 23:59:03 +01:00
}
// Allow access to all http/https origins.
permissions->SetString(idx++, "http://*/*");
permissions->SetString(idx++, "https://*/*");
manifest->SetList("permissions", permissions);
return manifest;
}
// static
std::string ExtensionTestHandler::GetMessageJS(const std::string& message) {
EXPECT_TRUE(!message.empty());
return "window.testQuery({request:'" + message + "'});";
}
// static
void ExtensionTestHandler::VerifyExtensionInContext(
CefRefPtr<CefExtension> extension,
CefRefPtr<CefRequestContext> context,
bool has_access,
bool is_loader) {
const CefString& extension_id = extension->GetIdentifier();
EXPECT_FALSE(extension_id.empty());
if (has_access) {
EXPECT_TRUE(context->DidLoadExtension(extension_id));
EXPECT_TRUE(context->HasExtension(extension_id));
} else {
EXPECT_FALSE(context->DidLoadExtension(extension_id));
EXPECT_FALSE(context->HasExtension(extension_id));
}
CefRefPtr<CefExtension> extension2 = context->GetExtension(extension_id);
if (has_access) {
EXPECT_TRUE(extension2);
EXPECT_TRUE(extension->IsSame(extension2));
TestDictionaryEqual(extension->GetManifest(), extension2->GetManifest());
} else {
EXPECT_FALSE(extension2);
}
std::vector<CefString> extension_ids;
EXPECT_TRUE(context->GetExtensions(extension_ids));
// Should be our test extension and possibly the builtin PDF extension if it
// has finished loading (our extension may load first if the call to
// LoadExtension initializes the request context).
bool has_extension = false;
for (size_t i = 0; i < extension_ids.size(); ++i) {
if (extension_ids[i] == extension_id) {
has_extension = true;
break;
}
}
if (has_access) {
EXPECT_TRUE(has_extension);
} else {
EXPECT_FALSE(has_extension);
}
}
void ExtensionTestHandler::LoadExtension(
const std::string& extension_path,
CefRefPtr<CefDictionaryValue> manifest) {
EXPECT_TRUE(!extension_path.empty());
loader_request_context_->LoadExtension(extension_path, manifest, this);
}
void ExtensionTestHandler::UnloadExtension(CefRefPtr<CefExtension> extension) {
EXPECT_TRUE(extension);
extension->Unload();
EXPECT_FALSE(extension->IsLoaded());
EXPECT_FALSE(extension->GetLoaderContext());
}
void ExtensionTestHandler::ReleaseRequestContexts() {
request_context_ = nullptr;
loader_request_context_ = nullptr;
}