Index: content/browser/appcache/appcache_dispatcher_host.cc |
diff --git a/content/browser/appcache/appcache_dispatcher_host.cc b/content/browser/appcache/appcache_dispatcher_host.cc |
index 2eb90dd5189e5630c4b3baadc6cb9d40995e4791..67d216e15dbefd87de7351aaba5301de2d232d63 100644 |
--- a/content/browser/appcache/appcache_dispatcher_host.cc |
+++ b/content/browser/appcache/appcache_dispatcher_host.cc |
@@ -4,12 +4,22 @@ |
#include "content/browser/appcache/appcache_dispatcher_host.h" |
+#include <map> |
#include "base/bind.h" |
#include "base/bind_helpers.h" |
#include "content/browser/appcache/chrome_appcache_service.h" |
#include "content/browser/bad_message.h" |
#include "content/common/appcache_messages.h" |
+#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/user_metrics.h" |
+#include "content/public/common/browser_side_navigation_policy.h" |
+ |
+namespace { |
+ |
+typedef std::map<int, content::AppCacheDispatcherHost*> ProcessIdToHostMap; |
+base::LazyInstance<ProcessIdToHostMap> g_process_id_host_map; |
+ |
+} // namespace |
namespace content { |
@@ -21,22 +31,21 @@ AppCacheDispatcherHost::AppCacheDispatcherHost( |
frontend_proxy_(this), |
process_id_(process_id), |
weak_factory_(this) { |
+ g_process_id_host_map.Get()[process_id] = this; |
} |
void AppCacheDispatcherHost::OnChannelConnected(int32_t peer_pid) { |
- if (appcache_service_.get()) { |
- backend_impl_.Initialize( |
- appcache_service_.get(), &frontend_proxy_, process_id_); |
- get_status_callback_ = |
- base::Bind(&AppCacheDispatcherHost::GetStatusCallback, |
- weak_factory_.GetWeakPtr()); |
- start_update_callback_ = |
- base::Bind(&AppCacheDispatcherHost::StartUpdateCallback, |
- weak_factory_.GetWeakPtr()); |
- swap_cache_callback_ = |
- base::Bind(&AppCacheDispatcherHost::SwapCacheCallback, |
- weak_factory_.GetWeakPtr()); |
- } |
+ if (!appcache_service_.get()) |
+ return; |
+ |
+ backend_impl_.Initialize(appcache_service_.get(), &frontend_proxy_, |
+ process_id_); |
+ get_status_callback_ = base::Bind(&AppCacheDispatcherHost::GetStatusCallback, |
+ weak_factory_.GetWeakPtr()); |
+ start_update_callback_ = base::Bind( |
+ &AppCacheDispatcherHost::StartUpdateCallback, weak_factory_.GetWeakPtr()); |
+ swap_cache_callback_ = base::Bind(&AppCacheDispatcherHost::SwapCacheCallback, |
+ weak_factory_.GetWeakPtr()); |
} |
bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
@@ -62,10 +71,45 @@ bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
return handled; |
} |
-AppCacheDispatcherHost::~AppCacheDispatcherHost() {} |
+void AppCacheDispatcherHost::RegisterPendingHost(int host_id) { |
+ DCHECK(IsBrowserSideNavigationEnabled()); |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ DCHECK(host_id != kAppCacheNoHostId); |
+ DCHECK(pending_hosts_.find(host_id) == pending_hosts_.end()); |
+ pending_hosts_.insert(host_id); |
+} |
+ |
+AppCacheDispatcherHost::~AppCacheDispatcherHost() { |
+ ProcessIdToHostMap::iterator index = |
+ g_process_id_host_map.Get().find(process_id_); |
+ if ((index != g_process_id_host_map.Get().end()) && index->second == this) |
+ g_process_id_host_map.Get().erase(index); |
+} |
+ |
+void AppCacheDispatcherHost::RegisterPrecreatedHost( |
+ std::unique_ptr<AppCacheHost> host) { |
+ DCHECK(host.get()); |
+ DCHECK(IsBrowserSideNavigationEnabled()); |
+ |
+ // Before switching the backends we mark the host as pending so we can avoid |
+ // registering them again in the AppCacheHostMsg_RegisterHost IPC. |
+ RegisterPendingHost(host->host_id()); |
+ host->set_frontend(&frontend_proxy_); |
+ backend_impl_.RegisterPrecreatedHost(std::move(host)); |
+} |
void AppCacheDispatcherHost::OnRegisterHost(int host_id) { |
if (appcache_service_.get()) { |
+ // PlzNavigate. If the |host_id| is the pending_backends_ list, it means |
+ // it was registered via the RegisterPendingHost() function. We remove the |
+ // host from the pending list and return without registering the host. |
+ if (IsBrowserSideNavigationEnabled()) { |
+ auto found = pending_hosts_.find(host_id); |
+ if (found != pending_hosts_.end()) { |
+ pending_hosts_.erase(found); |
+ return; |
+ } |
+ } |
if (!backend_impl_.RegisterHost(host_id)) { |
bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER); |
} |
@@ -94,8 +138,7 @@ void AppCacheDispatcherHost::OnSelectCache( |
int64_t cache_document_was_loaded_from, |
const GURL& opt_manifest_url) { |
if (appcache_service_.get()) { |
- if (!backend_impl_.SelectCache(host_id, |
- document_url, |
+ if (!backend_impl_.SelectCache(host_id, document_url, |
cache_document_was_loaded_from, |
opt_manifest_url)) { |
bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE); |
@@ -108,8 +151,8 @@ void AppCacheDispatcherHost::OnSelectCache( |
void AppCacheDispatcherHost::OnSelectCacheForWorker( |
int host_id, int parent_process_id, int parent_host_id) { |
if (appcache_service_.get()) { |
- if (!backend_impl_.SelectCacheForWorker( |
- host_id, parent_process_id, parent_host_id)) { |
+ if (!backend_impl_.SelectCacheForWorker(host_id, parent_process_id, |
+ parent_host_id)) { |
bad_message::ReceivedBadMessage( |
this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER); |
} |
@@ -134,8 +177,8 @@ void AppCacheDispatcherHost::OnMarkAsForeignEntry( |
const GURL& document_url, |
int64_t cache_document_was_loaded_from) { |
if (appcache_service_.get()) { |
- if (!backend_impl_.MarkAsForeignEntry( |
- host_id, document_url, cache_document_was_loaded_from)) { |
+ if (!backend_impl_.MarkAsForeignEntry(host_id, document_url, |
+ cache_document_was_loaded_from)) { |
bad_message::ReceivedBadMessage(this, |
bad_message::ACDH_MARK_AS_FOREIGN_ENTRY); |
} |
@@ -158,8 +201,8 @@ void AppCacheDispatcherHost::OnGetStatus(int host_id, IPC::Message* reply_msg) { |
pending_reply_msg_.reset(reply_msg); |
if (appcache_service_.get()) { |
- if (!backend_impl_.GetStatusWithCallback( |
- host_id, get_status_callback_, reply_msg)) { |
+ if (!backend_impl_.GetStatusWithCallback(host_id, get_status_callback_, |
+ reply_msg)) { |
bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS); |
} |
return; |
@@ -179,8 +222,8 @@ void AppCacheDispatcherHost::OnStartUpdate(int host_id, |
pending_reply_msg_.reset(reply_msg); |
if (appcache_service_.get()) { |
- if (!backend_impl_.StartUpdateWithCallback( |
- host_id, start_update_callback_, reply_msg)) { |
+ if (!backend_impl_.StartUpdateWithCallback(host_id, start_update_callback_, |
+ reply_msg)) { |
bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE); |
} |
return; |
@@ -199,8 +242,8 @@ void AppCacheDispatcherHost::OnSwapCache(int host_id, IPC::Message* reply_msg) { |
pending_reply_msg_.reset(reply_msg); |
if (appcache_service_.get()) { |
- if (!backend_impl_.SwapCacheWithCallback( |
- host_id, swap_cache_callback_, reply_msg)) { |
+ if (!backend_impl_.SwapCacheWithCallback(host_id, swap_cache_callback_, |
+ reply_msg)) { |
bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE); |
} |
return; |
@@ -231,4 +274,14 @@ void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) { |
Send(pending_reply_msg_.release()); |
} |
+// static |
+scoped_refptr<AppCacheDispatcherHost> AppCacheDispatcherHost::GetHostForProcess( |
+ int process_id) { |
+ ProcessIdToHostMap::iterator index = |
+ g_process_id_host_map.Get().find(process_id); |
+ if (index == g_process_id_host_map.Get().end()) |
+ return scoped_refptr<AppCacheDispatcherHost>(); |
+ return index->second; |
+} |
+ |
} // namespace content |