Commit 1e871cc2 authored by Marshall Greenblatt's avatar Marshall Greenblatt

- Pass information to the renderer process synchronously on render thread...

- Pass information to the renderer process synchronously on render thread creation and new browser creation to avoid race conditions (issue #744).
- Add the ability to pass extra information to child processes using a new CefBrowserProcessHandler::OnRenderProcessThreadCreated callback (issue #744).
- Fix OnBeforeChildProcessLaunch documentation (issue #754).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@910 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
parent 8a504d3d
......@@ -72,12 +72,26 @@ typedef struct _cef_browser_process_handler_t {
struct _cef_browser_process_handler_t* self);
///
// Called on the browser process IO thread before a child process is launched.
// Provides an opportunity to modify the child process command line.
// Called before a child process is launched. Will be called on the browser
// process UI thread when launching a render process and on the browser
// process IO thread when launching a GPU or plugin process. Provides an
// opportunity to modify the child process command line. Do not keep a
// reference to |command_line| outside of this function.
///
void (CEF_CALLBACK *on_before_child_process_launch)(
struct _cef_browser_process_handler_t* self,
struct _cef_command_line_t* command_line);
///
// Called on the browser process IO thread after the main thread has been
// created for a new render process. Provides an opportunity to specify extra
// information that will be passed to
// cef_render_process_handler_t::on_render_thread_created() in the render
// process. Do not keep a reference to |extra_info| outside of this function.
///
void (CEF_CALLBACK *on_render_process_thread_created)(
struct _cef_browser_process_handler_t* self,
struct _cef_list_value_t* extra_info);
} cef_browser_process_handler_t;
......
......@@ -56,10 +56,14 @@ typedef struct _cef_render_process_handler_t {
cef_base_t base;
///
// Called after the render process main thread has been created.
// Called after the render process main thread has been created. |extra_info|
// is a read-only value originating from
// cef_browser_process_handler_t::on_render_process_thread_created(). Do not
// keep a reference to |extra_info| outside of this function.
///
void (CEF_CALLBACK *on_render_thread_created)(
struct _cef_render_process_handler_t* self);
struct _cef_render_process_handler_t* self,
struct _cef_list_value_t* extra_info);
///
// Called after WebKit has been initialized.
......
......@@ -41,6 +41,7 @@
#include "include/cef_base.h"
#include "include/cef_command_line.h"
#include "include/cef_proxy_handler.h"
#include "include/cef_values.h"
///
// Class used to implement browser process callbacks. The methods of this class
......@@ -67,13 +68,26 @@ class CefBrowserProcessHandler : public virtual CefBase {
virtual void OnContextInitialized() {}
///
// Called on the browser process IO thread before a child process is launched.
// Provides an opportunity to modify the child process command line.
// Called before a child process is launched. Will be called on the browser
// process UI thread when launching a render process and on the browser
// process IO thread when launching a GPU or plugin process. Provides an
// opportunity to modify the child process command line. Do not keep a
// reference to |command_line| outside of this method.
///
/*--cef()--*/
virtual void OnBeforeChildProcessLaunch(
CefRefPtr<CefCommandLine> command_line) {
}
CefRefPtr<CefCommandLine> command_line) {}
///
// Called on the browser process IO thread after the main thread has been
// created for a new render process. Provides an opportunity to specify extra
// information that will be passed to
// CefRenderProcessHandler::OnRenderThreadCreated() in the render process. Do
// not keep a reference to |extra_info| outside of this method.
///
/*--cef()--*/
virtual void OnRenderProcessThreadCreated(
CefRefPtr<CefListValue> extra_info) {}
};
#endif // CEF_INCLUDE_CEF_BROWSER_PROCESS_HANDLER_H_
......@@ -44,6 +44,7 @@
#include "include/cef_frame.h"
#include "include/cef_process_message.h"
#include "include/cef_v8.h"
#include "include/cef_values.h"
///
// Class used to implement render process callbacks. The methods of this class
......@@ -55,10 +56,13 @@ class CefRenderProcessHandler : public virtual CefBase {
typedef cef_navigation_type_t NavigationType;
///
// Called after the render process main thread has been created.
// Called after the render process main thread has been created. |extra_info|
// is a read-only value originating from
// CefBrowserProcessHandler::OnRenderProcessThreadCreated(). Do not keep a
// reference to |extra_info| outside of this method.
///
/*--cef()--*/
virtual void OnRenderThreadCreated() {}
virtual void OnRenderThreadCreated(CefRefPtr<CefListValue> extra_info) {}
///
// Called after WebKit has been initialized.
......
......@@ -10,6 +10,7 @@
#include "libcef/browser/browser_context.h"
#include "libcef/browser/chrome_scheme_handler.h"
#include "libcef/browser/content_browser_client.h"
#include "libcef/browser/context.h"
#include "libcef/browser/devtools_delegate.h"
#include "libcef/browser/navigate_params.h"
......@@ -251,8 +252,10 @@ CefRefPtr<CefBrowser> CefBrowserHost::CreateBrowserSync(
return NULL;
}
int browser_id = _Context->GetNextBrowserID();
CefRefPtr<CefBrowserHostImpl> browser =
CefBrowserHostImpl::Create(windowInfo, settings, client, NULL, NULL);
CefBrowserHostImpl::Create(windowInfo, settings, client, NULL, browser_id,
NULL);
if (!url.empty())
browser->LoadURL(CefFrameHostImpl::kMainFrameId, url);
return browser.get();
......@@ -268,6 +271,7 @@ CefRefPtr<CefBrowserHostImpl> CefBrowserHostImpl::Create(
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
int browser_id,
CefWindowHandle opener) {
CEF_REQUIRE_UIT();
......@@ -281,7 +285,7 @@ CefRefPtr<CefBrowserHostImpl> CefBrowserHostImpl::Create(
CefRefPtr<CefBrowserHostImpl> browser =
new CefBrowserHostImpl(window_info, settings, client, web_contents,
opener);
browser_id, opener);
if (!browser->PlatformCreateWindow())
return NULL;
......@@ -561,7 +565,7 @@ void CefBrowserHostImpl::StopLoad() {
}
int CefBrowserHostImpl::GetIdentifier() {
return unique_id();
return browser_id();
}
bool CefBrowserHostImpl::IsPopup() {
......@@ -669,12 +673,6 @@ bool CefBrowserHostImpl::SendProcessMessage(
// CefBrowserHostImpl public methods.
// -----------------------------------------------------------------------------
void CefBrowserHostImpl::SetUniqueId(int unique_id) {
CEF_REQUIRE_UIT();
unique_id_ = unique_id;
Send(new CefMsg_UpdateBrowserWindowId(routing_id(), unique_id, IsPopup()));
}
void CefBrowserHostImpl::DestroyBrowser() {
CEF_REQUIRE_UIT();
......@@ -1218,9 +1216,16 @@ void CefBrowserHostImpl::WebContentsCreated(
if (source_contents)
opener = GetBrowserForContents(source_contents)->GetWindowHandle();
CefContentBrowserClient::NewPopupBrowserInfo info;
CefContentBrowserClient::Get()->GetNewPopupBrowserInfo(
new_contents->GetRenderProcessHost()->GetID(),
new_contents->GetRoutingID(),
&info);
DCHECK_GT(info.browser_id, 0);
CefRefPtr<CefBrowserHostImpl> browser = CefBrowserHostImpl::Create(
pending_window_info_, pending_settings_, pending_client_, new_contents,
opener);
info.browser_id, opener);
pending_client_ = NULL;
}
......@@ -1288,6 +1293,12 @@ void CefBrowserHostImpl::RenderViewDeleted(
}
void CefBrowserHostImpl::RenderViewReady() {
if (IsPopup()) {
CefContentBrowserClient::Get()->ClearNewPopupBrowserInfo(
web_contents()->GetRenderProcessHost()->GetID(),
web_contents()->GetRoutingID());
}
// Send the queued messages.
queue_messages_ = false;
while (!queued_messages_.empty()) {
......@@ -1511,15 +1522,16 @@ CefBrowserHostImpl::CefBrowserHostImpl(const CefWindowInfo& window_info,
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
int browser_id,
CefWindowHandle opener)
: content::WebContentsObserver(web_contents),
window_info_(window_info),
settings_(settings),
client_(client),
browser_id_(browser_id),
opener_(opener),
render_process_id_(0),
render_view_id_(0),
unique_id_(0),
render_process_id_(MSG_ROUTING_NONE),
render_view_id_(MSG_ROUTING_NONE),
is_loading_(false),
can_go_back_(false),
can_go_forward_(false),
......
......@@ -78,6 +78,7 @@ class CefBrowserHostImpl : public CefBrowserHost,
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
int browser_id,
CefWindowHandle opener);
// Returns the browser associated with the specified RenderViewHost.
......@@ -138,9 +139,6 @@ class CefBrowserHostImpl : public CefBrowserHost,
CefProcessId target_process,
CefRefPtr<CefProcessMessage> message) OVERRIDE;
// Set the unique identifier for this browser.
void SetUniqueId(int unique_id);
// Destroy the browser members. This method should only be called after the
// native browser window is not longer processing messages.
void DestroyBrowser();
......@@ -198,7 +196,7 @@ class CefBrowserHostImpl : public CefBrowserHost,
// Thread safe accessors.
const CefBrowserSettings& settings() const { return settings_; }
CefRefPtr<CefClient> client() const { return client_; }
int unique_id() const { return unique_id_; }
int browser_id() const { return browser_id_; }
// Returns the URL that is currently loading (or loaded) in the main frame.
GURL GetLoadingURL();
......@@ -323,6 +321,7 @@ class CefBrowserHostImpl : public CefBrowserHost,
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
int browser_id,
CefWindowHandle opener);
// Initialize settings based on the specified RenderViewHost.
......@@ -398,6 +397,7 @@ class CefBrowserHostImpl : public CefBrowserHost,
CefBrowserSettings settings_;
CefRefPtr<CefClient> client_;
scoped_ptr<content::WebContents> web_contents_;
int browser_id_;
CefWindowHandle opener_;
// Unique ids used for routing communication to/from the renderer. We keep a
......@@ -406,9 +406,6 @@ class CefBrowserHostImpl : public CefBrowserHost,
int render_process_id_;
int render_view_id_;
// Unique id for the browser.
int unique_id_;
// Used when creating a new popup window.
CefWindowInfo pending_window_info_;
CefBrowserSettings pending_settings_;
......
......@@ -5,12 +5,17 @@
#include "libcef/browser/browser_message_filter.h"
#include "libcef/browser/browser_host_impl.h"
#include "libcef/browser/content_browser_client.h"
#include "libcef/browser/context.h"
#include "libcef/browser/origin_whitelist_impl.h"
#include "libcef/browser/thread_util.h"
#include "libcef/common/cef_messages.h"
#include "libcef/common/values_impl.h"
#include "base/compiler_specific.h"
#include "base/bind.h"
#include "content/public/browser/render_process_host.h"
CefBrowserMessageFilter::CefBrowserMessageFilter(
content::RenderProcessHost* host)
......@@ -31,22 +36,50 @@ void CefBrowserMessageFilter::OnFilterRemoved() {
bool CefBrowserMessageFilter::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(CefBrowserMessageFilter, message)
IPC_MESSAGE_HANDLER(CefProcessHostMsg_RenderThreadStarted,
OnRenderThreadStarted)
IPC_MESSAGE_HANDLER(CefProcessHostMsg_GetNewRenderThreadInfo,
OnGetNewRenderThreadInfo)
IPC_MESSAGE_HANDLER(CefProcessHostMsg_GetNewBrowserInfo,
OnGetNewBrowserInfo)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void CefBrowserMessageFilter::OnRenderThreadStarted() {
// Execute registration on the UI thread.
CEF_POST_TASK(CEF_UIT,
base::Bind(&CefBrowserMessageFilter::RegisterOnUIThread, this));
bool CefBrowserMessageFilter::Send(IPC::Message* message) {
return host_->Send(message);
}
void CefBrowserMessageFilter::RegisterOnUIThread() {
CEF_REQUIRE_UIT();
// Send existing registrations to the new render process.
RegisterCrossOriginWhitelistEntriesWithHost(host_);
void CefBrowserMessageFilter::OnGetNewRenderThreadInfo(
CefProcessHostMsg_GetNewRenderThreadInfo_Params* params) {
GetCrossOriginWhitelistEntries(&params->cross_origin_whitelist_entries);
CefRefPtr<CefApp> app = _Context->application();
if (app.get()) {
CefRefPtr<CefBrowserProcessHandler> handler =
app->GetBrowserProcessHandler();
if (handler.get()) {
CefRefPtr<CefListValueImpl> listValuePtr(
new CefListValueImpl(&params->extra_info, false, false));
handler->OnRenderProcessThreadCreated(listValuePtr.get());
listValuePtr->Detach(NULL);
}
}
}
void CefBrowserMessageFilter::OnGetNewBrowserInfo(
int routing_id, CefProcessHostMsg_GetNewBrowserInfo_Params* params) {
CefRefPtr<CefBrowserHostImpl> browser =
CefBrowserHostImpl::GetBrowserByRoutingID(host_->GetID(), routing_id);
if (browser.get()) {
params->browser_id = browser->GetIdentifier();
params->is_popup = browser->IsPopup();
} else {
CefContentBrowserClient::NewPopupBrowserInfo info;
CefContentBrowserClient::Get()->GetNewPopupBrowserInfo(host_->GetID(),
routing_id,
&info);
DCHECK_GT(info.browser_id, 0);
params->browser_id = info.browser_id;
params->is_popup = true;
}
}
......@@ -13,6 +13,9 @@ namespace content {
class RenderProcessHost;
}
struct CefProcessHostMsg_GetNewBrowserInfo_Params;
struct CefProcessHostMsg_GetNewRenderThreadInfo_Params;
// This class sends and receives control messages on the browser process.
class CefBrowserMessageFilter : public IPC::ChannelProxy::MessageFilter {
public:
......@@ -24,11 +27,14 @@ class CefBrowserMessageFilter : public IPC::ChannelProxy::MessageFilter {
virtual void OnFilterRemoved() OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
bool Send(IPC::Message* message);
private:
// Message handlers.
void OnRenderThreadStarted();
void RegisterOnUIThread();
void OnGetNewRenderThreadInfo(
CefProcessHostMsg_GetNewRenderThreadInfo_Params* params);
void OnGetNewBrowserInfo(int routing_id,
CefProcessHostMsg_GetNewBrowserInfo_Params* params);
content::RenderProcessHost* host_;
IPC::Channel* channel_;
......
......@@ -247,6 +247,44 @@ CefContentBrowserClient::CefContentBrowserClient()
CefContentBrowserClient::~CefContentBrowserClient() {
}
// static
CefContentBrowserClient* CefContentBrowserClient::Get() {
return static_cast<CefContentBrowserClient*>(
content::GetContentClient()->browser());
}
void CefContentBrowserClient::GetNewPopupBrowserInfo(
int render_process_id, int render_view_id, NewPopupBrowserInfo* info) {
base::AutoLock lock_scope(new_popup_browser_lock_);
NewPopupBrowserInfoMap::const_iterator it =
new_popup_browser_info_map_.find(
std::make_pair(render_process_id, render_view_id));
if (it != new_popup_browser_info_map_.end()) {
*info = it->second;
return;
}
// Create the info now.
NewPopupBrowserInfo new_info;
new_info.browser_id = _Context->GetNextBrowserID();
new_popup_browser_info_map_.insert(
std::make_pair(
std::make_pair(render_process_id, render_view_id), new_info));
*info = new_info;
}
void CefContentBrowserClient::ClearNewPopupBrowserInfo(int render_process_id,
int render_view_id) {
base::AutoLock lock_scope(new_popup_browser_lock_);
NewPopupBrowserInfoMap::iterator it =
new_popup_browser_info_map_.find(
std::make_pair(render_process_id, render_view_id));
if (it != new_popup_browser_info_map_.end())
new_popup_browser_info_map_.erase(it);
}
content::BrowserMainParts* CefContentBrowserClient::CreateBrowserMainParts(
const content::MainFunctionParams& parameters) {
browser_main_parts_ = new CefBrowserMainParts(parameters);
......
......@@ -6,12 +6,14 @@
#define CEF_LIBCEF_BROWSER_CONTENT_BROWSER_CLIENT_H_
#pragma once
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/content_browser_client.h"
class CefBrowserMainParts;
......@@ -28,10 +30,29 @@ class CefContentBrowserClient : public content::ContentBrowserClient {
CefContentBrowserClient();
virtual ~CefContentBrowserClient();
// Returns the singleton CefContentBrowserClient instance.
static CefContentBrowserClient* Get();
CefBrowserMainParts* browser_main_parts() const {
return browser_main_parts_;
}
// During popup window creation there is a race between the call to
// CefBrowserMessageFilter::OnGetNewBrowserInfo on the IO thread and the call
// to CefBrowserHostImpl::WebContentsCreated on the UI thread. To resolve this
// race we create the info when requested for the first time.
class NewPopupBrowserInfo {
public:
NewPopupBrowserInfo()
: browser_id(0) {}
int browser_id;
};
void GetNewPopupBrowserInfo(int render_process_id,
int render_view_id,
NewPopupBrowserInfo* info);
void ClearNewPopupBrowserInfo(int render_process_id,
int render_view_id);
virtual content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams& parameters) OVERRIDE;
virtual void RenderProcessHostCreated(
......@@ -60,6 +81,14 @@ class CefContentBrowserClient : public content::ContentBrowserClient {
scoped_ptr<content::PluginServiceFilter> plugin_service_filter_;
scoped_ptr<CefResourceDispatcherHostDelegate>
resource_dispatcher_host_delegate_;
base::Lock new_popup_browser_lock_;
// Map of (render_process_id, render_view_id) to info. Access must be
// protected by |new_popup_browser_lock_|.
typedef std::map<std::pair<int, int>, NewPopupBrowserInfo>
NewPopupBrowserInfoMap;
NewPopupBrowserInfoMap new_popup_browser_info_map_;
};
#endif // CEF_LIBCEF_BROWSER_CONTENT_BROWSER_CLIENT_H_
......@@ -167,8 +167,7 @@ void CefQuitMessageLoop() {
CefContext::CefContext()
: initialized_(false),
shutting_down_(false),
init_thread_id_(0),
next_browser_id_(kNextBrowserIdReset) {
init_thread_id_(0) {
}
CefContext::~CefContext() {
......@@ -271,48 +270,26 @@ bool CefContext::OnInitThread() {
return (base::PlatformThread::CurrentId() == init_thread_id_);
}
bool CefContext::AddBrowser(CefRefPtr<CefBrowserHostImpl> browser) {
bool found = false;
int CefContext::GetNextBrowserID() {
return next_browser_id_.GetNext() + 1;
}
void CefContext::AddBrowser(CefRefPtr<CefBrowserHostImpl> browser) {
AutoLock lock_scope(this);
// 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->SetUniqueId(next_browser_id_++);
browserlist_.push_back(browser);
}
return !found;
browserlist_.push_back(browser);
}
bool CefContext::RemoveBrowser(CefRefPtr<CefBrowserHostImpl> browser) {
bool deleted = false;
{
AutoLock lock_scope(this);
void CefContext::RemoveBrowser(CefRefPtr<CefBrowserHostImpl> browser) {
AutoLock lock_scope(this);
BrowserList::iterator it = browserlist_.begin();
for (; it != browserlist_.end(); ++it) {
if (it->get() == browser.get()) {
browserlist_.erase(it);
deleted = true;
break;
}
BrowserList::iterator it = browserlist_.begin();
for (; it != browserlist_.end(); ++it) {
if (it->get() == browser.get()) {
browserlist_.erase(it);
break;
}
if (browserlist_.empty())
next_browser_id_ = kNextBrowserIdReset;
}
return deleted;
}
CefRefPtr<CefBrowserHostImpl> CefContext::GetBrowserByID(int id) {
......@@ -320,7 +297,7 @@ CefRefPtr<CefBrowserHostImpl> CefContext::GetBrowserByID(int id) {
BrowserList::const_iterator it = browserlist_.begin();
for (; it != browserlist_.end(); ++it) {
if (it->get()->unique_id() == id)
if (it->get()->browser_id() == id)
return it->get();
}
......
......@@ -13,6 +13,7 @@
#include "include/cef_app.h"
#include "include/cef_base.h"
#include "base/atomic_sequence_num.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/scoped_temp_dir.h"
......@@ -54,8 +55,9 @@ class CefContext : public CefBase {
// Returns true if the context is shutting down.
bool shutting_down() { return shutting_down_; }
bool AddBrowser(CefRefPtr<CefBrowserHostImpl> browser);
bool RemoveBrowser(CefRefPtr<CefBrowserHostImpl> browser);
int GetNextBrowserID();
void AddBrowser(CefRefPtr<CefBrowserHostImpl> browser);
void RemoveBrowser(CefRefPtr<CefBrowserHostImpl> browser);
CefRefPtr<CefBrowserHostImpl> GetBrowserByID(int id);
CefRefPtr<CefBrowserHostImpl> GetBrowserByRoutingID(int render_process_id,
int render_view_id);
......@@ -97,7 +99,7 @@ class CefContext : public CefBase {
BrowserList browserlist_;
// Used for assigning unique IDs to browser instances.
int next_browser_id_;
base::AtomicSequenceNumber next_browser_id_;
scoped_ptr<CefMainDelegate> main_delegate_;
scoped_ptr<content::ContentMainRunner> main_runner_;
......
......@@ -5,7 +5,7 @@
#include "libcef/browser/origin_whitelist_impl.h"
#include <string>
#include <list>
#include <vector>
#include "include/cef_origin_whitelist.h"
#include "libcef/browser/context.h"
......@@ -14,6 +14,7 @@
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/render_process_host.h"
#include "googleurl/src/gurl.h"
......@@ -31,25 +32,26 @@ class CefOriginWhitelistManager {
const std::string& target_protocol,
const std::string& target_domain,
bool allow_target_subdomains) {
CEF_REQUIRE_UIT();
OriginInfo info;
Cef_CrossOriginWhiteListEntry_Params info;
info.source_origin = source_origin;
info.target_protocol = target_protocol;
info.target_domain = target_domain;
info.allow_target_subdomains = allow_target_subdomains;
// Verify that the origin entry doesn't already exist.
OriginList::const_iterator it = origin_list_.begin();
for (; it != origin_list_.end(); ++it) {
if (it->Equals(info))
return false;
}
{
base::AutoLock lock_scope(lock_);
origin_list_.push_back(info);
// Verify that the origin entry doesn't already exist.
OriginList::const_iterator it = origin_list_.begin();
for (; it != origin_list_.end(); ++it) {
if (IsEqual(*it, info))
return false;
}
SendModifyCrossOriginWhitelistEntry(true, source_origin, target_protocol,
target_domain, allow_target_subdomains);
origin_list_.push_back(info);
}
SendModifyCrossOriginWhitelistEntry(true, info);
return true;