OLD | NEW |
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()) |
28 backend_impl_.Initialize( | 39 return; |
29 appcache_service_.get(), &frontend_proxy_, process_id_); | 40 |
30 get_status_callback_ = | 41 backend_impl_.Initialize(appcache_service_.get(), &frontend_proxy_, |
31 base::Bind(&AppCacheDispatcherHost::GetStatusCallback, | 42 process_id_); |
32 weak_factory_.GetWeakPtr()); | 43 get_status_callback_ = base::Bind(&AppCacheDispatcherHost::GetStatusCallback, |
33 start_update_callback_ = | 44 weak_factory_.GetWeakPtr()); |
34 base::Bind(&AppCacheDispatcherHost::StartUpdateCallback, | 45 start_update_callback_ = base::Bind( |
35 weak_factory_.GetWeakPtr()); | 46 &AppCacheDispatcherHost::StartUpdateCallback, weak_factory_.GetWeakPtr()); |
36 swap_cache_callback_ = | 47 swap_cache_callback_ = base::Bind(&AppCacheDispatcherHost::SwapCacheCallback, |
37 base::Bind(&AppCacheDispatcherHost::SwapCacheCallback, | 48 weak_factory_.GetWeakPtr()); |
38 weak_factory_.GetWeakPtr()); | |
39 } | |
40 } | 49 } |
41 | 50 |
42 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 51 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
43 bool handled = true; | 52 bool handled = true; |
44 IPC_BEGIN_MESSAGE_MAP(AppCacheDispatcherHost, message) | 53 IPC_BEGIN_MESSAGE_MAP(AppCacheDispatcherHost, message) |
45 IPC_MESSAGE_HANDLER(AppCacheHostMsg_RegisterHost, OnRegisterHost) | 54 IPC_MESSAGE_HANDLER(AppCacheHostMsg_RegisterHost, OnRegisterHost) |
46 IPC_MESSAGE_HANDLER(AppCacheHostMsg_UnregisterHost, OnUnregisterHost) | 55 IPC_MESSAGE_HANDLER(AppCacheHostMsg_UnregisterHost, OnUnregisterHost) |
47 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SetSpawningHostId, OnSetSpawningHostId) | 56 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SetSpawningHostId, OnSetSpawningHostId) |
48 IPC_MESSAGE_HANDLER(AppCacheHostMsg_GetResourceList, OnGetResourceList) | 57 IPC_MESSAGE_HANDLER(AppCacheHostMsg_GetResourceList, OnGetResourceList) |
49 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCache, OnSelectCache) | 58 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCache, OnSelectCache) |
50 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForWorker, | 59 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForWorker, |
51 OnSelectCacheForWorker) | 60 OnSelectCacheForWorker) |
52 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForSharedWorker, | 61 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForSharedWorker, |
53 OnSelectCacheForSharedWorker) | 62 OnSelectCacheForSharedWorker) |
54 IPC_MESSAGE_HANDLER(AppCacheHostMsg_MarkAsForeignEntry, | 63 IPC_MESSAGE_HANDLER(AppCacheHostMsg_MarkAsForeignEntry, |
55 OnMarkAsForeignEntry) | 64 OnMarkAsForeignEntry) |
56 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_GetStatus, OnGetStatus) | 65 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_GetStatus, OnGetStatus) |
57 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_StartUpdate, OnStartUpdate) | 66 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_StartUpdate, OnStartUpdate) |
58 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_SwapCache, OnSwapCache) | 67 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_SwapCache, OnSwapCache) |
59 IPC_MESSAGE_UNHANDLED(handled = false) | 68 IPC_MESSAGE_UNHANDLED(handled = false) |
60 IPC_END_MESSAGE_MAP() | 69 IPC_END_MESSAGE_MAP() |
61 | 70 |
62 return handled; | 71 return handled; |
63 } | 72 } |
64 | 73 |
65 AppCacheDispatcherHost::~AppCacheDispatcherHost() {} | 74 void AppCacheDispatcherHost::RegisterPendingHost(int host_id) { |
| 75 DCHECK(IsBrowserSideNavigationEnabled()); |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 77 DCHECK(host_id != kAppCacheNoHostId); |
| 78 DCHECK(pending_hosts_.find(host_id) == pending_hosts_.end()); |
| 79 pending_hosts_.insert(host_id); |
| 80 } |
| 81 |
| 82 AppCacheDispatcherHost::~AppCacheDispatcherHost() { |
| 83 ProcessIdToHostMap::iterator index = |
| 84 g_process_id_host_map.Get().find(process_id_); |
| 85 if ((index != g_process_id_host_map.Get().end()) && index->second == this) |
| 86 g_process_id_host_map.Get().erase(index); |
| 87 } |
| 88 |
| 89 void AppCacheDispatcherHost::RegisterPrecreatedHost( |
| 90 std::unique_ptr<AppCacheHost> host) { |
| 91 DCHECK(host.get()); |
| 92 DCHECK(IsBrowserSideNavigationEnabled()); |
| 93 |
| 94 // Before switching the backends we mark the host as pending so we can avoid |
| 95 // registering them again in the AppCacheHostMsg_RegisterHost IPC. |
| 96 RegisterPendingHost(host->host_id()); |
| 97 host->set_frontend(&frontend_proxy_); |
| 98 backend_impl_.RegisterPrecreatedHost(std::move(host)); |
| 99 } |
66 | 100 |
67 void AppCacheDispatcherHost::OnRegisterHost(int host_id) { | 101 void AppCacheDispatcherHost::OnRegisterHost(int host_id) { |
68 if (appcache_service_.get()) { | 102 if (appcache_service_.get()) { |
| 103 // PlzNavigate. If the |host_id| is the pending_backends_ list, it means |
| 104 // it was registered via the RegisterPendingHost() function. We remove the |
| 105 // host from the pending list and return without registering the host. |
| 106 if (IsBrowserSideNavigationEnabled()) { |
| 107 auto found = pending_hosts_.find(host_id); |
| 108 if (found != pending_hosts_.end()) { |
| 109 pending_hosts_.erase(found); |
| 110 return; |
| 111 } |
| 112 } |
69 if (!backend_impl_.RegisterHost(host_id)) { | 113 if (!backend_impl_.RegisterHost(host_id)) { |
70 bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER); | 114 bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER); |
71 } | 115 } |
72 } | 116 } |
73 } | 117 } |
74 | 118 |
75 void AppCacheDispatcherHost::OnUnregisterHost(int host_id) { | 119 void AppCacheDispatcherHost::OnUnregisterHost(int host_id) { |
76 if (appcache_service_.get()) { | 120 if (appcache_service_.get()) { |
77 if (!backend_impl_.UnregisterHost(host_id)) { | 121 if (!backend_impl_.UnregisterHost(host_id)) { |
78 bad_message::ReceivedBadMessage(this, bad_message::ACDH_UNREGISTER); | 122 bad_message::ReceivedBadMessage(this, bad_message::ACDH_UNREGISTER); |
79 } | 123 } |
80 } | 124 } |
81 } | 125 } |
82 | 126 |
83 void AppCacheDispatcherHost::OnSetSpawningHostId( | 127 void AppCacheDispatcherHost::OnSetSpawningHostId( |
84 int host_id, int spawning_host_id) { | 128 int host_id, int spawning_host_id) { |
85 if (appcache_service_.get()) { | 129 if (appcache_service_.get()) { |
86 if (!backend_impl_.SetSpawningHostId(host_id, spawning_host_id)) | 130 if (!backend_impl_.SetSpawningHostId(host_id, spawning_host_id)) |
87 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SET_SPAWNING); | 131 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SET_SPAWNING); |
88 } | 132 } |
89 } | 133 } |
90 | 134 |
91 void AppCacheDispatcherHost::OnSelectCache( | 135 void AppCacheDispatcherHost::OnSelectCache( |
92 int host_id, | 136 int host_id, |
93 const GURL& document_url, | 137 const GURL& document_url, |
94 int64_t cache_document_was_loaded_from, | 138 int64_t cache_document_was_loaded_from, |
95 const GURL& opt_manifest_url) { | 139 const GURL& opt_manifest_url) { |
96 if (appcache_service_.get()) { | 140 if (appcache_service_.get()) { |
97 if (!backend_impl_.SelectCache(host_id, | 141 if (!backend_impl_.SelectCache(host_id, document_url, |
98 document_url, | |
99 cache_document_was_loaded_from, | 142 cache_document_was_loaded_from, |
100 opt_manifest_url)) { | 143 opt_manifest_url)) { |
101 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE); | 144 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE); |
102 } | 145 } |
103 } else { | 146 } else { |
104 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); | 147 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); |
105 } | 148 } |
106 } | 149 } |
107 | 150 |
108 void AppCacheDispatcherHost::OnSelectCacheForWorker( | 151 void AppCacheDispatcherHost::OnSelectCacheForWorker( |
109 int host_id, int parent_process_id, int parent_host_id) { | 152 int host_id, int parent_process_id, int parent_host_id) { |
110 if (appcache_service_.get()) { | 153 if (appcache_service_.get()) { |
111 if (!backend_impl_.SelectCacheForWorker( | 154 if (!backend_impl_.SelectCacheForWorker(host_id, parent_process_id, |
112 host_id, parent_process_id, parent_host_id)) { | 155 parent_host_id)) { |
113 bad_message::ReceivedBadMessage( | 156 bad_message::ReceivedBadMessage( |
114 this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER); | 157 this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER); |
115 } | 158 } |
116 } else { | 159 } else { |
117 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); | 160 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); |
118 } | 161 } |
119 } | 162 } |
120 | 163 |
121 void AppCacheDispatcherHost::OnSelectCacheForSharedWorker(int host_id, | 164 void AppCacheDispatcherHost::OnSelectCacheForSharedWorker(int host_id, |
122 int64_t appcache_id) { | 165 int64_t appcache_id) { |
123 if (appcache_service_.get()) { | 166 if (appcache_service_.get()) { |
124 if (!backend_impl_.SelectCacheForSharedWorker(host_id, appcache_id)) | 167 if (!backend_impl_.SelectCacheForSharedWorker(host_id, appcache_id)) |
125 bad_message::ReceivedBadMessage( | 168 bad_message::ReceivedBadMessage( |
126 this, bad_message::ACDH_SELECT_CACHE_FOR_SHARED_WORKER); | 169 this, bad_message::ACDH_SELECT_CACHE_FOR_SHARED_WORKER); |
127 } else { | 170 } else { |
128 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); | 171 frontend_proxy_.OnCacheSelected(host_id, AppCacheInfo()); |
129 } | 172 } |
130 } | 173 } |
131 | 174 |
132 void AppCacheDispatcherHost::OnMarkAsForeignEntry( | 175 void AppCacheDispatcherHost::OnMarkAsForeignEntry( |
133 int host_id, | 176 int host_id, |
134 const GURL& document_url, | 177 const GURL& document_url, |
135 int64_t cache_document_was_loaded_from) { | 178 int64_t cache_document_was_loaded_from) { |
136 if (appcache_service_.get()) { | 179 if (appcache_service_.get()) { |
137 if (!backend_impl_.MarkAsForeignEntry( | 180 if (!backend_impl_.MarkAsForeignEntry(host_id, document_url, |
138 host_id, document_url, cache_document_was_loaded_from)) { | 181 cache_document_was_loaded_from)) { |
139 bad_message::ReceivedBadMessage(this, | 182 bad_message::ReceivedBadMessage(this, |
140 bad_message::ACDH_MARK_AS_FOREIGN_ENTRY); | 183 bad_message::ACDH_MARK_AS_FOREIGN_ENTRY); |
141 } | 184 } |
142 } | 185 } |
143 } | 186 } |
144 | 187 |
145 void AppCacheDispatcherHost::OnGetResourceList( | 188 void AppCacheDispatcherHost::OnGetResourceList( |
146 int host_id, std::vector<AppCacheResourceInfo>* params) { | 189 int host_id, std::vector<AppCacheResourceInfo>* params) { |
147 if (appcache_service_.get()) | 190 if (appcache_service_.get()) |
148 backend_impl_.GetResourceList(host_id, params); | 191 backend_impl_.GetResourceList(host_id, params); |
149 } | 192 } |
150 | 193 |
151 void AppCacheDispatcherHost::OnGetStatus(int host_id, IPC::Message* reply_msg) { | 194 void AppCacheDispatcherHost::OnGetStatus(int host_id, IPC::Message* reply_msg) { |
152 if (pending_reply_msg_) { | 195 if (pending_reply_msg_) { |
153 bad_message::ReceivedBadMessage( | 196 bad_message::ReceivedBadMessage( |
154 this, bad_message::ACDH_PENDING_REPLY_IN_GET_STATUS); | 197 this, bad_message::ACDH_PENDING_REPLY_IN_GET_STATUS); |
155 delete reply_msg; | 198 delete reply_msg; |
156 return; | 199 return; |
157 } | 200 } |
158 | 201 |
159 pending_reply_msg_.reset(reply_msg); | 202 pending_reply_msg_.reset(reply_msg); |
160 if (appcache_service_.get()) { | 203 if (appcache_service_.get()) { |
161 if (!backend_impl_.GetStatusWithCallback( | 204 if (!backend_impl_.GetStatusWithCallback(host_id, get_status_callback_, |
162 host_id, get_status_callback_, reply_msg)) { | 205 reply_msg)) { |
163 bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS); | 206 bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS); |
164 } | 207 } |
165 return; | 208 return; |
166 } | 209 } |
167 | 210 |
168 GetStatusCallback(APPCACHE_STATUS_UNCACHED, reply_msg); | 211 GetStatusCallback(APPCACHE_STATUS_UNCACHED, reply_msg); |
169 } | 212 } |
170 | 213 |
171 void AppCacheDispatcherHost::OnStartUpdate(int host_id, | 214 void AppCacheDispatcherHost::OnStartUpdate(int host_id, |
172 IPC::Message* reply_msg) { | 215 IPC::Message* reply_msg) { |
173 if (pending_reply_msg_) { | 216 if (pending_reply_msg_) { |
174 bad_message::ReceivedBadMessage( | 217 bad_message::ReceivedBadMessage( |
175 this, bad_message::ACDH_PENDING_REPLY_IN_START_UPDATE); | 218 this, bad_message::ACDH_PENDING_REPLY_IN_START_UPDATE); |
176 delete reply_msg; | 219 delete reply_msg; |
177 return; | 220 return; |
178 } | 221 } |
179 | 222 |
180 pending_reply_msg_.reset(reply_msg); | 223 pending_reply_msg_.reset(reply_msg); |
181 if (appcache_service_.get()) { | 224 if (appcache_service_.get()) { |
182 if (!backend_impl_.StartUpdateWithCallback( | 225 if (!backend_impl_.StartUpdateWithCallback(host_id, start_update_callback_, |
183 host_id, start_update_callback_, reply_msg)) { | 226 reply_msg)) { |
184 bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE); | 227 bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE); |
185 } | 228 } |
186 return; | 229 return; |
187 } | 230 } |
188 | 231 |
189 StartUpdateCallback(false, reply_msg); | 232 StartUpdateCallback(false, reply_msg); |
190 } | 233 } |
191 | 234 |
192 void AppCacheDispatcherHost::OnSwapCache(int host_id, IPC::Message* reply_msg) { | 235 void AppCacheDispatcherHost::OnSwapCache(int host_id, IPC::Message* reply_msg) { |
193 if (pending_reply_msg_) { | 236 if (pending_reply_msg_) { |
194 bad_message::ReceivedBadMessage( | 237 bad_message::ReceivedBadMessage( |
195 this, bad_message::ACDH_PENDING_REPLY_IN_SWAP_CACHE); | 238 this, bad_message::ACDH_PENDING_REPLY_IN_SWAP_CACHE); |
196 delete reply_msg; | 239 delete reply_msg; |
197 return; | 240 return; |
198 } | 241 } |
199 | 242 |
200 pending_reply_msg_.reset(reply_msg); | 243 pending_reply_msg_.reset(reply_msg); |
201 if (appcache_service_.get()) { | 244 if (appcache_service_.get()) { |
202 if (!backend_impl_.SwapCacheWithCallback( | 245 if (!backend_impl_.SwapCacheWithCallback(host_id, swap_cache_callback_, |
203 host_id, swap_cache_callback_, reply_msg)) { | 246 reply_msg)) { |
204 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE); | 247 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE); |
205 } | 248 } |
206 return; | 249 return; |
207 } | 250 } |
208 | 251 |
209 SwapCacheCallback(false, reply_msg); | 252 SwapCacheCallback(false, reply_msg); |
210 } | 253 } |
211 | 254 |
212 void AppCacheDispatcherHost::GetStatusCallback( | 255 void AppCacheDispatcherHost::GetStatusCallback( |
213 AppCacheStatus status, void* param) { | 256 AppCacheStatus status, void* param) { |
(...skipping 10 matching lines...) Expand all Loading... |
224 Send(pending_reply_msg_.release()); | 267 Send(pending_reply_msg_.release()); |
225 } | 268 } |
226 | 269 |
227 void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) { | 270 void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) { |
228 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); | 271 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); |
229 DCHECK_EQ(pending_reply_msg_.get(), reply_msg); | 272 DCHECK_EQ(pending_reply_msg_.get(), reply_msg); |
230 AppCacheHostMsg_SwapCache::WriteReplyParams(reply_msg, result); | 273 AppCacheHostMsg_SwapCache::WriteReplyParams(reply_msg, result); |
231 Send(pending_reply_msg_.release()); | 274 Send(pending_reply_msg_.release()); |
232 } | 275 } |
233 | 276 |
| 277 // static |
| 278 scoped_refptr<AppCacheDispatcherHost> AppCacheDispatcherHost::GetHostForProcess( |
| 279 int process_id) { |
| 280 ProcessIdToHostMap::iterator index = |
| 281 g_process_id_host_map.Get().find(process_id); |
| 282 if (index == g_process_id_host_map.Get().end()) |
| 283 return scoped_refptr<AppCacheDispatcherHost>(); |
| 284 return index->second; |
| 285 } |
| 286 |
234 } // namespace content | 287 } // namespace content |
OLD | NEW |