2011-12-23 18:36:30 +01:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
2010-10-03 23:04:50 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/cef_context.h"
|
|
|
|
#include "libcef/browser_devtools_scheme_handler.h"
|
|
|
|
#include "libcef/browser_impl.h"
|
|
|
|
#include "libcef/browser_webkit_glue.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-09-23 02:16:03 +02:00
|
|
|
#include "base/bind.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "base/file_util.h"
|
2012-02-16 18:11:49 +01:00
|
|
|
#include "base/path_service.h"
|
2011-05-16 18:56:12 +02:00
|
|
|
#include "base/synchronization/waitable_event.h"
|
2012-02-16 18:11:49 +01:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
#include "ui/base/ui_base_paths.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN)
|
|
|
|
#include "crypto/nss_util.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-16 18:11:49 +01:00
|
|
|
#if defined(OS_MACOSX)
|
2012-05-23 21:01:04 +02:00
|
|
|
#include "base/mac/foundation_util.h"
|
2012-02-16 18:11:49 +01:00
|
|
|
#include "base/mac/mac_util.h"
|
|
|
|
#include "grit/webkit_resources.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "base/win/resource_util.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Global CefContext pointer
|
|
|
|
CefRefPtr<CefContext> _Context;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-03-23 00:11:26 +01:00
|
|
|
// Both the CefContext constuctor and the CefContext::RemoveBrowser method need
|
|
|
|
// to initialize or reset to the same value.
|
|
|
|
const int kNextBrowserIdReset = 1;
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
#if defined(OS_MACOSX)
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
FilePath GetDefaultPackPath() {
|
2012-05-23 22:33:32 +02:00
|
|
|
// Start out with the path to the running executable.
|
|
|
|
FilePath execPath;
|
|
|
|
PathService::Get(base::FILE_EXE, &execPath);
|
|
|
|
|
|
|
|
// Get the main bundle path.
|
2012-05-23 21:01:04 +02:00
|
|
|
FilePath bundlePath = base::mac::GetAppBundlePath(execPath);
|
2012-05-23 22:33:32 +02:00
|
|
|
|
|
|
|
// Go into the Contents/Resources directory.
|
2012-05-23 21:01:04 +02:00
|
|
|
return bundlePath.Append(FILE_PATH_LITERAL("Contents"))
|
|
|
|
.Append(FILE_PATH_LITERAL("Resources"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // !defined(OS_MACOSX)
|
|
|
|
|
|
|
|
FilePath GetDefaultPackPath() {
|
|
|
|
FilePath pak_dir;
|
|
|
|
PathService::Get(base::DIR_MODULE, &pak_dir);
|
|
|
|
return pak_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !defined(OS_MACOSX)
|
2011-04-09 03:05:30 +02:00
|
|
|
|
2011-06-14 19:10:15 +02:00
|
|
|
// Used in multi-threaded message loop mode to observe shutdown of the UI
|
|
|
|
// thread.
|
2012-01-10 00:46:23 +01:00
|
|
|
class DestructionObserver : public MessageLoop::DestructionObserver {
|
|
|
|
public:
|
|
|
|
explicit DestructionObserver(base::WaitableEvent *event) : event_(event) {}
|
2011-06-14 19:10:15 +02:00
|
|
|
virtual void WillDestroyCurrentMessageLoop() {
|
|
|
|
MessageLoop::current()->RemoveDestructionObserver(this);
|
|
|
|
event_->Signal();
|
|
|
|
delete this;
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
2011-06-14 19:10:15 +02:00
|
|
|
base::WaitableEvent *event_;
|
|
|
|
};
|
|
|
|
|
2012-02-16 18:11:49 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
|
|
|
// Helper method for retrieving a resource from a module.
|
|
|
|
base::StringPiece GetRawDataResource(HMODULE module, int resource_id) {
|
|
|
|
void* data_ptr;
|
|
|
|
size_t data_size;
|
|
|
|
return base::win::GetDataResourceFromModule(module, resource_id, &data_ptr,
|
|
|
|
&data_size)
|
|
|
|
? base::StringPiece(static_cast<char*>(data_ptr), data_size)
|
|
|
|
: base::StringPiece();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(OS_MACOSX)
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-04-09 03:05:30 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
class CefResourceBundleDelegate : public ui::ResourceBundle::Delegate {
|
|
|
|
public:
|
|
|
|
CefResourceBundleDelegate(CefContext* context)
|
|
|
|
: context_(context),
|
|
|
|
allow_pack_file_load_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_allow_pack_file_load(bool val) { allow_pack_file_load_ = val; }
|
|
|
|
|
|
|
|
private:
|
2012-06-21 22:35:13 +02:00
|
|
|
virtual FilePath GetPathForResourcePack(
|
|
|
|
const FilePath& pack_path,
|
|
|
|
ui::ScaleFactor scale_factor) OVERRIDE {
|
2012-05-23 21:01:04 +02:00
|
|
|
// Only allow the cef pack file to load.
|
|
|
|
if (!context_->settings().pack_loading_disabled && allow_pack_file_load_)
|
|
|
|
return pack_path;
|
|
|
|
return FilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual FilePath GetPathForLocalePack(const FilePath& pack_path,
|
|
|
|
const std::string& locale) OVERRIDE {
|
|
|
|
if (!context_->settings().pack_loading_disabled)
|
|
|
|
return pack_path;
|
|
|
|
return FilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual gfx::Image GetImageNamed(int resource_id) OVERRIDE {
|
|
|
|
return gfx::Image();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual gfx::Image GetNativeImageNamed(
|
|
|
|
int resource_id,
|
|
|
|
ui::ResourceBundle::ImageRTL rtl) OVERRIDE {
|
|
|
|
return gfx::Image();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual base::RefCountedStaticMemory* LoadDataResourceBytes(
|
2012-06-21 22:35:13 +02:00
|
|
|
int resource_id,
|
|
|
|
ui::ScaleFactor scale_factor) OVERRIDE {
|
2012-05-23 21:01:04 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool GetRawDataResource(int resource_id,
|
2012-06-21 22:35:13 +02:00
|
|
|
ui::ScaleFactor scale_factor,
|
2012-05-23 21:01:04 +02:00
|
|
|
base::StringPiece* value) OVERRIDE {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool GetLocalizedString(int message_id, string16* value) OVERRIDE {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual scoped_ptr<gfx::Font> GetFont(
|
|
|
|
ui::ResourceBundle::FontStyle style) OVERRIDE {
|
|
|
|
return scoped_ptr<gfx::Font>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefContext pointer is guaranteed to outlive this object.
|
|
|
|
CefContext* context_;
|
|
|
|
bool allow_pack_file_load_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefResourceBundleDelegate);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefInitialize(const CefSettings& settings, CefRefPtr<CefApp> application) {
|
2011-01-07 02:06:10 +01:00
|
|
|
// Return true if the global context already exists.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (_Context.get())
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (settings.size != sizeof(cef_settings_t)) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "invalid CefSettings structure size";
|
2010-11-18 22:05:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
// Create the new global context object.
|
2010-10-03 23:04:50 +02:00
|
|
|
_Context = new CefContext();
|
2010-11-18 15:47:38 +01:00
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
// Initialize the global context.
|
2011-11-23 23:47:09 +01:00
|
|
|
return _Context->Initialize(settings, application);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefShutdown() {
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "context not valid";
|
2010-10-03 23:04:50 +02:00
|
|
|
return;
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
// Must always be called on the same thread as Initialize.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!_Context->process()->CalledOnValidThread()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "called on invalid thread";
|
2011-01-07 02:06:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down the global context. This will block until shutdown is complete.
|
2010-10-03 23:04:50 +02:00
|
|
|
_Context->Shutdown();
|
2011-01-07 02:06:10 +01:00
|
|
|
|
|
|
|
// Delete the global context object.
|
2010-10-03 23:04:50 +02:00
|
|
|
_Context = NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefDoMessageLoopWork() {
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "context not valid";
|
2010-10-03 23:04:50 +02:00
|
|
|
return;
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
// Must always be called on the same thread as Initialize.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!_Context->process()->CalledOnValidThread()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "called on invalid thread";
|
2010-10-03 23:04:50 +02:00
|
|
|
return;
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
_Context->process()->DoMessageLoopIteration();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefRunMessageLoop() {
|
2011-03-24 21:36:47 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "context not valid";
|
2011-03-24 21:36:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!_Context->process()->CalledOnValidThread()) {
|
2011-10-06 15:34:47 +02:00
|
|
|
NOTREACHED() << "called on invalid thread";
|
2011-03-24 21:36:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Context->process()->RunMessageLoop();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefQuitMessageLoop() {
|
2011-12-08 14:25:26 +01:00
|
|
|
// 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.
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!_Context->process()->CalledOnValidThread()) {
|
2011-12-08 14:25:26 +01:00
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Context->process()->QuitMessageLoop();
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// CefContext
|
|
|
|
|
2011-02-28 01:33:11 +01:00
|
|
|
CefContext::CefContext()
|
2011-03-23 00:11:26 +01:00
|
|
|
: initialized_(false),
|
|
|
|
shutting_down_(false),
|
2012-05-23 21:01:04 +02:00
|
|
|
request_context_(NULL),
|
2011-03-23 00:11:26 +01:00
|
|
|
next_browser_id_(kNextBrowserIdReset),
|
2012-01-10 00:46:23 +01:00
|
|
|
current_webviewhost_(NULL) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefContext::~CefContext() {
|
|
|
|
if (!shutting_down_)
|
2011-01-07 02:06:10 +01:00
|
|
|
Shutdown();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-11-23 23:47:09 +01:00
|
|
|
bool CefContext::Initialize(const CefSettings& settings,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefApp> application) {
|
2010-11-18 22:05:25 +01:00
|
|
|
settings_ = settings;
|
2011-11-23 23:47:09 +01:00
|
|
|
application_ = application;
|
2010-11-18 22:05:25 +01:00
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
cache_path_ = FilePath(CefString(&settings.cache_path));
|
2012-06-20 21:44:00 +02:00
|
|
|
if (!cache_path_.empty() &&
|
|
|
|
!file_util::PathExists(cache_path_) &&
|
|
|
|
!file_util::CreateDirectory(cache_path_)) {
|
|
|
|
NOTREACHED() << "Failed to create cache_path directory";
|
|
|
|
cache_path_.clear();
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN)
|
|
|
|
// We want to be sure to init NSPR on the main thread.
|
2011-05-16 18:56:12 +02:00
|
|
|
crypto::EnsureNSPRInit();
|
2010-10-03 23:04:50 +02:00
|
|
|
#endif
|
|
|
|
|
2011-11-30 18:00:24 +01:00
|
|
|
process_.reset(new CefProcess(settings_.multi_threaded_message_loop));
|
2010-10-03 23:04:50 +02:00
|
|
|
process_->CreateChildThreads();
|
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
initialized_ = true;
|
|
|
|
|
2011-10-04 13:49:36 +02:00
|
|
|
// Perform DevTools scheme registration when CEF initialization is complete.
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
2011-10-31 18:56:35 +01:00
|
|
|
base::Bind(&RegisterDevToolsSchemeHandler, true));
|
2011-10-04 13:49:36 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefContext::Shutdown() {
|
2011-01-07 02:06:10 +01:00
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(process_->CalledOnValidThread());
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
shutting_down_ = true;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (settings_.multi_threaded_message_loop) {
|
2011-06-14 19:10:15 +02:00
|
|
|
// Events that will be used to signal when shutdown is complete. Start in
|
2011-01-07 02:06:10 +01:00
|
|
|
// non-signaled mode so that the event will block.
|
2011-06-14 19:10:15 +02:00
|
|
|
base::WaitableEvent browser_shutdown_event(false, false);
|
|
|
|
base::WaitableEvent uithread_shutdown_event(false, false);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
// Finish shutdown on the UI thread.
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
2012-02-17 15:51:20 +01:00
|
|
|
base::Bind(&CefContext::UIT_FinishShutdown, this,
|
|
|
|
&browser_shutdown_event, &uithread_shutdown_event));
|
2011-01-07 02:06:10 +01:00
|
|
|
|
2011-06-14 19:10:15 +02:00
|
|
|
// Block until browser shutdown is complete.
|
|
|
|
browser_shutdown_event.Wait();
|
|
|
|
|
|
|
|
// Delete the process to destroy the child threads.
|
2011-11-30 18:00:24 +01:00
|
|
|
process_.reset(NULL);
|
2011-06-14 19:10:15 +02:00
|
|
|
|
|
|
|
// Block until UI thread shutdown is complete.
|
|
|
|
uithread_shutdown_event.Wait();
|
2011-01-07 02:06:10 +01:00
|
|
|
} else {
|
|
|
|
// Finish shutdown on the current thread, which should be the UI thread.
|
2011-06-14 19:10:15 +02:00
|
|
|
UIT_FinishShutdown(NULL, NULL);
|
2011-01-07 02:06:10 +01:00
|
|
|
|
2011-06-14 19:10:15 +02:00
|
|
|
// Delete the process to destroy the child threads.
|
2011-11-30 18:00:24 +01:00
|
|
|
process_.reset(NULL);
|
2011-06-14 19:10:15 +02:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefContext::AddBrowser(CefRefPtr<CefBrowserImpl> browser) {
|
2010-10-03 23:04:50 +02:00
|
|
|
bool found = false;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// check that the browser isn't already in the list before adding
|
|
|
|
BrowserList::const_iterator it = browserlist_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != browserlist_.end(); ++it) {
|
|
|
|
if (it->get() == browser.get()) {
|
2010-10-03 23:04:50 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!found) {
|
2010-10-03 23:04:50 +02:00
|
|
|
browser->UIT_SetUniqueID(next_browser_id_++);
|
|
|
|
browserlist_.push_back(browser);
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return !found;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefContext::RemoveBrowser(CefRefPtr<CefBrowserImpl> browser) {
|
2010-10-03 23:04:50 +02:00
|
|
|
bool deleted = false;
|
|
|
|
bool empty = false;
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
|
|
|
|
BrowserList::iterator it = browserlist_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != browserlist_.end(); ++it) {
|
|
|
|
if (it->get() == browser.get()) {
|
2011-05-20 16:42:25 +02:00
|
|
|
browserlist_.erase(it);
|
|
|
|
deleted = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
if (browserlist_.empty()) {
|
|
|
|
next_browser_id_ = kNextBrowserIdReset;
|
|
|
|
empty = true;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty) {
|
2011-08-03 18:24:38 +02:00
|
|
|
if (CefThread::CurrentlyOn(CefThread::UI)) {
|
|
|
|
webkit_glue::ClearCache();
|
|
|
|
} else {
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
2012-02-17 15:51:20 +01:00
|
|
|
base::Bind(webkit_glue::ClearCache));
|
2011-08-03 18:24:38 +02:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return deleted;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefBrowserImpl> CefContext::GetBrowserByID(int id) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
BrowserList::const_iterator it = browserlist_.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != browserlist_.end(); ++it) {
|
|
|
|
if (it->get()->UIT_GetUniqueID() == id)
|
2011-05-20 16:42:25 +02:00
|
|
|
return it->get();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
return NULL;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-01-07 02:06:10 +01:00
|
|
|
|
2012-02-16 18:11:49 +01:00
|
|
|
void CefContext::InitializeResourceBundle() {
|
|
|
|
FilePath pak_file, locales_dir;
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
if (!settings_.pack_loading_disabled) {
|
|
|
|
if (settings_.pack_file_path.length > 0)
|
|
|
|
pak_file = FilePath(CefString(&settings_.pack_file_path));
|
2012-02-16 18:11:49 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
if (pak_file.empty())
|
|
|
|
pak_file = GetDefaultPackPath().Append(FILE_PATH_LITERAL("chrome.pak"));
|
2012-02-16 18:11:49 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
if (settings_.locales_dir_path.length > 0)
|
|
|
|
locales_dir = FilePath(CefString(&settings_.locales_dir_path));
|
2012-02-16 18:11:49 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
if (!locales_dir.empty())
|
|
|
|
PathService::Override(ui::DIR_LOCALES, locales_dir);
|
|
|
|
}
|
2012-02-16 18:11:49 +01:00
|
|
|
|
|
|
|
std::string locale_str = locale();
|
2012-05-23 21:01:04 +02:00
|
|
|
if (locale_str.empty())
|
|
|
|
locale_str = "en-US";
|
2012-02-16 18:11:49 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
resource_bundle_delegate_.reset(new CefResourceBundleDelegate(this));
|
|
|
|
const std::string loaded_locale =
|
|
|
|
ui::ResourceBundle::InitSharedInstanceWithLocale(
|
|
|
|
locale_str, resource_bundle_delegate_.get());
|
|
|
|
if (!settings_.pack_loading_disabled) {
|
|
|
|
CHECK(!loaded_locale.empty()) << "Locale could not be found for "
|
|
|
|
<< locale_str;
|
|
|
|
|
|
|
|
if (file_util::PathExists(pak_file)) {
|
|
|
|
resource_bundle_delegate_->set_allow_pack_file_load(true);
|
|
|
|
ResourceBundle::GetSharedInstance().AddDataPack(
|
2012-06-21 22:35:13 +02:00
|
|
|
pak_file, ui::SCALE_FACTOR_NONE);
|
2012-05-23 21:01:04 +02:00
|
|
|
resource_bundle_delegate_->set_allow_pack_file_load(false);
|
|
|
|
} else {
|
|
|
|
NOTREACHED() << "Could not load chrome.pak";
|
|
|
|
}
|
|
|
|
}
|
2012-02-16 18:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::CleanupResourceBundle() {
|
2012-05-23 21:01:04 +02:00
|
|
|
ResourceBundle::CleanupSharedInstance();
|
|
|
|
resource_bundle_delegate_.reset(NULL);
|
2012-02-16 18:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string16 CefContext::GetLocalizedString(int message_id) const {
|
|
|
|
string16 value;
|
|
|
|
|
|
|
|
if (application_.get()) {
|
|
|
|
CefRefPtr<CefResourceBundleHandler> handler =
|
|
|
|
application_->GetResourceBundleHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefString cef_str;
|
|
|
|
if (handler->GetLocalizedString(message_id, cef_str))
|
|
|
|
value = cef_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.empty() && !settings_.pack_loading_disabled)
|
|
|
|
value = ResourceBundle::GetSharedInstance().GetLocalizedString(message_id);
|
|
|
|
|
|
|
|
if (value.empty())
|
|
|
|
LOG(ERROR) << "No localized string available for id " << message_id;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::StringPiece CefContext::GetDataResource(int resource_id) const {
|
|
|
|
base::StringPiece value;
|
|
|
|
|
|
|
|
if (application_.get()) {
|
|
|
|
CefRefPtr<CefResourceBundleHandler> handler =
|
|
|
|
application_->GetResourceBundleHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
void* data = NULL;
|
|
|
|
size_t data_size = 0;
|
|
|
|
if (handler->GetDataResource(resource_id, data, data_size))
|
|
|
|
value = base::StringPiece(static_cast<char*>(data), data_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
if (value.empty()) {
|
|
|
|
FilePath file_path;
|
|
|
|
HMODULE hModule = NULL;
|
|
|
|
|
|
|
|
// Try to load the resource from the DLL.
|
|
|
|
if (PathService::Get(base::FILE_MODULE, &file_path))
|
|
|
|
hModule = ::GetModuleHandle(file_path.value().c_str());
|
|
|
|
if (!hModule)
|
|
|
|
hModule = ::GetModuleHandle(NULL);
|
2012-05-23 21:01:04 +02:00
|
|
|
value = ::GetRawDataResource(hModule, resource_id);
|
2012-02-16 18:11:49 +01:00
|
|
|
}
|
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
if (value.empty()) {
|
|
|
|
switch (resource_id) {
|
|
|
|
case IDR_BROKENIMAGE: {
|
|
|
|
// Use webkit's broken image icon (16x16)
|
|
|
|
static std::string broken_image_data;
|
|
|
|
if (broken_image_data.empty()) {
|
|
|
|
FilePath path = GetResourcesFilePath();
|
|
|
|
// In order to match WebKit's colors for the missing image, we have to
|
|
|
|
// use a PNG. The GIF doesn't have the color range needed to correctly
|
|
|
|
// match the TIFF they use in Safari.
|
|
|
|
path = path.AppendASCII("missingImage.png");
|
|
|
|
bool success = file_util::ReadFileToString(path, &broken_image_data);
|
|
|
|
if (!success) {
|
|
|
|
LOG(FATAL) << "Failed reading: " << path.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = broken_image_data;
|
2012-05-22 18:45:21 +02:00
|
|
|
break;
|
2012-02-16 18:11:49 +01:00
|
|
|
}
|
|
|
|
case IDR_TEXTAREA_RESIZER: {
|
|
|
|
// Use webkit's text area resizer image.
|
|
|
|
static std::string resize_corner_data;
|
|
|
|
if (resize_corner_data.empty()) {
|
|
|
|
FilePath path = GetResourcesFilePath();
|
|
|
|
path = path.AppendASCII("textAreaResizeCorner.png");
|
|
|
|
bool success = file_util::ReadFileToString(path, &resize_corner_data);
|
|
|
|
if (!success) {
|
|
|
|
LOG(FATAL) << "Failed reading: " << path.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = resize_corner_data;
|
2012-05-22 18:45:21 +02:00
|
|
|
break;
|
2012-02-16 18:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // defined(OS_MACOSX)
|
|
|
|
|
2012-06-21 22:35:13 +02:00
|
|
|
if (value.empty() && !settings_.pack_loading_disabled) {
|
|
|
|
value = ResourceBundle::GetSharedInstance().GetRawDataResource(
|
|
|
|
resource_id, ui::SCALE_FACTOR_NONE);
|
|
|
|
}
|
2012-02-16 18:11:49 +01:00
|
|
|
|
|
|
|
if (value.empty())
|
|
|
|
LOG(ERROR) << "No data resource available for id " << resource_id;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
FilePath CefContext::GetResourcesFilePath() const {
|
|
|
|
FilePath path;
|
|
|
|
// We need to know if we're bundled or not to know which path to use.
|
|
|
|
if (base::mac::AmIBundled()) {
|
|
|
|
PathService::Get(base::DIR_EXE, &path);
|
|
|
|
path = path.Append(FilePath::kParentDirectory);
|
|
|
|
return path.AppendASCII("Resources");
|
|
|
|
} else {
|
|
|
|
// TODO(port): Allow the embedder to customize the resource path.
|
|
|
|
PathService::Get(base::DIR_SOURCE_ROOT, &path);
|
|
|
|
path = path.AppendASCII("src");
|
|
|
|
path = path.AppendASCII("cef");
|
|
|
|
path = path.AppendASCII("tests");
|
|
|
|
path = path.AppendASCII("cefclient");
|
|
|
|
return path.AppendASCII("res");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // defined(OS_MACOSX)
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
std::string CefContext::locale() const {
|
2011-11-17 19:52:16 +01:00
|
|
|
std::string localeStr = CefString(&settings_.locale);
|
|
|
|
if (!localeStr.empty())
|
|
|
|
return localeStr;
|
|
|
|
|
|
|
|
return "en-US";
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void CefContext::UIT_FinishShutdown(
|
|
|
|
base::WaitableEvent* browser_shutdown_event,
|
|
|
|
base::WaitableEvent* uithread_shutdown_event) {
|
2011-01-07 02:06:10 +01:00
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
|
|
|
|
BrowserList list;
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
if (!browserlist_.empty()) {
|
|
|
|
list = browserlist_;
|
|
|
|
browserlist_.clear();
|
|
|
|
}
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy any remaining browser windows.
|
|
|
|
if (!list.empty()) {
|
|
|
|
BrowserList::iterator it = list.begin();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (; it != list.end(); ++it)
|
2011-01-07 02:06:10 +01:00
|
|
|
(*it)->UIT_DestroyBrowser();
|
|
|
|
}
|
|
|
|
|
2011-06-14 19:10:15 +02:00
|
|
|
if (uithread_shutdown_event) {
|
|
|
|
// The destruction observer will signal the UI thread shutdown event when
|
|
|
|
// the UI thread has been destroyed.
|
|
|
|
MessageLoop::current()->AddDestructionObserver(
|
|
|
|
new DestructionObserver(uithread_shutdown_event));
|
|
|
|
|
|
|
|
// Signal the browser shutdown event now.
|
|
|
|
browser_shutdown_event->Signal();
|
|
|
|
}
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|