2015-01-27 01:03:25 +01:00
|
|
|
// 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.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/root_window_manager.h"
|
2015-01-27 01:03:25 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2015-02-14 00:17:08 +01:00
|
|
|
#include "include/base/cef_logging.h"
|
2024-02-08 22:29:17 +01:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2015-01-27 01:03:25 +01:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2024-04-17 18:01:26 +02:00
|
|
|
#include "tests/cefclient/browser/default_client_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/main_context.h"
|
|
|
|
#include "tests/cefclient/browser/test_runner.h"
|
2019-03-24 19:41:42 +01:00
|
|
|
#include "tests/shared/browser/file_util.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/resource_util.h"
|
|
|
|
#include "tests/shared/common/client_switches.h"
|
2015-01-27 01:03:25 +01:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
namespace {
|
|
|
|
|
2024-05-03 20:13:38 +02:00
|
|
|
class ClientRequestContextHandler : public CefRequestContextHandler {
|
2015-09-09 16:05:39 +02:00
|
|
|
public:
|
2024-02-08 22:29:17 +01:00
|
|
|
using CreateCallback = RootWindow::Delegate::RequestContextCallback;
|
|
|
|
|
|
|
|
explicit ClientRequestContextHandler(CreateCallback callback)
|
|
|
|
: create_callback_(std::move(callback)) {}
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
// CefRequestContextHandler methods:
|
|
|
|
void OnRequestContextInitialized(
|
2021-06-17 21:43:06 +02:00
|
|
|
CefRefPtr<CefRequestContext> request_context) override {
|
2017-08-04 00:55:19 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2023-05-25 13:15:06 +02:00
|
|
|
// Allow the startup URL to create popups that bypass the popup blocker.
|
|
|
|
// For example, via Tests > New Popup from the top menu. This applies for
|
|
|
|
// for the Chrome runtime only.
|
2023-11-29 02:33:44 +01:00
|
|
|
const auto& startup_url =
|
|
|
|
MainContext::Get()->GetMainURL(/*command_line=*/nullptr);
|
2023-05-25 13:15:06 +02:00
|
|
|
request_context->SetContentSetting(startup_url, startup_url,
|
|
|
|
CEF_CONTENT_SETTING_TYPE_POPUPS,
|
|
|
|
CEF_CONTENT_SETTING_VALUE_ALLOW);
|
2024-02-08 22:29:17 +01:00
|
|
|
|
|
|
|
if (!create_callback_.is_null()) {
|
|
|
|
// Execute the callback asynchronously.
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(std::move(create_callback_), request_context));
|
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
private:
|
2024-02-08 22:29:17 +01:00
|
|
|
CreateCallback create_callback_;
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(ClientRequestContextHandler);
|
2017-08-04 00:55:19 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(ClientRequestContextHandler);
|
2015-09-09 16:05:39 +02:00
|
|
|
};
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
// Ensure a compatible set of window creation attributes.
|
|
|
|
void SanityCheckWindowConfig(const bool is_devtools,
|
2024-05-20 23:19:00 +02:00
|
|
|
const bool use_views,
|
2024-04-17 18:01:26 +02:00
|
|
|
bool& use_alloy_style,
|
|
|
|
bool& with_osr) {
|
2024-05-20 23:19:00 +02:00
|
|
|
// This configuration is not supported by cefclient architecture and
|
|
|
|
// should use default window creation instead.
|
|
|
|
CHECK(!(is_devtools && !use_views));
|
|
|
|
|
2024-04-30 03:09:17 +02:00
|
|
|
#if !defined(DISABLE_ALLOY_BOOTSTRAP)
|
|
|
|
if (MainContext::Get()->UseChromeBootstrap())
|
|
|
|
#endif
|
|
|
|
{
|
2024-04-17 18:01:26 +02:00
|
|
|
if (is_devtools && use_alloy_style) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Alloy style is not supported with Chrome runtime DevTools;"
|
|
|
|
" using Chrome style.";
|
|
|
|
use_alloy_style = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_alloy_style && with_osr) {
|
|
|
|
LOG(WARNING) << "Windowless rendering is not supported with Chrome style;"
|
|
|
|
" using windowed rendering.";
|
|
|
|
with_osr = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_views && with_osr) {
|
|
|
|
LOG(WARNING) << "Windowless rendering is not supported with Views;"
|
|
|
|
" using windowed rendering.";
|
|
|
|
with_osr = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
RootWindowManager::RootWindowManager(bool terminate_when_all_windows_closed)
|
2019-11-18 21:02:39 +01:00
|
|
|
: terminate_when_all_windows_closed_(terminate_when_all_windows_closed) {
|
2015-02-14 00:17:08 +01:00
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
|
|
|
DCHECK(command_line.get());
|
|
|
|
request_context_per_browser_ =
|
|
|
|
command_line->HasSwitch(switches::kRequestContextPerBrowser);
|
2016-01-12 01:19:51 +01:00
|
|
|
request_context_shared_cache_ =
|
|
|
|
command_line->HasSwitch(switches::kRequestContextSharedCache);
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RootWindowManager::~RootWindowManager() {
|
|
|
|
// All root windows should already have been destroyed.
|
|
|
|
DCHECK(root_windows_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<RootWindow> RootWindowManager::CreateRootWindow(
|
2021-06-19 21:54:45 +02:00
|
|
|
std::unique_ptr<RootWindowConfig> config) {
|
2015-01-27 01:03:25 +01:00
|
|
|
CefBrowserSettings settings;
|
|
|
|
MainContext::Get()->PopulateBrowserSettings(&settings);
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
SanityCheckWindowConfig(/*is_devtools=*/false, config->use_views,
|
|
|
|
config->use_alloy_style, config->with_osr);
|
|
|
|
|
2024-04-30 03:09:17 +02:00
|
|
|
scoped_refptr<RootWindow> root_window =
|
|
|
|
RootWindow::Create(config->use_views, config->use_alloy_style);
|
2021-06-19 21:54:45 +02:00
|
|
|
root_window->Init(this, std::move(config), settings);
|
2015-01-27 01:03:25 +01:00
|
|
|
|
|
|
|
// Store a reference to the root window on the main thread.
|
|
|
|
OnRootWindowCreated(root_window);
|
|
|
|
|
|
|
|
return root_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<RootWindow> RootWindowManager::CreateRootWindowAsPopup(
|
2024-04-17 18:01:26 +02:00
|
|
|
bool use_views,
|
|
|
|
bool use_alloy_style,
|
2015-01-27 01:03:25 +01:00
|
|
|
bool with_controls,
|
|
|
|
bool with_osr,
|
2024-04-17 18:01:26 +02:00
|
|
|
bool is_devtools,
|
2015-01-27 01:03:25 +01:00
|
|
|
const CefPopupFeatures& popupFeatures,
|
|
|
|
CefWindowInfo& windowInfo,
|
|
|
|
CefRefPtr<CefClient>& client,
|
|
|
|
CefBrowserSettings& settings) {
|
2017-05-31 17:33:30 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
2024-05-20 23:19:00 +02:00
|
|
|
if (MainContext::Get()->UseDefaultPopup() || (is_devtools && !use_views)) {
|
2022-04-11 22:54:33 +02:00
|
|
|
// Use default window creation for the popup. A new |client| instance is
|
|
|
|
// still required by cefclient architecture.
|
2024-04-17 18:01:26 +02:00
|
|
|
client = new DefaultClientHandler();
|
2022-04-11 22:54:33 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-05-20 23:19:00 +02:00
|
|
|
SanityCheckWindowConfig(is_devtools, use_views, use_alloy_style, with_osr);
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
if (!temp_window_) {
|
|
|
|
// TempWindow must be created on the UI thread.
|
|
|
|
temp_window_.reset(new TempWindow());
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
MainContext::Get()->PopulateBrowserSettings(&settings);
|
2023-11-29 02:33:44 +01:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
scoped_refptr<RootWindow> root_window =
|
2024-04-17 18:01:26 +02:00
|
|
|
RootWindow::Create(use_views, use_alloy_style);
|
2017-05-17 11:29:28 +02:00
|
|
|
root_window->InitAsPopup(this, with_controls, with_osr, popupFeatures,
|
|
|
|
windowInfo, client, settings);
|
2015-01-27 01:03:25 +01:00
|
|
|
|
|
|
|
// Store a reference to the root window on the main thread.
|
|
|
|
OnRootWindowCreated(root_window);
|
|
|
|
|
|
|
|
return root_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<RootWindow> RootWindowManager::GetWindowForBrowser(
|
2017-08-04 00:55:19 +02:00
|
|
|
int browser_id) const {
|
2015-01-27 01:03:25 +01:00
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
for (auto root_window : root_windows_) {
|
|
|
|
CefRefPtr<CefBrowser> browser = root_window->GetBrowser();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser.get() && browser->GetIdentifier() == browser_id) {
|
2021-06-17 22:08:01 +02:00
|
|
|
return root_window;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
scoped_refptr<RootWindow> RootWindowManager::GetActiveRootWindow() const {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
return active_root_window_;
|
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
void RootWindowManager::CloseAllWindows(bool force) {
|
|
|
|
if (!CURRENTLY_ON_MAIN_THREAD()) {
|
|
|
|
// Execute this method on the main thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
MAIN_POST_CLOSURE(base::BindOnce(&RootWindowManager::CloseAllWindows,
|
|
|
|
base::Unretained(this), force));
|
2015-01-27 01:03:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (root_windows_.empty()) {
|
2015-01-27 01:03:25 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
|
2018-09-24 17:14:04 +02:00
|
|
|
// Use a copy of |root_windows_| because the original set may be modified
|
|
|
|
// in OnRootWindowDestroyed while iterating.
|
|
|
|
RootWindowSet root_windows = root_windows_;
|
|
|
|
|
2021-10-23 00:36:25 +02:00
|
|
|
for (auto root_window : root_windows) {
|
2021-06-17 22:08:01 +02:00
|
|
|
root_window->Close(force);
|
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RootWindowManager::OnRootWindowCreated(
|
|
|
|
scoped_refptr<RootWindow> root_window) {
|
|
|
|
if (!CURRENTLY_ON_MAIN_THREAD()) {
|
|
|
|
// Execute this method on the main thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
MAIN_POST_CLOSURE(base::BindOnce(&RootWindowManager::OnRootWindowCreated,
|
|
|
|
base::Unretained(this), root_window));
|
2015-01-27 01:03:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
root_windows_.insert(root_window);
|
2017-08-04 00:55:19 +02:00
|
|
|
|
2024-05-03 20:13:38 +02:00
|
|
|
if (root_windows_.size() == 1U) {
|
|
|
|
// The first root window should be considered the active window.
|
|
|
|
OnRootWindowActivated(root_window.get());
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
|
|
|
|
2024-02-08 22:29:17 +01:00
|
|
|
CefRefPtr<CefRequestContext> RootWindowManager::GetRequestContext() {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
return CreateRequestContext(RequestContextCallback());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootWindowManager::GetRequestContext(RequestContextCallback callback) {
|
|
|
|
DCHECK(!callback.is_null());
|
|
|
|
|
|
|
|
if (!CURRENTLY_ON_MAIN_THREAD()) {
|
|
|
|
// Execute on the main thread.
|
|
|
|
MAIN_POST_CLOSURE(base::BindOnce(
|
|
|
|
base::IgnoreResult(&RootWindowManager::CreateRequestContext),
|
|
|
|
base::Unretained(this), std::move(callback)));
|
|
|
|
} else {
|
|
|
|
CreateRequestContext(std::move(callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequestContext> RootWindowManager::CreateRequestContext(
|
|
|
|
RequestContextCallback callback) {
|
2015-02-14 00:17:08 +01:00
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
|
|
|
if (request_context_per_browser_) {
|
2024-02-08 22:29:17 +01:00
|
|
|
// Synchronous use of non-global request contexts is not safe with the
|
|
|
|
// Chrome runtime.
|
2024-05-31 19:28:33 +02:00
|
|
|
#if defined(DISABLE_ALLOY_BOOTSTRAP)
|
|
|
|
CHECK(!callback.is_null());
|
|
|
|
#else
|
|
|
|
CHECK(!callback.is_null() || !MainContext::Get()->UseChromeBootstrap());
|
2024-04-30 03:09:17 +02:00
|
|
|
#endif
|
2024-02-08 22:29:17 +01:00
|
|
|
|
2015-02-14 00:17:08 +01:00
|
|
|
// Create a new request context for each browser.
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRequestContextSettings settings;
|
|
|
|
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
|
|
|
if (command_line->HasSwitch(switches::kCachePath)) {
|
2016-01-12 01:19:51 +01:00
|
|
|
if (request_context_shared_cache_) {
|
|
|
|
// Give each browser the same cache path. The resulting context objects
|
|
|
|
// will share the same storage internally.
|
|
|
|
CefString(&settings.cache_path) =
|
|
|
|
command_line->GetSwitchValue(switches::kCachePath);
|
|
|
|
} else {
|
|
|
|
// Give each browser a unique cache path. This will create completely
|
|
|
|
// isolated context objects.
|
|
|
|
std::stringstream ss;
|
2017-05-17 11:29:28 +02:00
|
|
|
ss << command_line->GetSwitchValue(switches::kCachePath).ToString()
|
2021-06-17 22:08:01 +02:00
|
|
|
<< file_util::kPathSep << time(nullptr);
|
2016-01-12 01:19:51 +01:00
|
|
|
CefString(&settings.cache_path) = ss.str();
|
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2024-02-08 22:29:17 +01:00
|
|
|
return CefRequestContext::CreateContext(
|
|
|
|
settings, new ClientRequestContextHandler(std::move(callback)));
|
2015-02-14 00:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// All browsers will share the global request context.
|
2024-02-08 22:29:17 +01:00
|
|
|
if (!shared_request_context_) {
|
2017-05-17 11:29:28 +02:00
|
|
|
shared_request_context_ = CefRequestContext::CreateContext(
|
2024-02-08 22:29:17 +01:00
|
|
|
CefRequestContext::GetGlobalContext(),
|
|
|
|
new ClientRequestContextHandler(std::move(callback)));
|
|
|
|
} else if (!callback.is_null()) {
|
|
|
|
// Execute the callback on the UI thread.
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(std::move(callback), shared_request_context_));
|
2015-09-09 16:05:39 +02:00
|
|
|
}
|
2024-02-08 22:29:17 +01:00
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
return shared_request_context_;
|
2015-02-14 00:17:08 +01:00
|
|
|
}
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
scoped_refptr<ImageCache> RootWindowManager::GetImageCache() {
|
2019-11-18 21:02:39 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2019-11-18 21:02:39 +01:00
|
|
|
if (!image_cache_) {
|
|
|
|
image_cache_ = new ImageCache;
|
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
return image_cache_;
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
void RootWindowManager::OnTest(RootWindow* root_window, int test_id) {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
|
|
|
test_runner::RunTest(root_window->GetBrowser(), test_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootWindowManager::OnExit(RootWindow* root_window) {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
|
|
|
CloseAllWindows(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootWindowManager::OnRootWindowDestroyed(RootWindow* root_window) {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
|
|
|
RootWindowSet::iterator it = root_windows_.find(root_window);
|
|
|
|
DCHECK(it != root_windows_.end());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != root_windows_.end()) {
|
2015-01-27 01:03:25 +01:00
|
|
|
root_windows_.erase(it);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
if (root_window == active_root_window_) {
|
2020-01-15 15:28:12 +01:00
|
|
|
active_root_window_ = nullptr;
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
if (terminate_when_all_windows_closed_ && root_windows_.empty()) {
|
2018-09-20 15:00:14 +02:00
|
|
|
// All windows have closed. Clean up on the UI thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(&RootWindowManager::CleanupOnUIThread,
|
|
|
|
base::Unretained(this)));
|
2015-01-27 01:03:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
void RootWindowManager::OnRootWindowActivated(RootWindow* root_window) {
|
|
|
|
REQUIRE_MAIN_THREAD();
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (root_window == active_root_window_) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
active_root_window_ = root_window;
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
void RootWindowManager::CleanupOnUIThread() {
|
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
if (temp_window_) {
|
|
|
|
// TempWindow must be destroyed on the UI thread.
|
|
|
|
temp_window_.reset(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-11-18 21:02:39 +01:00
|
|
|
if (image_cache_) {
|
|
|
|
image_cache_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:00:14 +02:00
|
|
|
// Quit the main message loop.
|
|
|
|
MainMessageLoop::Get()->Quit();
|
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
} // namespace client
|