Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/service_worker/service_worker_client_utils.h" | 5 #include "content/browser/service_worker/service_worker_client_utils.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "content/browser/frame_host/frame_tree_node.h" | 10 #include "content/browser/frame_host/frame_tree_node.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 void AddNonWindowClient(ServiceWorkerProviderHost* host, | 218 void AddNonWindowClient(ServiceWorkerProviderHost* host, |
| 219 const ServiceWorkerClientQueryOptions& options, | 219 const ServiceWorkerClientQueryOptions& options, |
| 220 ServiceWorkerClients* clients) { | 220 ServiceWorkerClients* clients) { |
| 221 blink::WebServiceWorkerClientType host_client_type = host->client_type(); | 221 blink::WebServiceWorkerClientType host_client_type = host->client_type(); |
| 222 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) | 222 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) |
| 223 return; | 223 return; |
| 224 if (options.client_type != blink::WebServiceWorkerClientTypeAll && | 224 if (options.client_type != blink::WebServiceWorkerClientTypeAll && |
| 225 options.client_type != host_client_type) | 225 options.client_type != host_client_type) |
| 226 return; | 226 return; |
| 227 | 227 |
| 228 ServiceWorkerClientInfo client_info(blink::WebPageVisibilityStateHidden, | 228 ServiceWorkerClientInfo client_info( |
| 229 false, // is_focused | 229 host->client_uuid(), blink::WebPageVisibilityStateHidden, |
| 230 host->document_url(), | 230 false, // is_focused |
| 231 REQUEST_CONTEXT_FRAME_TYPE_NONE, | 231 host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, base::TimeTicks(), |
| 232 base::TimeTicks(), host_client_type); | 232 host_client_type); |
| 233 client_info.client_uuid = host->client_uuid(); | 233 client_info.client_uuid = host->client_uuid(); |
|
nhiroki
2016/02/03 09:39:59
It looks like |client_info.client_uuid| is already
jungkees
2016/02/03 14:15:07
Ah.. yes. I forgot to remove this after changing t
| |
| 234 clients->push_back(client_info); | 234 clients->push_back(client_info); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void OnGetWindowClientsOnUI( | 237 void OnGetWindowClientsOnUI( |
| 238 // The tuple contains process_id, frame_id, client_uuid. | 238 // The tuple contains process_id, frame_id, client_uuid. |
| 239 const std::vector<base::Tuple<int, int, std::string>>& clients_info, | 239 const std::vector<base::Tuple<int, int, std::string>>& clients_info, |
| 240 const GURL& script_url, | 240 const GURL& script_url, |
| 241 const GetWindowClientsCallback& callback) { | 241 const GetWindowClientsCallback& callback) { |
| 242 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); | 242 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); |
| 243 | 243 |
| 244 for (const auto& it : clients_info) { | 244 for (const auto& it : clients_info) { |
| 245 ServiceWorkerClientInfo info = | 245 ServiceWorkerClientInfo info = |
| 246 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(base::get<0>(it), | 246 ServiceWorkerProviderHost::GetWindowClientInfoOnUI( |
| 247 base::get<1>(it)); | 247 base::get<0>(it), base::get<1>(it), base::get<2>(it)); |
| 248 | 248 |
| 249 // If the request to the provider_host returned an empty | 249 // If the request to the provider_host returned an empty |
| 250 // ServiceWorkerClientInfo, that means that it wasn't possible to associate | 250 // ServiceWorkerClientInfo, that means that it wasn't possible to associate |
| 251 // it with a valid RenderFrameHost. It might be because the frame was killed | 251 // it with a valid RenderFrameHost. It might be because the frame was killed |
| 252 // or navigated in between. | 252 // or navigated in between. |
| 253 if (info.IsEmpty()) | 253 if (info.IsEmpty()) |
| 254 continue; | 254 continue; |
| 255 | 255 |
| 256 // We can get info for a frame that was navigating end ended up with a | 256 // We can get info for a frame that was navigating end ended up with a |
| 257 // different URL than expected. In such case, we should make sure to not | 257 // different URL than expected. In such case, we should make sure to not |
| 258 // expose cross-origin WindowClient. | 258 // expose cross-origin WindowClient. |
| 259 if (info.url.GetOrigin() != script_url.GetOrigin()) | 259 if (info.url.GetOrigin() != script_url.GetOrigin()) |
| 260 continue; | 260 continue; |
| 261 | 261 |
| 262 info.client_uuid = base::get<2>(it); | 262 info.client_uuid = base::get<2>(it); |
|
nhiroki
2016/02/03 09:39:59
ditto: |client_uuid| would be already set.
jungkees
2016/02/03 14:15:07
Done.
| |
| 263 clients->push_back(info); | 263 clients->push_back(info); |
| 264 } | 264 } |
| 265 | 265 |
| 266 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 266 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 267 base::Bind(callback, base::Passed(&clients))); | 267 base::Bind(callback, base::Passed(&clients))); |
| 268 } | 268 } |
| 269 | 269 |
| 270 struct ServiceWorkerClientInfoSortMRU { | 270 struct ServiceWorkerClientInfoSortMRU { |
| 271 bool operator()(const ServiceWorkerClientInfo& a, | 271 bool operator()(const ServiceWorkerClientInfo& a, |
| 272 const ServiceWorkerClientInfo& b) const { | 272 const ServiceWorkerClientInfo& b) const { |
| 273 return a.last_focus_time > b.last_focus_time; | 273 return a.last_focus_time > b.last_focus_time; |
| 274 } | 274 } |
| 275 }; | 275 }; |
| 276 | 276 |
| 277 void DidGetClient(const ClientCallback& callback, | |
| 278 const ServiceWorkerClientInfo& client_info) { | |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 280 | |
| 281 callback.Run(client_info); | |
| 282 } | |
| 283 | |
| 277 void DidGetClients(const ClientsCallback& callback, | 284 void DidGetClients(const ClientsCallback& callback, |
| 278 ServiceWorkerClients* clients) { | 285 ServiceWorkerClients* clients) { |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 280 | 287 |
| 281 // Sort clients so that the most recently active tab is in the front. | 288 // Sort clients so that the most recently active tab is in the front. |
| 282 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); | 289 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); |
| 283 | 290 |
| 284 callback.Run(clients); | 291 callback.Run(clients); |
| 285 } | 292 } |
| 286 | 293 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 const base::WeakPtr<ServiceWorkerContextCore>& context, | 371 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 365 const NavigationCallback& callback) { | 372 const NavigationCallback& callback) { |
| 366 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 373 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 367 BrowserThread::PostTask( | 374 BrowserThread::PostTask( |
| 368 BrowserThread::UI, FROM_HERE, | 375 BrowserThread::UI, FROM_HERE, |
| 369 base::Bind( | 376 base::Bind( |
| 370 &NavigateClientOnUI, url, script_url, process_id, frame_id, | 377 &NavigateClientOnUI, url, script_url, process_id, frame_id, |
| 371 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback))); | 378 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback))); |
| 372 } | 379 } |
| 373 | 380 |
| 381 void GetClient(const base::WeakPtr<ServiceWorkerVersion>& controller, | |
| 382 const std::string& id, | |
| 383 const base::WeakPtr<ServiceWorkerContextCore>& context, | |
| 384 const ClientCallback& callback) { | |
| 385 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 386 | |
| 387 ServiceWorkerProviderHost* provider_host = | |
| 388 context->GetProviderHostByClientID(id); | |
| 389 | |
| 390 ServiceWorkerClientInfo client_info; | |
| 391 if (!provider_host) { | |
| 392 // The client may already have been closed, just ignore. | |
| 393 DidGetClient(callback, client_info); | |
| 394 return; | |
| 395 } | |
| 396 | |
| 397 if (provider_host->client_type() == blink::WebServiceWorkerClientTypeWindow) { | |
| 398 provider_host->GetWindowClientInfo(callback); | |
|
nhiroki
2016/02/03 09:39:59
"base::Bind(&DidGetClient(callback))" would be con
jungkees
2016/02/03 14:15:07
Right. Addressed.
Out of my own curiosity, which
nhiroki
2016/02/16 06:09:31
Sorry, I missed your reply and miswrote the code s
jungkees
2016/02/16 16:28:58
Right. Good point. Addressed.
| |
| 399 return; | |
| 400 } | |
| 401 | |
| 402 client_info.client_uuid = provider_host->client_uuid(); | |
| 403 client_info.url = provider_host->document_url(); | |
| 404 client_info.page_visibility_state = blink::WebPageVisibilityStateHidden; | |
| 405 client_info.is_focused = false; | |
| 406 client_info.frame_type = REQUEST_CONTEXT_FRAME_TYPE_NONE; | |
| 407 client_info.client_type = provider_host->client_type(); | |
| 408 client_info.last_focus_time = base::TimeTicks(); | |
|
nhiroki
2016/02/03 09:39:59
You may want to call the ctor instead of assigning
jungkees
2016/02/03 14:15:07
Done.
| |
| 409 DidGetClient(callback, client_info); | |
| 410 } | |
| 411 | |
| 374 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller, | 412 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller, |
| 375 const ServiceWorkerClientQueryOptions& options, | 413 const ServiceWorkerClientQueryOptions& options, |
| 376 const ClientsCallback& callback) { | 414 const ClientsCallback& callback) { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 415 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 378 | 416 |
| 379 ServiceWorkerClients clients; | 417 ServiceWorkerClients clients; |
| 380 if (!controller->HasControllee() && !options.include_uncontrolled) { | 418 if (!controller->HasControllee() && !options.include_uncontrolled) { |
| 381 DidGetClients(callback, &clients); | 419 DidGetClients(callback, &clients); |
| 382 return; | 420 return; |
| 383 } | 421 } |
| 384 | 422 |
| 385 // For Window clients we want to query the info on the UI thread first. | 423 // For Window clients we want to query the info on the UI thread first. |
| 386 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || | 424 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || |
| 387 options.client_type == blink::WebServiceWorkerClientTypeAll) { | 425 options.client_type == blink::WebServiceWorkerClientTypeAll) { |
| 388 GetWindowClients(controller, options, callback); | 426 GetWindowClients(controller, options, callback); |
| 389 return; | 427 return; |
| 390 } | 428 } |
| 391 | 429 |
| 392 GetNonWindowClients(controller, options, &clients); | 430 GetNonWindowClients(controller, options, &clients); |
| 393 DidGetClients(callback, &clients); | 431 DidGetClients(callback, &clients); |
| 394 } | 432 } |
| 395 | 433 |
| 396 } // namespace service_worker_client_utils | 434 } // namespace service_worker_client_utils |
| 397 } // namespace content | 435 } // namespace content |
| OLD | NEW |