2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2010 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 "cef_context.h"
|
|
|
|
#include "browser_impl.h"
|
|
|
|
#include "browser_webkit_glue.h"
|
|
|
|
#include "cef_thread.h"
|
|
|
|
#include "cef_process.h"
|
|
|
|
#include "../include/cef_nplugin.h"
|
|
|
|
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#if defined(OS_MACOSX) || defined(OS_WIN)
|
|
|
|
#include "base/nss_util.h"
|
|
|
|
#endif
|
2011-01-07 22:34:20 +01:00
|
|
|
#include "webkit/plugins/npapi/plugin_list.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Global CefContext pointer
|
|
|
|
CefRefPtr<CefContext> _Context;
|
|
|
|
|
2010-11-18 22:05:25 +01:00
|
|
|
bool CefInitialize(const CefSettings& settings,
|
|
|
|
const CefBrowserSettings& browser_defaults)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Return true if the global context already exists.
|
2010-10-03 23:04:50 +02:00
|
|
|
if(_Context.get())
|
|
|
|
return true;
|
|
|
|
|
2010-11-18 22:05:25 +01:00
|
|
|
if(settings.size != sizeof(cef_settings_t) ||
|
|
|
|
browser_defaults.size != sizeof(cef_browser_settings_t)) {
|
|
|
|
NOTREACHED();
|
|
|
|
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.
|
2010-11-18 22:05:25 +01:00
|
|
|
return _Context->Initialize(settings, browser_defaults);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefShutdown()
|
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
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.
|
|
|
|
if(!_Context->process()->CalledOnValidThread()) {
|
|
|
|
NOTREACHED();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDoMessageLoopWork()
|
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
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.
|
|
|
|
if(!_Context->process()->CalledOnValidThread()) {
|
|
|
|
NOTREACHED();
|
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();
|
|
|
|
}
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
static void UIT_RegisterPlugin(CefPluginInfo* plugin_info)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
REQUIRE_UIT();
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
webkit::npapi::WebPluginInfo info;
|
|
|
|
|
|
|
|
FilePath filename = FilePath(CefString(&plugin_info->unique_name));
|
|
|
|
std::string name = CefString(&plugin_info->display_name);
|
|
|
|
std::string description = CefString(&plugin_info->description);
|
|
|
|
std::string mime_type = CefString(&plugin_info->mime_type);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
webkit::npapi::PluginEntryPoints entry_points;
|
2010-12-07 02:30:26 +01:00
|
|
|
#if !defined(OS_POSIX) || defined(OS_MACOSX)
|
2011-02-15 19:07:24 +01:00
|
|
|
entry_points.np_getentrypoints = plugin_info->np_getentrypoints;
|
2010-12-07 02:30:26 +01:00
|
|
|
#endif
|
2011-02-15 19:07:24 +01:00
|
|
|
entry_points.np_initialize = plugin_info->np_initialize;
|
|
|
|
entry_points.np_shutdown = plugin_info->np_shutdown;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(filename,
|
|
|
|
name, description, mime_type, entry_points);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
delete plugin_info;
|
|
|
|
}
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
bool CefRegisterPlugin(const CefPluginInfo& plugin_info)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED();
|
2010-10-03 23:04:50 +02:00
|
|
|
return false;
|
2011-01-07 02:06:10 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
2011-02-15 19:07:24 +01:00
|
|
|
NewRunnableFunction(UIT_RegisterPlugin, new CefPluginInfo(plugin_info)));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetThreadId(CefThreadId threadId)
|
|
|
|
{
|
|
|
|
switch(threadId) {
|
|
|
|
case TID_UI: return CefThread::UI;
|
|
|
|
case TID_IO: return CefThread::IO;
|
|
|
|
case TID_FILE: return CefThread::FILE;
|
|
|
|
};
|
|
|
|
NOTREACHED();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCurrentlyOn(CefThreadId threadId)
|
|
|
|
{
|
|
|
|
int id = GetThreadId(threadId);
|
|
|
|
if(id < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CefThread::CurrentlyOn(static_cast<CefThread::ID>(id));
|
|
|
|
}
|
|
|
|
|
2011-01-29 23:52:34 +01:00
|
|
|
class CefTaskHelper : public Task
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-01-29 23:52:34 +01:00
|
|
|
CefTaskHelper(CefRefPtr<CefTask> task, CefThreadId threadId)
|
|
|
|
: task_(task), thread_id_(threadId) {}
|
|
|
|
virtual void Run() { task_->Execute(thread_id_); }
|
2010-10-03 23:04:50 +02:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefTask> task_;
|
2011-01-29 23:52:34 +01:00
|
|
|
CefThreadId thread_id_;
|
2010-10-03 23:04:50 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefTaskHelper);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool CefPostTask(CefThreadId threadId, CefRefPtr<CefTask> task)
|
|
|
|
{
|
|
|
|
int id = GetThreadId(threadId);
|
|
|
|
if(id < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CefThread::PostTask(static_cast<CefThread::ID>(id), FROM_HERE,
|
2011-01-29 23:52:34 +01:00
|
|
|
new CefTaskHelper(task, threadId));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPostDelayedTask(CefThreadId threadId, CefRefPtr<CefTask> task,
|
|
|
|
long delay_ms)
|
|
|
|
{
|
|
|
|
int id = GetThreadId(threadId);
|
|
|
|
if(id < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CefThread::PostDelayedTask(static_cast<CefThread::ID>(id), FROM_HERE,
|
2011-01-29 23:52:34 +01:00
|
|
|
new CefTaskHelper(task, threadId), delay_ms);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-01-31 21:47:04 +01:00
|
|
|
bool CefParseURL(const CefString& url,
|
|
|
|
CefURLParts& parts)
|
|
|
|
{
|
|
|
|
GURL gurl(url.ToString());
|
|
|
|
if (!gurl.is_valid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CefString(&parts.spec).FromString(gurl.spec());
|
|
|
|
CefString(&parts.scheme).FromString(gurl.scheme());
|
|
|
|
CefString(&parts.username).FromString(gurl.username());
|
|
|
|
CefString(&parts.password).FromString(gurl.password());
|
|
|
|
CefString(&parts.host).FromString(gurl.host());
|
|
|
|
CefString(&parts.port).FromString(gurl.port());
|
|
|
|
CefString(&parts.path).FromString(gurl.path());
|
|
|
|
CefString(&parts.query).FromString(gurl.query());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCreateURL(const CefURLParts& parts,
|
|
|
|
CefString& url)
|
|
|
|
{
|
|
|
|
std::string spec = CefString(parts.spec.str, parts.spec.length, false);
|
|
|
|
std::string scheme = CefString(parts.scheme.str, parts.scheme.length, false);
|
|
|
|
std::string username =
|
|
|
|
CefString(parts.username.str, parts.username.length, false);
|
|
|
|
std::string password =
|
|
|
|
CefString(parts.password.str, parts.password.length, false);
|
|
|
|
std::string host = CefString(parts.host.str, parts.host.length, false);
|
|
|
|
std::string port = CefString(parts.port.str, parts.port.length, false);
|
|
|
|
std::string path = CefString(parts.path.str, parts.path.length, false);
|
|
|
|
std::string query = CefString(parts.query.str, parts.query.length, false);
|
|
|
|
|
|
|
|
GURL gurl;
|
|
|
|
if (!spec.empty()) {
|
|
|
|
gurl = GURL(spec);
|
|
|
|
} else if (!scheme.empty() && !host.empty()) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << scheme << "://";
|
|
|
|
if (!username.empty()) {
|
|
|
|
ss << username;
|
|
|
|
if (!password.empty())
|
|
|
|
ss << ":" << password;
|
|
|
|
ss << "@";
|
|
|
|
}
|
|
|
|
ss << host;
|
|
|
|
if (!port.empty())
|
|
|
|
ss << ":" << port;
|
|
|
|
if (!path.empty())
|
|
|
|
ss << path;
|
|
|
|
if (!query.empty())
|
|
|
|
ss << "?" << query;
|
|
|
|
gurl = GURL(ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gurl.is_valid()) {
|
|
|
|
url = gurl.spec();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// CefContext
|
|
|
|
|
2011-01-07 02:06:10 +01:00
|
|
|
CefContext::CefContext() : initialized_(false), shutting_down_(false)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CefContext::~CefContext()
|
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
if(!shutting_down_)
|
|
|
|
Shutdown();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 22:05:25 +01:00
|
|
|
bool CefContext::Initialize(const CefSettings& settings,
|
|
|
|
const CefBrowserSettings& browser_defaults)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
2010-11-18 22:05:25 +01:00
|
|
|
settings_ = settings;
|
|
|
|
browser_defaults_ = browser_defaults;
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
cache_path_ = FilePath(CefString(&settings.cache_path));
|
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.
|
|
|
|
base::EnsureNSPRInit();
|
|
|
|
#endif
|
|
|
|
|
2010-11-18 22:05:25 +01:00
|
|
|
process_ = 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;
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::Shutdown()
|
|
|
|
{
|
2011-01-07 02:06:10 +01:00
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(process_->CalledOnValidThread());
|
|
|
|
|
|
|
|
shutting_down_ = true;
|
|
|
|
|
|
|
|
if(settings_.multi_threaded_message_loop) {
|
|
|
|
// Event that will be used to signal when shutdown is complete. Start in
|
|
|
|
// non-signaled mode so that the event will block.
|
|
|
|
base::WaitableEvent 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,
|
|
|
|
NewRunnableMethod(this, &CefContext::UIT_FinishShutdown, &event));
|
|
|
|
|
|
|
|
// Block until shutdown is complete.
|
|
|
|
event.Wait();
|
|
|
|
} else {
|
|
|
|
// Finish shutdown on the current thread, which should be the UI thread.
|
|
|
|
UIT_FinishShutdown(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the process to destroy the child threads.
|
2010-10-03 23:04:50 +02:00
|
|
|
process_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::AddBrowser(CefRefPtr<CefBrowserImpl> browser)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
Lock();
|
|
|
|
|
|
|
|
// check that the browser isn't already in the list before adding
|
|
|
|
BrowserList::const_iterator it = browserlist_.begin();
|
|
|
|
for(; it != browserlist_.end(); ++it) {
|
|
|
|
if(it->get() == browser.get()) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
browser->UIT_SetUniqueID(next_browser_id_++);
|
|
|
|
browserlist_.push_back(browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
return !found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::RemoveBrowser(CefRefPtr<CefBrowserImpl> browser)
|
|
|
|
{
|
|
|
|
bool deleted = false;
|
|
|
|
bool empty = false;
|
|
|
|
|
|
|
|
Lock();
|
|
|
|
|
|
|
|
BrowserList::iterator it = browserlist_.begin();
|
|
|
|
for(; it != browserlist_.end(); ++it) {
|
|
|
|
if(it->get() == browser.get()) {
|
|
|
|
browserlist_.erase(it);
|
|
|
|
deleted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browserlist_.empty()) {
|
|
|
|
next_browser_id_ = 1;
|
|
|
|
empty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
|
|
|
NewRunnableFunction(webkit_glue::ClearCache));
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserImpl> CefContext::GetBrowserByID(int id)
|
|
|
|
{
|
|
|
|
CefRefPtr<CefBrowserImpl> browser;
|
|
|
|
Lock();
|
|
|
|
|
|
|
|
BrowserList::const_iterator it = browserlist_.begin();
|
|
|
|
for(; it != browserlist_.end(); ++it) {
|
|
|
|
if(it->get()->UIT_GetUniqueID() == id) {
|
|
|
|
browser = it->get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
2011-01-07 02:06:10 +01:00
|
|
|
|
|
|
|
void CefContext::UIT_FinishShutdown(base::WaitableEvent* event)
|
|
|
|
{
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
|
|
|
|
BrowserList list;
|
|
|
|
|
|
|
|
Lock();
|
|
|
|
if (!browserlist_.empty()) {
|
|
|
|
list = browserlist_;
|
|
|
|
browserlist_.clear();
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
|
|
|
|
// Destroy any remaining browser windows.
|
|
|
|
if (!list.empty()) {
|
|
|
|
BrowserList::iterator it = list.begin();
|
|
|
|
for(; it != list.end(); ++it)
|
|
|
|
(*it)->UIT_DestroyBrowser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event)
|
|
|
|
event->Signal();
|
|
|
|
}
|