2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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 "libcef/browser/context.h"
|
|
|
|
#include "libcef/browser/browser_context.h"
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
2012-12-30 12:17:49 +01:00
|
|
|
#include "libcef/browser/browser_info.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/browser/browser_main.h"
|
|
|
|
#include "libcef/browser/browser_message_loop.h"
|
|
|
|
#include "libcef/browser/content_browser_client.h"
|
2013-06-04 19:41:37 +02:00
|
|
|
#include "libcef/browser/scheme_handler.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
2012-10-18 00:45:49 +02:00
|
|
|
#include "libcef/browser/trace_subscriber.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/main_delegate.h"
|
2013-02-02 01:21:59 +01:00
|
|
|
#include "libcef/renderer/content_renderer_client.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "base/synchronization/waitable_event.h"
|
|
|
|
#include "content/public/app/content_main.h"
|
|
|
|
#include "content/public/app/content_main_runner.h"
|
2013-02-02 01:21:59 +01:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
#include "ui/base/ui_base_switches.h"
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
#include "libcef/browser/application_mac.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "content/public/app/startup_helper_win.h"
|
2012-08-04 02:59:58 +02:00
|
|
|
#include "sandbox/win/src/sandbox_types.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Global CefContext pointer
|
|
|
|
CefRefPtr<CefContext> _Context;
|
|
|
|
|
|
|
|
|
|
|
|
int CefExecuteProcess(const CefMainArgs& args,
|
|
|
|
CefRefPtr<CefApp> application) {
|
|
|
|
CommandLine command_line(CommandLine::NO_PROGRAM);
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
command_line.ParseFromString(::GetCommandLineW());
|
|
|
|
#else
|
|
|
|
command_line.InitFromArgv(args.argc, args.argv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If no process type is specified then it represents the browser process and
|
|
|
|
// we do nothing.
|
|
|
|
std::string process_type =
|
|
|
|
command_line.GetSwitchValueASCII(switches::kProcessType);
|
|
|
|
if (process_type.empty())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// Create the CefCrApplication instance.
|
|
|
|
CefCrApplicationCreate();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CefMainDelegate main_delegate(application);
|
|
|
|
|
|
|
|
// Execute the secondary process.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
sandbox::SandboxInterfaceInfo sandbox_info = {0};
|
|
|
|
content::InitializeSandboxInfo(&sandbox_info);
|
|
|
|
|
|
|
|
return content::ContentMain(args.instance, &sandbox_info, &main_delegate);
|
|
|
|
#else
|
|
|
|
return content::ContentMain(args.argc, const_cast<const char**>(args.argv),
|
|
|
|
&main_delegate);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefInitialize(const CefMainArgs& args,
|
|
|
|
const CefSettings& settings,
|
|
|
|
CefRefPtr<CefApp> application) {
|
|
|
|
// Return true if the global context already exists.
|
|
|
|
if (_Context.get())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (settings.size != sizeof(cef_settings_t)) {
|
|
|
|
NOTREACHED() << "invalid CefSettings structure size";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new global context object.
|
|
|
|
_Context = new CefContext();
|
|
|
|
|
|
|
|
// Initialize the global context.
|
|
|
|
return _Context->Initialize(args, settings, application);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefShutdown() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
if (!_Context->OnInitThread()) {
|
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down the global context. This will block until shutdown is complete.
|
|
|
|
_Context->Shutdown();
|
|
|
|
|
|
|
|
// Delete the global context object.
|
|
|
|
_Context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDoMessageLoopWork() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
if (!_Context->OnInitThread()) {
|
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserMessageLoop::current()->DoMessageLoopIteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefRunMessageLoop() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
if (!_Context->OnInitThread()) {
|
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserMessageLoop::current()->RunMessageLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefQuitMessageLoop() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
if (!_Context->OnInitThread()) {
|
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserMessageLoop::current()->Quit();
|
|
|
|
}
|
|
|
|
|
2013-08-22 19:11:19 +02:00
|
|
|
void CefSetOSModalLoop(bool osModalLoop) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON_UIT())
|
2013-08-23 16:34:43 +02:00
|
|
|
base::MessageLoop::current()->set_os_modal_loop(osModalLoop);
|
2013-08-22 19:11:19 +02:00
|
|
|
else
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(CefSetOSModalLoop, osModalLoop));
|
|
|
|
#endif // defined(OS_WIN)
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefContext
|
|
|
|
|
|
|
|
CefContext::CefContext()
|
|
|
|
: initialized_(false),
|
|
|
|
shutting_down_(false),
|
2012-11-20 21:08:36 +01:00
|
|
|
init_thread_id_(0) {
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefContext::~CefContext() {
|
|
|
|
if (!shutting_down_)
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::Initialize(const CefMainArgs& args,
|
|
|
|
const CefSettings& settings,
|
|
|
|
CefRefPtr<CefApp> application) {
|
|
|
|
init_thread_id_ = base::PlatformThread::CurrentId();
|
|
|
|
settings_ = settings;
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
cache_path_ = base::FilePath(CefString(&settings.cache_path));
|
2012-06-25 20:22:50 +02:00
|
|
|
if (!cache_path_.empty() &&
|
2013-07-24 22:15:18 +02:00
|
|
|
!base::DirectoryExists(cache_path_) &&
|
2012-06-25 20:22:50 +02:00
|
|
|
!file_util::CreateDirectory(cache_path_)) {
|
2012-05-18 17:04:56 +02:00
|
|
|
NOTREACHED() << "The cache_path directory could not be created";
|
2013-02-23 01:43:28 +01:00
|
|
|
cache_path_ = base::FilePath();
|
2012-05-18 17:04:56 +02:00
|
|
|
}
|
2012-09-27 23:25:29 +02:00
|
|
|
if (cache_path_.empty()) {
|
|
|
|
// Create and use a temporary directory.
|
|
|
|
if (cache_temp_dir_.CreateUniqueTempDir()) {
|
|
|
|
cache_path_ = cache_temp_dir_.path();
|
|
|
|
} else {
|
|
|
|
NOTREACHED() << "Failed to create temporary cache_path directory";
|
|
|
|
}
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
#if !defined(OS_WIN)
|
2012-04-03 03:34:16 +02:00
|
|
|
if (settings.multi_threaded_message_loop) {
|
2012-04-11 20:00:55 +02:00
|
|
|
NOTIMPLEMENTED() << "multi_threaded_message_loop is not supported.";
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-11 20:00:55 +02:00
|
|
|
#endif
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
main_delegate_.reset(new CefMainDelegate(application));
|
|
|
|
main_runner_.reset(content::ContentMainRunner::Create());
|
|
|
|
|
|
|
|
int exit_code;
|
|
|
|
|
|
|
|
// Initialize the content runner.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
sandbox::SandboxInterfaceInfo sandbox_info = {0};
|
|
|
|
content::InitializeSandboxInfo(&sandbox_info);
|
|
|
|
|
|
|
|
exit_code = main_runner_->Initialize(args.instance, &sandbox_info,
|
|
|
|
main_delegate_.get());
|
|
|
|
#else
|
|
|
|
exit_code = main_runner_->Initialize(args.argc,
|
|
|
|
const_cast<const char**>(args.argv),
|
|
|
|
main_delegate_.get());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DCHECK_LT(exit_code, 0);
|
|
|
|
if (exit_code >= 0)
|
|
|
|
return false;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Run the process. Results in a call to CefMainDelegate::RunProcess() which
|
|
|
|
// will create the browser runner and message loop without blocking.
|
2012-04-03 03:34:16 +02:00
|
|
|
exit_code = main_runner_->Run();
|
|
|
|
|
|
|
|
initialized_ = true;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Continue initialization on the UI thread.
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(&CefContext::OnContextInitialized, this));
|
2012-05-18 17:04:56 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::Shutdown() {
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(OnInitThread());
|
|
|
|
|
|
|
|
shutting_down_ = true;
|
|
|
|
|
|
|
|
if (settings_.multi_threaded_message_loop) {
|
|
|
|
// Events that will be used to signal when shutdown is complete. Start in
|
|
|
|
// non-signaled mode so that the event will block.
|
|
|
|
base::WaitableEvent uithread_shutdown_event(false, false);
|
|
|
|
|
|
|
|
// Finish shutdown on the UI thread.
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefContext::FinishShutdownOnUIThread, this,
|
2012-04-11 20:00:55 +02:00
|
|
|
&uithread_shutdown_event));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
/// Block until UI thread shutdown is complete.
|
|
|
|
uithread_shutdown_event.Wait();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
FinalizeShutdown();
|
|
|
|
} else {
|
|
|
|
// Finish shutdown on the current thread, which should be the UI thread.
|
2012-04-11 20:00:55 +02:00
|
|
|
FinishShutdownOnUIThread(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
FinalizeShutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::OnInitThread() {
|
|
|
|
return (base::PlatformThread::CurrentId() == init_thread_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostImpl> CefContext::GetBrowserByRoutingID(
|
|
|
|
int render_process_id, int render_view_id) {
|
2012-12-30 12:17:49 +01:00
|
|
|
scoped_refptr<CefBrowserInfo> info =
|
|
|
|
CefContentBrowserClient::Get()->GetBrowserInfo(render_process_id,
|
|
|
|
render_view_id);
|
|
|
|
if (info.get()) {
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser = info->browser();
|
|
|
|
if (!browser.get()) {
|
2013-02-09 23:38:24 +01:00
|
|
|
LOG(WARNING) << "Found browser id " << info->browser_id() <<
|
|
|
|
" but no browser object matching process id " <<
|
|
|
|
render_process_id << " and view id " << render_view_id;
|
2012-12-30 12:17:49 +01:00
|
|
|
}
|
|
|
|
return browser;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefApp> CefContext::application() const {
|
|
|
|
return main_delegate_->content_client()->application();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserContext* CefContext::browser_context() const {
|
|
|
|
return main_delegate_->browser_client()->browser_main_parts()->
|
|
|
|
browser_context();
|
|
|
|
}
|
|
|
|
|
2012-04-27 00:20:18 +02:00
|
|
|
CefDevToolsDelegate* CefContext::devtools_delegate() const {
|
|
|
|
return main_delegate_->browser_client()->browser_main_parts()->
|
|
|
|
devtools_delegate();
|
|
|
|
}
|
|
|
|
|
2013-02-06 21:41:54 +01:00
|
|
|
scoped_ptr<net::ProxyConfigService> CefContext::proxy_config_service() const {
|
|
|
|
return main_delegate_->browser_client()->browser_main_parts()->
|
|
|
|
proxy_config_service();
|
|
|
|
}
|
|
|
|
|
2013-03-07 02:20:24 +01:00
|
|
|
PrefService* CefContext::pref_service() const {
|
|
|
|
return main_delegate_->browser_client()->browser_main_parts()->pref_service();
|
|
|
|
}
|
|
|
|
|
2012-10-18 00:45:49 +02:00
|
|
|
CefTraceSubscriber* CefContext::GetTraceSubscriber() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
if (shutting_down_)
|
|
|
|
return NULL;
|
|
|
|
if (!trace_subscriber_.get())
|
|
|
|
trace_subscriber_.reset(new CefTraceSubscriber());
|
|
|
|
return trace_subscriber_.get();
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
void CefContext::OnContextInitialized() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2012-10-08 19:47:37 +02:00
|
|
|
// Register internal scheme handlers.
|
|
|
|
scheme::RegisterInternalHandlers();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Notify the handler.
|
|
|
|
CefRefPtr<CefApp> app = application();
|
|
|
|
if (app.get()) {
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler =
|
|
|
|
app->GetBrowserProcessHandler();
|
|
|
|
if (handler.get())
|
|
|
|
handler->OnContextInitialized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefContext::FinishShutdownOnUIThread(
|
|
|
|
base::WaitableEvent* uithread_shutdown_event) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2012-12-30 12:17:49 +01:00
|
|
|
CefContentBrowserClient::Get()->DestroyAllBrowsers();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-10-18 00:45:49 +02:00
|
|
|
if (trace_subscriber_.get())
|
|
|
|
trace_subscriber_.reset(NULL);
|
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
if (uithread_shutdown_event)
|
|
|
|
uithread_shutdown_event->Signal();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::FinalizeShutdown() {
|
2013-02-02 01:21:59 +01:00
|
|
|
if (content::RenderProcessHost::run_renderer_in_process()) {
|
|
|
|
// Blocks until RenderProcess cleanup is complete.
|
|
|
|
CefContentRendererClient::Get()->RunSingleProcessCleanup();
|
|
|
|
}
|
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
// Shut down the browser runner or UI thread.
|
2012-04-03 03:34:16 +02:00
|
|
|
main_delegate_->ShutdownBrowser();
|
|
|
|
|
|
|
|
// Shut down the content runner.
|
|
|
|
main_runner_->Shutdown();
|
|
|
|
|
|
|
|
main_runner_.reset(NULL);
|
|
|
|
main_delegate_.reset(NULL);
|
|
|
|
}
|