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(); | |
| 234 clients->push_back(client_info); | 233 clients->push_back(client_info); |
| 235 } | 234 } |
| 236 | 235 |
| 237 void OnGetWindowClientsOnUI( | 236 void OnGetWindowClientsOnUI( |
| 238 // The tuple contains process_id, frame_id, client_uuid. | 237 // The tuple contains process_id, frame_id, client_uuid. |
| 239 const std::vector<base::Tuple<int, int, std::string>>& clients_info, | 238 const std::vector<base::Tuple<int, int, std::string>>& clients_info, |
| 240 const GURL& script_url, | 239 const GURL& script_url, |
| 241 const GetWindowClientsCallback& callback) { | 240 const GetWindowClientsCallback& callback) { |
| 242 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); | 241 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); |
| 243 | 242 |
| 244 for (const auto& it : clients_info) { | 243 for (const auto& it : clients_info) { |
| 245 ServiceWorkerClientInfo info = | 244 ServiceWorkerClientInfo info = |
| 246 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(base::get<0>(it), | 245 ServiceWorkerProviderHost::GetWindowClientInfoOnUI( |
| 247 base::get<1>(it)); | 246 base::get<0>(it), base::get<1>(it), base::get<2>(it)); |
| 248 | 247 |
| 249 // If the request to the provider_host returned an empty | 248 // If the request to the provider_host returned an empty |
| 250 // ServiceWorkerClientInfo, that means that it wasn't possible to associate | 249 // ServiceWorkerClientInfo, that means that it wasn't possible to associate |
| 251 // it with a valid RenderFrameHost. It might be because the frame was killed | 250 // it with a valid RenderFrameHost. It might be because the frame was killed |
| 252 // or navigated in between. | 251 // or navigated in between. |
| 253 if (info.IsEmpty()) | 252 if (info.IsEmpty()) |
| 254 continue; | 253 continue; |
| 255 | 254 |
| 256 // We can get info for a frame that was navigating end ended up with a | 255 // 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 | 256 // different URL than expected. In such case, we should make sure to not |
| 258 // expose cross-origin WindowClient. | 257 // expose cross-origin WindowClient. |
| 259 if (info.url.GetOrigin() != script_url.GetOrigin()) | 258 if (info.url.GetOrigin() != script_url.GetOrigin()) |
| 260 continue; | 259 continue; |
| 261 | 260 |
| 262 info.client_uuid = base::get<2>(it); | |
| 263 clients->push_back(info); | 261 clients->push_back(info); |
| 264 } | 262 } |
| 265 | 263 |
| 266 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 264 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 267 base::Bind(callback, base::Passed(&clients))); | 265 base::Bind(callback, base::Passed(&clients))); |
| 268 } | 266 } |
| 269 | 267 |
| 270 struct ServiceWorkerClientInfoSortMRU { | 268 struct ServiceWorkerClientInfoSortMRU { |
| 271 bool operator()(const ServiceWorkerClientInfo& a, | 269 bool operator()(const ServiceWorkerClientInfo& a, |
| 272 const ServiceWorkerClientInfo& b) const { | 270 const ServiceWorkerClientInfo& b) const { |
| 273 return a.last_focus_time > b.last_focus_time; | 271 return a.last_focus_time > b.last_focus_time; |
| 274 } | 272 } |
| 275 }; | 273 }; |
| 276 | 274 |
| 275 void DidGetClient(const ClientCallback& callback, | |
| 276 const ServiceWorkerClientInfo& client_info) { | |
| 277 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 278 | |
| 279 callback.Run(client_info); | |
| 280 } | |
| 281 | |
| 277 void DidGetClients(const ClientsCallback& callback, | 282 void DidGetClients(const ClientsCallback& callback, |
| 278 ServiceWorkerClients* clients) { | 283 ServiceWorkerClients* clients) { |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 284 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 280 | 285 |
| 281 // Sort clients so that the most recently active tab is in the front. | 286 // Sort clients so that the most recently active tab is in the front. |
| 282 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); | 287 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); |
| 283 | 288 |
| 284 callback.Run(clients); | 289 callback.Run(clients); |
| 285 } | 290 } |
| 286 | 291 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 const base::WeakPtr<ServiceWorkerContextCore>& context, | 369 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 365 const NavigationCallback& callback) { | 370 const NavigationCallback& callback) { |
| 366 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 371 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 367 BrowserThread::PostTask( | 372 BrowserThread::PostTask( |
| 368 BrowserThread::UI, FROM_HERE, | 373 BrowserThread::UI, FROM_HERE, |
| 369 base::Bind( | 374 base::Bind( |
| 370 &NavigateClientOnUI, url, script_url, process_id, frame_id, | 375 &NavigateClientOnUI, url, script_url, process_id, frame_id, |
| 371 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback))); | 376 base::Bind(&DidNavigate, context, script_url.GetOrigin(), callback))); |
| 372 } | 377 } |
| 373 | 378 |
| 379 void GetClient(const base::WeakPtr<ServiceWorkerVersion>& controller, | |
| 380 const std::string& client_uuid, | |
| 381 const base::WeakPtr<ServiceWorkerContextCore>& context, | |
| 382 const ClientCallback& callback) { | |
| 383 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 384 | |
| 385 ServiceWorkerProviderHost* provider_host = | |
| 386 context->GetProviderHostByClientID(client_uuid); | |
| 387 | |
| 388 if (!provider_host) { | |
| 389 // The client may already have been closed, just ignore. | |
| 390 DidGetClient(callback, ServiceWorkerClientInfo()); | |
| 391 return; | |
| 392 } | |
| 393 | |
| 394 if (provider_host->client_type() == blink::WebServiceWorkerClientTypeWindow) { | |
| 395 provider_host->GetWindowClientInfo(base::Bind(&DidGetClient, callback)); | |
|
zino
2016/02/05 06:21:44
If you use GetClientInfoCallback, you can pass cal
jungkees
2016/02/12 15:03:21
Done.
| |
| 396 return; | |
| 397 } | |
| 398 | |
| 399 ServiceWorkerClientInfo client_info( | |
| 400 provider_host->client_uuid(), blink::WebPageVisibilityStateHidden, | |
| 401 false, // is_focused | |
| 402 provider_host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, | |
| 403 base::TimeTicks(), provider_host->client_type()); | |
| 404 DidGetClient(callback, client_info); | |
| 405 } | |
| 406 | |
| 374 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller, | 407 void GetClients(const base::WeakPtr<ServiceWorkerVersion>& controller, |
| 375 const ServiceWorkerClientQueryOptions& options, | 408 const ServiceWorkerClientQueryOptions& options, |
| 376 const ClientsCallback& callback) { | 409 const ClientsCallback& callback) { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 410 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 378 | 411 |
| 379 ServiceWorkerClients clients; | 412 ServiceWorkerClients clients; |
| 380 if (!controller->HasControllee() && !options.include_uncontrolled) { | 413 if (!controller->HasControllee() && !options.include_uncontrolled) { |
| 381 DidGetClients(callback, &clients); | 414 DidGetClients(callback, &clients); |
| 382 return; | 415 return; |
| 383 } | 416 } |
| 384 | 417 |
| 385 // For Window clients we want to query the info on the UI thread first. | 418 // For Window clients we want to query the info on the UI thread first. |
| 386 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || | 419 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || |
| 387 options.client_type == blink::WebServiceWorkerClientTypeAll) { | 420 options.client_type == blink::WebServiceWorkerClientTypeAll) { |
| 388 GetWindowClients(controller, options, callback); | 421 GetWindowClients(controller, options, callback); |
| 389 return; | 422 return; |
| 390 } | 423 } |
| 391 | 424 |
| 392 GetNonWindowClients(controller, options, &clients); | 425 GetNonWindowClients(controller, options, &clients); |
| 393 DidGetClients(callback, &clients); | 426 DidGetClients(callback, &clients); |
| 394 } | 427 } |
| 395 | 428 |
| 396 } // namespace service_worker_client_utils | 429 } // namespace service_worker_client_utils |
| 397 } // namespace content | 430 } // namespace content |
| OLD | NEW |