OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 ServiceWorkerStatusCode status = | 989 ServiceWorkerStatusCode status = |
990 embedded_worker_->SendMessage(ServiceWorkerMsg_ActivateEvent(request_id)); | 990 embedded_worker_->SendMessage(ServiceWorkerMsg_ActivateEvent(request_id)); |
991 if (status != SERVICE_WORKER_OK) { | 991 if (status != SERVICE_WORKER_OK) { |
992 activate_callbacks_.Remove(request_id); | 992 activate_callbacks_.Remove(request_id); |
993 RunSoon(base::Bind(callback, status)); | 993 RunSoon(base::Bind(callback, status)); |
994 } | 994 } |
995 } | 995 } |
996 | 996 |
997 void ServiceWorkerVersion::OnGetClients( | 997 void ServiceWorkerVersion::OnGetClients( |
998 int request_id, | 998 int request_id, |
999 const ServiceWorkerClientQueryOptions& /* options */) { | 999 const ServiceWorkerClientQueryOptions& options) { |
1000 // TODO(kinuko): Handle ClientQueryOptions. (crbug.com/455241, 460415 etc) | 1000 if (controllee_map_.empty() && !options.include_uncontrolled) { |
1001 if (controllee_map_.empty()) { | |
1002 if (running_status() == RUNNING) { | 1001 if (running_status() == RUNNING) { |
1003 embedded_worker_->SendMessage( | 1002 embedded_worker_->SendMessage( |
1004 ServiceWorkerMsg_DidGetClients(request_id, | 1003 ServiceWorkerMsg_DidGetClients(request_id, |
1005 std::vector<ServiceWorkerClientInfo>())); | 1004 std::vector<ServiceWorkerClientInfo>())); |
1006 } | 1005 } |
1007 return; | 1006 return; |
1008 } | 1007 } |
1009 | 1008 |
1010 TRACE_EVENT0("ServiceWorker", | 1009 TRACE_EVENT0("ServiceWorker", |
1011 "ServiceWorkerVersion::OnGetClients"); | 1010 "ServiceWorkerVersion::OnGetClients"); |
1012 | 1011 |
| 1012 // 4.3.1 matchAll(options) |
1013 std::vector<Tuple<int,int,std::string>> clients_info; | 1013 std::vector<Tuple<int,int,std::string>> clients_info; |
1014 for (auto& controllee : controllee_map_) { | 1014 if (!options.include_uncontrolled) { |
1015 int process_id = controllee.second->process_id(); | 1015 for (auto& controllee : controllee_map_) { |
1016 int frame_id = controllee.second->frame_id(); | 1016 int process_id = controllee.second->process_id(); |
1017 const std::string& client_uuid = controllee.first; | 1017 int frame_id = controllee.second->frame_id(); |
1018 | 1018 const std::string& client_uuid = controllee.first; |
1019 clients_info.push_back(MakeTuple(process_id, frame_id, client_uuid)); | 1019 clients_info.push_back(MakeTuple(process_id, frame_id, client_uuid)); |
| 1020 } |
| 1021 } else { |
| 1022 for (auto it = |
| 1023 context_->GetClientProviderHostIterator(script_url_.GetOrigin()); |
| 1024 !it->IsAtEnd(); it->Advance()) { |
| 1025 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 1026 clients_info.push_back( |
| 1027 MakeTuple(host->process_id(), host->frame_id(), host->client_uuid())); |
| 1028 } |
1020 } | 1029 } |
1021 | 1030 |
1022 BrowserThread::PostTask( | 1031 BrowserThread::PostTask( |
1023 BrowserThread::UI, FROM_HERE, | 1032 BrowserThread::UI, FROM_HERE, |
1024 base::Bind(&OnGetClientsFromUI, clients_info, script_url_, | 1033 base::Bind(&OnGetClientsFromUI, clients_info, script_url_, |
1025 base::Bind(&ServiceWorkerVersion::DidGetClients, | 1034 base::Bind(&ServiceWorkerVersion::DidGetClients, |
1026 weak_factory_.GetWeakPtr(), | 1035 weak_factory_.GetWeakPtr(), |
1027 request_id))); | 1036 request_id))); |
1028 | 1037 |
1029 } | 1038 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1229 if (running_status() != RUNNING) | 1238 if (running_status() != RUNNING) |
1230 return; | 1239 return; |
1231 | 1240 |
1232 if (render_process_id == ChildProcessHost::kInvalidUniqueID && | 1241 if (render_process_id == ChildProcessHost::kInvalidUniqueID && |
1233 render_frame_id == MSG_ROUTING_NONE) { | 1242 render_frame_id == MSG_ROUTING_NONE) { |
1234 embedded_worker_->SendMessage(ServiceWorkerMsg_OpenWindowError( | 1243 embedded_worker_->SendMessage(ServiceWorkerMsg_OpenWindowError( |
1235 request_id, "Something went wrong while trying to open the window.")); | 1244 request_id, "Something went wrong while trying to open the window.")); |
1236 return; | 1245 return; |
1237 } | 1246 } |
1238 | 1247 |
1239 for (const auto& it : controllee_map_) { | 1248 for (auto it = |
1240 const ServiceWorkerProviderHost* provider_host = it.second; | 1249 context_->GetClientProviderHostIterator(script_url_.GetOrigin()); |
| 1250 !it->IsAtEnd(); it->Advance()) { |
| 1251 ServiceWorkerProviderHost* provider_host = it->GetProviderHost(); |
1241 if (provider_host->process_id() != render_process_id || | 1252 if (provider_host->process_id() != render_process_id || |
1242 provider_host->frame_id() != render_frame_id) { | 1253 provider_host->frame_id() != render_frame_id) { |
1243 continue; | 1254 continue; |
1244 } | 1255 } |
1245 | 1256 provider_host->GetClientInfo(base::Bind( |
1246 // it.second is the client_uuid associated with the provider_host. | 1257 &ServiceWorkerVersion::OnOpenWindowFinished, weak_factory_.GetWeakPtr(), |
1247 provider_host->GetClientInfo( | 1258 request_id, provider_host->client_uuid())); |
1248 base::Bind(&ServiceWorkerVersion::OnOpenWindowFinished, | |
1249 weak_factory_.GetWeakPtr(), request_id, it.first)); | |
1250 return; | 1259 return; |
1251 } | 1260 } |
1252 | 1261 |
1253 // If here, it means that no provider_host was found, in which case, the | 1262 // If here, it means that no provider_host was found, in which case, the |
1254 // renderer should still be informed that the window was opened. | 1263 // renderer should still be informed that the window was opened. |
1255 OnOpenWindowFinished(request_id, std::string(), ServiceWorkerClientInfo()); | 1264 OnOpenWindowFinished(request_id, std::string(), ServiceWorkerClientInfo()); |
1256 } | 1265 } |
1257 | 1266 |
1258 void ServiceWorkerVersion::OnOpenWindowFinished( | 1267 void ServiceWorkerVersion::OnOpenWindowFinished( |
1259 int request_id, | 1268 int request_id, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1310 TRACE_EVENT_ASYNC_END1("ServiceWorker", | 1319 TRACE_EVENT_ASYNC_END1("ServiceWorker", |
1311 "ServiceWorkerVersion::OnClearCachedMetadata", | 1320 "ServiceWorkerVersion::OnClearCachedMetadata", |
1312 callback_id, "result", result); | 1321 callback_id, "result", result); |
1313 FOR_EACH_OBSERVER(Listener, listeners_, OnCachedMetadataUpdated(this)); | 1322 FOR_EACH_OBSERVER(Listener, listeners_, OnCachedMetadataUpdated(this)); |
1314 } | 1323 } |
1315 | 1324 |
1316 void ServiceWorkerVersion::OnPostMessageToClient( | 1325 void ServiceWorkerVersion::OnPostMessageToClient( |
1317 const std::string& client_uuid, | 1326 const std::string& client_uuid, |
1318 const base::string16& message, | 1327 const base::string16& message, |
1319 const std::vector<TransferredMessagePort>& sent_message_ports) { | 1328 const std::vector<TransferredMessagePort>& sent_message_ports) { |
| 1329 if (!context_) |
| 1330 return; |
1320 TRACE_EVENT1("ServiceWorker", | 1331 TRACE_EVENT1("ServiceWorker", |
1321 "ServiceWorkerVersion::OnPostMessageToDocument", | 1332 "ServiceWorkerVersion::OnPostMessageToDocument", |
1322 "Client id", client_uuid); | 1333 "Client id", client_uuid); |
1323 auto it = controllee_map_.find(client_uuid); | 1334 ServiceWorkerProviderHost* provider_host = |
1324 if (it == controllee_map_.end()) { | 1335 context_->GetProviderHostByClientID(client_uuid); |
| 1336 if (!provider_host) { |
1325 // The client may already have been closed, just ignore. | 1337 // The client may already have been closed, just ignore. |
1326 return; | 1338 return; |
1327 } | 1339 } |
1328 if (it->second->document_url().GetOrigin() != script_url_.GetOrigin()) { | 1340 if (provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) { |
1329 // The client does not belong to the same origin as this ServiceWorker, | 1341 // The client does not belong to the same origin as this ServiceWorker, |
1330 // possibly due to timing issue or bad message. | 1342 // possibly due to timing issue or bad message. |
1331 return; | 1343 return; |
1332 } | 1344 } |
1333 it->second->PostMessage(message, sent_message_ports); | 1345 provider_host->PostMessage(message, sent_message_ports); |
1334 } | 1346 } |
1335 | 1347 |
1336 void ServiceWorkerVersion::OnFocusClient(int request_id, | 1348 void ServiceWorkerVersion::OnFocusClient(int request_id, |
1337 const std::string& client_uuid) { | 1349 const std::string& client_uuid) { |
| 1350 if (!context_) |
| 1351 return; |
1338 TRACE_EVENT2("ServiceWorker", | 1352 TRACE_EVENT2("ServiceWorker", |
1339 "ServiceWorkerVersion::OnFocusClient", | 1353 "ServiceWorkerVersion::OnFocusClient", |
1340 "Request id", request_id, | 1354 "Request id", request_id, |
1341 "Client id", client_uuid); | 1355 "Client id", client_uuid); |
1342 auto it = controllee_map_.find(client_uuid); | 1356 ServiceWorkerProviderHost* provider_host = |
1343 if (it == controllee_map_.end()) { | 1357 context_->GetProviderHostByClientID(client_uuid); |
| 1358 if (!provider_host) { |
1344 // The client may already have been closed, just ignore. | 1359 // The client may already have been closed, just ignore. |
1345 return; | 1360 return; |
1346 } | 1361 } |
1347 if (it->second->document_url().GetOrigin() != script_url_.GetOrigin()) { | 1362 if (provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) { |
1348 // The client does not belong to the same origin as this ServiceWorker, | 1363 // The client does not belong to the same origin as this ServiceWorker, |
1349 // possibly due to timing issue or bad message. | 1364 // possibly due to timing issue or bad message. |
1350 return; | 1365 return; |
1351 } | 1366 } |
1352 | 1367 provider_host->Focus(base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, |
1353 it->second->Focus( | 1368 weak_factory_.GetWeakPtr(), request_id, |
1354 base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, | 1369 client_uuid)); |
1355 weak_factory_.GetWeakPtr(), | |
1356 request_id, | |
1357 client_uuid)); | |
1358 } | 1370 } |
1359 | 1371 |
1360 void ServiceWorkerVersion::OnFocusClientFinished( | 1372 void ServiceWorkerVersion::OnFocusClientFinished( |
1361 int request_id, | 1373 int request_id, |
1362 const std::string& cliend_uuid, | 1374 const std::string& cliend_uuid, |
1363 const ServiceWorkerClientInfo& client) { | 1375 const ServiceWorkerClientInfo& client) { |
1364 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1376 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1365 | 1377 |
1366 if (running_status() != RUNNING) | 1378 if (running_status() != RUNNING) |
1367 return; | 1379 return; |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1655 int request_id) { | 1667 int request_id) { |
1656 callbacks->Remove(request_id); | 1668 callbacks->Remove(request_id); |
1657 if (is_doomed_) { | 1669 if (is_doomed_) { |
1658 // The stop should be already scheduled, but try to stop immediately, in | 1670 // The stop should be already scheduled, but try to stop immediately, in |
1659 // order to release worker resources soon. | 1671 // order to release worker resources soon. |
1660 StopWorkerIfIdle(); | 1672 StopWorkerIfIdle(); |
1661 } | 1673 } |
1662 } | 1674 } |
1663 | 1675 |
1664 } // namespace content | 1676 } // namespace content |
OLD | NEW |