Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: content/browser/appcache/appcache_dispatcher_host.cc

Issue 2501343003: PlzNavigate: AppCache support. (Closed)
Patch Set: Add DCHECKs for PlzNavigate and fix a double Release problem which caused one unit_test to fail wit… Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/appcache/appcache_dispatcher_host.h" 5 #include "content/browser/appcache/appcache_dispatcher_host.h"
6 6
7 #include <map>
7 #include "base/bind.h" 8 #include "base/bind.h"
8 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
9 #include "content/browser/appcache/chrome_appcache_service.h" 10 #include "content/browser/appcache/chrome_appcache_service.h"
10 #include "content/browser/bad_message.h" 11 #include "content/browser/bad_message.h"
11 #include "content/common/appcache_messages.h" 12 #include "content/common/appcache_messages.h"
13 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/user_metrics.h" 14 #include "content/public/browser/user_metrics.h"
15 #include "content/public/common/browser_side_navigation_policy.h"
16
17 namespace {
18
19 typedef std::map<int, content::AppCacheDispatcherHost*> ProcessIdToHostMap;
20 base::LazyInstance<ProcessIdToHostMap> g_process_id_host_map;
21
22 } // namespace
13 23
14 namespace content { 24 namespace content {
15 25
16 AppCacheDispatcherHost::AppCacheDispatcherHost( 26 AppCacheDispatcherHost::AppCacheDispatcherHost(
17 ChromeAppCacheService* appcache_service, 27 ChromeAppCacheService* appcache_service,
18 int process_id) 28 int process_id)
19 : BrowserMessageFilter(AppCacheMsgStart), 29 : BrowserMessageFilter(AppCacheMsgStart),
20 appcache_service_(appcache_service), 30 appcache_service_(appcache_service),
21 frontend_proxy_(this), 31 frontend_proxy_(this),
22 process_id_(process_id), 32 process_id_(process_id),
23 weak_factory_(this) { 33 weak_factory_(this) {
34 g_process_id_host_map.Get()[process_id] = this;
24 } 35 }
25 36
26 void AppCacheDispatcherHost::OnChannelConnected(int32_t peer_pid) { 37 void AppCacheDispatcherHost::OnChannelConnected(int32_t peer_pid) {
27 if (appcache_service_.get()) { 38 if (!appcache_service_.get() || backend_impl_.get())
28 backend_impl_.Initialize( 39 return;
29 appcache_service_.get(), &frontend_proxy_, process_id_); 40
30 get_status_callback_ = 41 backend_impl_.reset(new AppCacheBackendImpl);
31 base::Bind(&AppCacheDispatcherHost::GetStatusCallback, 42 backend_impl_->Initialize(appcache_service_.get(), &frontend_proxy_,
32 weak_factory_.GetWeakPtr()); 43 process_id_, -1);
33 start_update_callback_ = 44 get_status_callback_ = base::Bind(&AppCacheDispatcherHost::GetStatusCallback,
34 base::Bind(&AppCacheDispatcherHost::StartUpdateCallback, 45 weak_factory_.GetWeakPtr());
35 weak_factory_.GetWeakPtr()); 46 start_update_callback_ = base::Bind(
36 swap_cache_callback_ = 47 &AppCacheDispatcherHost::StartUpdateCallback, weak_factory_.GetWeakPtr());
37 base::Bind(&AppCacheDispatcherHost::SwapCacheCallback, 48 swap_cache_callback_ = base::Bind(&AppCacheDispatcherHost::SwapCacheCallback,
38 weak_factory_.GetWeakPtr()); 49 weak_factory_.GetWeakPtr());
39 }
40 } 50 }
41 51
42 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) { 52 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) {
43 bool handled = true; 53 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP(AppCacheDispatcherHost, message) 54 IPC_BEGIN_MESSAGE_MAP(AppCacheDispatcherHost, message)
45 IPC_MESSAGE_HANDLER(AppCacheHostMsg_RegisterHost, OnRegisterHost) 55 IPC_MESSAGE_HANDLER(AppCacheHostMsg_RegisterHost, OnRegisterHost)
46 IPC_MESSAGE_HANDLER(AppCacheHostMsg_UnregisterHost, OnUnregisterHost) 56 IPC_MESSAGE_HANDLER(AppCacheHostMsg_UnregisterHost, OnUnregisterHost)
47 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SetSpawningHostId, OnSetSpawningHostId) 57 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SetSpawningHostId, OnSetSpawningHostId)
48 IPC_MESSAGE_HANDLER(AppCacheHostMsg_GetResourceList, OnGetResourceList) 58 IPC_MESSAGE_HANDLER(AppCacheHostMsg_GetResourceList, OnGetResourceList)
49 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCache, OnSelectCache) 59 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCache, OnSelectCache)
50 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForWorker, 60 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForWorker,
51 OnSelectCacheForWorker) 61 OnSelectCacheForWorker)
52 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForSharedWorker, 62 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForSharedWorker,
53 OnSelectCacheForSharedWorker) 63 OnSelectCacheForSharedWorker)
54 IPC_MESSAGE_HANDLER(AppCacheHostMsg_MarkAsForeignEntry, 64 IPC_MESSAGE_HANDLER(AppCacheHostMsg_MarkAsForeignEntry,
55 OnMarkAsForeignEntry) 65 OnMarkAsForeignEntry)
56 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_GetStatus, OnGetStatus) 66 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_GetStatus, OnGetStatus)
57 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_StartUpdate, OnStartUpdate) 67 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_StartUpdate, OnStartUpdate)
58 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_SwapCache, OnSwapCache) 68 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_SwapCache, OnSwapCache)
59 IPC_MESSAGE_UNHANDLED(handled = false) 69 IPC_MESSAGE_UNHANDLED(handled = false)
60 IPC_END_MESSAGE_MAP() 70 IPC_END_MESSAGE_MAP()
61 71
62 return handled; 72 return handled;
63 } 73 }
64 74
65 AppCacheDispatcherHost::~AppCacheDispatcherHost() {} 75 void AppCacheDispatcherHost::RegisterPendingHost(int host_id) {
76 DCHECK(IsBrowserSideNavigationEnabled());
77 DCHECK_CURRENTLY_ON(BrowserThread::IO);
78 DCHECK(host_id != kAppCacheNoHostId);
79 DCHECK(pending_hosts_.find(host_id) == pending_hosts_.end());
80 pending_hosts_.insert(host_id);
81 }
82
83 AppCacheDispatcherHost::~AppCacheDispatcherHost() {
84 ProcessIdToHostMap::iterator index =
85 g_process_id_host_map.Get().find(process_id_);
86 if ((index != g_process_id_host_map.Get().end()) && index->second == this)
87 g_process_id_host_map.Get().erase(index);
88 }
89
90 void AppCacheDispatcherHost::FrameNavigationCommitted(
91 std::unique_ptr<AppCacheBackendImpl> backend) {
92 DCHECK(backend.get());
93 DCHECK(IsBrowserSideNavigationEnabled());
94
95 // Before switching the backends we do the following:
96 // 1. Mark hosts registered with the |backend| as pending so we can avoid
97 // registering them again in the AppCacheHostMsg_RegisterHost IPC.
98 // 2. Unregister the backend we currently have in this class.
99 // 3. Copy registered hosts to the new backend.
100 // 4. Update the AppCacheFrontEnd and the AppCacheServiceImpl pointers in the
101 // new backend as they may have changed.
102 // 5. Register the new backend with the service.
103 AppCacheBackendImpl::HostMap::const_iterator host_index;
104 for (host_index = backend->hosts().begin();
105 host_index != backend->hosts().end(); ++host_index) {
106 RegisterPendingHost(host_index->first);
107 }
108
109 if (backend_impl_.get()) {
110 appcache_service_->UnregisterBackend(backend_impl_.get());
111 backend->CopyExistingHosts(backend_impl_.get());
112 }
113
114 backend_impl_ = std::move(backend);
115
116 backend_impl_->FrameNavigationCommitted(&frontend_proxy_,
117 appcache_service_.get());
michaeln 2016/11/22 00:17:27 it doesn't make sense to me that the service objec
ananta 2016/11/23 04:05:14 Reverted.
118
119 appcache_service_->RegisterBackend(backend_impl_.get());
120 }
66 121
67 void AppCacheDispatcherHost::OnRegisterHost(int host_id) { 122 void AppCacheDispatcherHost::OnRegisterHost(int host_id) {
68 if (appcache_service_.get()) { 123 if (appcache_service_.get()) {
69 if (!backend_impl_.RegisterHost(host_id)) { 124 // PlzNavigate. If the |host_id| is the pending_backends_ list, it means
125 // it was registered via the RegisterPendingHost() function. We remove the
126 // host from the pending list and return without registering the host.
127 if (IsBrowserSideNavigationEnabled()) {
128 std::set<int>::iterator pending_host_index = pending_hosts_.find(host_id);
michaeln 2016/11/22 00:17:27 readability nit: auto found = x.find(id)
ananta 2016/11/23 04:05:14 Done.
129 if (pending_host_index != pending_hosts_.end()) {
130 pending_hosts_.erase(pending_host_index);
131 return;
132 }
133 }
134 if (!backend_impl_->RegisterHost(host_id)) {
70 bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER); 135 bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER);
71 } 136 }
72 } 137 }
73 } 138 }
74 139
75 void AppCacheDispatcherHost::OnUnregisterHost(int host_id) { 140 void AppCacheDispatcherHost::OnUnregisterHost(int host_id) {
76 if (appcache_service_.get()) { 141 if (appcache_service_.get()) {
77 if (!backend_impl_.UnregisterHost(host_id)) { 142 if (!backend_impl_->UnregisterHost(host_id)) {
78 bad_message::ReceivedBadMessage(this, bad_message::ACDH_UNREGISTER); 143 bad_message::ReceivedBadMessage(this, bad_message::ACDH_UNREGISTER);
79 } 144 }
80 } 145 }
81 } 146 }
82 147
83 void AppCacheDispatcherHost::OnSetSpawningHostId( 148 void AppCacheDispatcherHost::OnSetSpawningHostId(
84 int host_id, int spawning_host_id) { 149 int host_id, int spawning_host_id) {
85 if (appcache_service_.get()) { 150 if (appcache_service_.get()) {
86 if (!backend_impl_.SetSpawningHostId(host_id, spawning_host_id)) 151 if (!backend_impl_->SetSpawningHostId(host_id, spawning_host_id))
87 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SET_SPAWNING); 152 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SET_SPAWNING);
88 } 153 }
89 } 154 }
90 155
91 void AppCacheDispatcherHost::OnSelectCache( 156 void AppCacheDispatcherHost::OnSelectCache(
92 int host_id, 157 int host_id,
93 const GURL& document_url, 158 const GURL& document_url,
94 int64_t cache_document_was_loaded_from, 159 int64_t cache_document_was_loaded_from,
95 const GURL& opt_manifest_url) { 160 const GURL& opt_manifest_url) {
96 if (appcache_service_.get()) { 161 if (appcache_service_.get()) {
97 if (!backend_impl_.SelectCache(host_id, 162 if (!backend_impl_->SelectCache(host_id, document_url,
98 document_url, 163 cache_document_was_loaded_from,
99 cache_document_was_loaded_from, 164 opt_manifest_url)) {
100 opt_manifest_url)) {
101 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE); 165 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE);
102 } 166 }
103 } else { 167 } else {
104 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); 168 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo());
105 } 169 }
106 } 170 }
107 171
108 void AppCacheDispatcherHost::OnSelectCacheForWorker( 172 void AppCacheDispatcherHost::OnSelectCacheForWorker(
109 int host_id, int parent_process_id, int parent_host_id) { 173 int host_id, int parent_process_id, int parent_host_id) {
110 if (appcache_service_.get()) { 174 if (appcache_service_.get()) {
111 if (!backend_impl_.SelectCacheForWorker( 175 if (!backend_impl_->SelectCacheForWorker(host_id, parent_process_id,
112 host_id, parent_process_id, parent_host_id)) { 176 parent_host_id)) {
113 bad_message::ReceivedBadMessage( 177 bad_message::ReceivedBadMessage(
114 this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER); 178 this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER);
115 } 179 }
116 } else { 180 } else {
117 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); 181 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo());
118 } 182 }
119 } 183 }
120 184
121 void AppCacheDispatcherHost::OnSelectCacheForSharedWorker(int host_id, 185 void AppCacheDispatcherHost::OnSelectCacheForSharedWorker(int host_id,
122 int64_t appcache_id) { 186 int64_t appcache_id) {
123 if (appcache_service_.get()) { 187 if (appcache_service_.get()) {
124 if (!backend_impl_.SelectCacheForSharedWorker(host_id, appcache_id)) 188 if (!backend_impl_->SelectCacheForSharedWorker(host_id, appcache_id))
125 bad_message::ReceivedBadMessage( 189 bad_message::ReceivedBadMessage(
126 this, bad_message::ACDH_SELECT_CACHE_FOR_SHARED_WORKER); 190 this, bad_message::ACDH_SELECT_CACHE_FOR_SHARED_WORKER);
127 } else { 191 } else {
128 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); 192 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo());
129 } 193 }
130 } 194 }
131 195
132 void AppCacheDispatcherHost::OnMarkAsForeignEntry( 196 void AppCacheDispatcherHost::OnMarkAsForeignEntry(
133 int host_id, 197 int host_id,
134 const GURL& document_url, 198 const GURL& document_url,
135 int64_t cache_document_was_loaded_from) { 199 int64_t cache_document_was_loaded_from) {
136 if (appcache_service_.get()) { 200 if (appcache_service_.get()) {
137 if (!backend_impl_.MarkAsForeignEntry( 201 if (!backend_impl_->MarkAsForeignEntry(host_id, document_url,
138 host_id, document_url, cache_document_was_loaded_from)) { 202 cache_document_was_loaded_from)) {
139 bad_message::ReceivedBadMessage(this, 203 bad_message::ReceivedBadMessage(this,
140 bad_message::ACDH_MARK_AS_FOREIGN_ENTRY); 204 bad_message::ACDH_MARK_AS_FOREIGN_ENTRY);
141 } 205 }
142 } 206 }
143 } 207 }
144 208
145 void AppCacheDispatcherHost::OnGetResourceList( 209 void AppCacheDispatcherHost::OnGetResourceList(
146 int host_id, std::vector<AppCacheResourceInfo>* params) { 210 int host_id, std::vector<AppCacheResourceInfo>* params) {
147 if (appcache_service_.get()) 211 if (appcache_service_.get())
148 backend_impl_.GetResourceList(host_id, params); 212 backend_impl_->GetResourceList(host_id, params);
149 } 213 }
150 214
151 void AppCacheDispatcherHost::OnGetStatus(int host_id, IPC::Message* reply_msg) { 215 void AppCacheDispatcherHost::OnGetStatus(int host_id, IPC::Message* reply_msg) {
152 if (pending_reply_msg_) { 216 if (pending_reply_msg_) {
153 bad_message::ReceivedBadMessage( 217 bad_message::ReceivedBadMessage(
154 this, bad_message::ACDH_PENDING_REPLY_IN_GET_STATUS); 218 this, bad_message::ACDH_PENDING_REPLY_IN_GET_STATUS);
155 delete reply_msg; 219 delete reply_msg;
156 return; 220 return;
157 } 221 }
158 222
159 pending_reply_msg_.reset(reply_msg); 223 pending_reply_msg_.reset(reply_msg);
160 if (appcache_service_.get()) { 224 if (appcache_service_.get()) {
161 if (!backend_impl_.GetStatusWithCallback( 225 if (!backend_impl_->GetStatusWithCallback(host_id, get_status_callback_,
162 host_id, get_status_callback_, reply_msg)) { 226 reply_msg)) {
163 bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS); 227 bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS);
164 } 228 }
165 return; 229 return;
166 } 230 }
167 231
168 GetStatusCallback(APPCACHE_STATUS_UNCACHED, reply_msg); 232 GetStatusCallback(APPCACHE_STATUS_UNCACHED, reply_msg);
169 } 233 }
170 234
171 void AppCacheDispatcherHost::OnStartUpdate(int host_id, 235 void AppCacheDispatcherHost::OnStartUpdate(int host_id,
172 IPC::Message* reply_msg) { 236 IPC::Message* reply_msg) {
173 if (pending_reply_msg_) { 237 if (pending_reply_msg_) {
174 bad_message::ReceivedBadMessage( 238 bad_message::ReceivedBadMessage(
175 this, bad_message::ACDH_PENDING_REPLY_IN_START_UPDATE); 239 this, bad_message::ACDH_PENDING_REPLY_IN_START_UPDATE);
176 delete reply_msg; 240 delete reply_msg;
177 return; 241 return;
178 } 242 }
179 243
180 pending_reply_msg_.reset(reply_msg); 244 pending_reply_msg_.reset(reply_msg);
181 if (appcache_service_.get()) { 245 if (appcache_service_.get()) {
182 if (!backend_impl_.StartUpdateWithCallback( 246 if (!backend_impl_->StartUpdateWithCallback(host_id, start_update_callback_,
183 host_id, start_update_callback_, reply_msg)) { 247 reply_msg)) {
184 bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE); 248 bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE);
185 } 249 }
186 return; 250 return;
187 } 251 }
188 252
189 StartUpdateCallback(false, reply_msg); 253 StartUpdateCallback(false, reply_msg);
190 } 254 }
191 255
192 void AppCacheDispatcherHost::OnSwapCache(int host_id, IPC::Message* reply_msg) { 256 void AppCacheDispatcherHost::OnSwapCache(int host_id, IPC::Message* reply_msg) {
193 if (pending_reply_msg_) { 257 if (pending_reply_msg_) {
194 bad_message::ReceivedBadMessage( 258 bad_message::ReceivedBadMessage(
195 this, bad_message::ACDH_PENDING_REPLY_IN_SWAP_CACHE); 259 this, bad_message::ACDH_PENDING_REPLY_IN_SWAP_CACHE);
196 delete reply_msg; 260 delete reply_msg;
197 return; 261 return;
198 } 262 }
199 263
200 pending_reply_msg_.reset(reply_msg); 264 pending_reply_msg_.reset(reply_msg);
201 if (appcache_service_.get()) { 265 if (appcache_service_.get()) {
202 if (!backend_impl_.SwapCacheWithCallback( 266 if (!backend_impl_->SwapCacheWithCallback(host_id, swap_cache_callback_,
203 host_id, swap_cache_callback_, reply_msg)) { 267 reply_msg)) {
204 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE); 268 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE);
205 } 269 }
206 return; 270 return;
207 } 271 }
208 272
209 SwapCacheCallback(false, reply_msg); 273 SwapCacheCallback(false, reply_msg);
210 } 274 }
211 275
212 void AppCacheDispatcherHost::GetStatusCallback( 276 void AppCacheDispatcherHost::GetStatusCallback(
213 AppCacheStatus status, void* param) { 277 AppCacheStatus status, void* param) {
(...skipping 10 matching lines...) Expand all
224 Send(pending_reply_msg_.release()); 288 Send(pending_reply_msg_.release());
225 } 289 }
226 290
227 void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) { 291 void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) {
228 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); 292 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param);
229 DCHECK_EQ(pending_reply_msg_.get(), reply_msg); 293 DCHECK_EQ(pending_reply_msg_.get(), reply_msg);
230 AppCacheHostMsg_SwapCache::WriteReplyParams(reply_msg, result); 294 AppCacheHostMsg_SwapCache::WriteReplyParams(reply_msg, result);
231 Send(pending_reply_msg_.release()); 295 Send(pending_reply_msg_.release());
232 } 296 }
233 297
298 // static
299 scoped_refptr<AppCacheDispatcherHost> AppCacheDispatcherHost::GetHostForProcess(
300 int process_id) {
301 ProcessIdToHostMap::iterator index =
302 g_process_id_host_map.Get().find(process_id);
303 if (index == g_process_id_host_map.Get().end())
304 return scoped_refptr<AppCacheDispatcherHost>();
305 return index->second;
306 }
307
234 } // namespace content 308 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698