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

Side by Side Diff: content/browser/service_worker/service_worker_context_wrapper.cc

Issue 2166523003: Add ref count to service workers for extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments from falken@ Created 4 years, 2 months 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 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_context_wrapper.h" 5 #include "content/browser/service_worker/service_worker_context_wrapper.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/barrier_closure.h" 13 #include "base/barrier_closure.h"
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
16 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
17 #include "base/location.h" 17 #include "base/location.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/profiler/scoped_tracker.h" 19 #include "base/profiler/scoped_tracker.h"
20 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/stl_util.h" 21 #include "base/stl_util.h"
22 #include "base/threading/sequenced_worker_pool.h" 22 #include "base/threading/sequenced_worker_pool.h"
23 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
24 #include "content/browser/renderer_host/render_process_host_impl.h" 24 #include "content/browser/renderer_host/render_process_host_impl.h"
25 #include "content/browser/service_worker/embedded_worker_status.h"
25 #include "content/browser/service_worker/service_worker_context_core.h" 26 #include "content/browser/service_worker/service_worker_context_core.h"
26 #include "content/browser/service_worker/service_worker_context_observer.h" 27 #include "content/browser/service_worker/service_worker_context_observer.h"
27 #include "content/browser/service_worker/service_worker_process_manager.h" 28 #include "content/browser/service_worker/service_worker_process_manager.h"
28 #include "content/browser/service_worker/service_worker_quota_client.h" 29 #include "content/browser/service_worker/service_worker_quota_client.h"
29 #include "content/browser/service_worker/service_worker_version.h" 30 #include "content/browser/service_worker/service_worker_version.h"
30 #include "content/browser/storage_partition_impl.h" 31 #include "content/browser/storage_partition_impl.h"
31 #include "content/common/service_worker/service_worker_utils.h" 32 #include "content/common/service_worker/service_worker_utils.h"
32 #include "content/public/browser/browser_context.h" 33 #include "content/public/browser/browser_context.h"
33 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 void ServiceWorkerContextWrapper::DidFinishNavigationHintTaskOnUI( 371 void ServiceWorkerContextWrapper::DidFinishNavigationHintTaskOnUI(
371 int render_process_id, 372 int render_process_id,
372 const ResultCallback& callback, 373 const ResultCallback& callback,
373 bool result) { 374 bool result) {
374 DCHECK_CURRENTLY_ON(BrowserThread::UI); 375 DCHECK_CURRENTLY_ON(BrowserThread::UI);
375 if (!--navigation_hint_task_count_per_process_[render_process_id]) 376 if (!--navigation_hint_task_count_per_process_[render_process_id])
376 navigation_hint_task_count_per_process_.erase(render_process_id); 377 navigation_hint_task_count_per_process_.erase(render_process_id);
377 callback.Run(result); 378 callback.Run(result);
378 } 379 }
379 380
381 void ServiceWorkerContextWrapper::ExternalRequestErrorCallback(
382 int64_t service_worker_version_id,
383 const std::string& request_uuid,
384 ServiceWorkerStatusCode status) {
385 if (status != SERVICE_WORKER_OK) {
386 int request_id_not_used;
387 RemovePendingExternalRequest(service_worker_version_id, request_uuid,
388 &request_id_not_used);
389 }
390 }
391
392 bool ServiceWorkerContextWrapper::RemovePendingExternalRequest(
393 int64_t service_worker_version_id,
394 const std::string& request_uuid,
395 int* request_id) {
Devlin 2016/10/05 17:02:25 drive-by nit: DCHECK(request_id)?
lazyboy 2016/10/06 01:02:06 Obsolete /w the recent change suggested by michael
396 RequestUUIDToRequestID& map_iter =
397 pending_external_requests_[service_worker_version_id];
398 RequestUUIDToRequestID::iterator iter = map_iter.find(request_uuid);
399 if (iter == map_iter.end())
400 return false;
401 *request_id = iter->second;
michaeln 2016/10/05 19:22:53 where is .erase(iter)? i don't see where anything
lazyboy 2016/10/06 01:02:06 Good catch, I might have dropped removing when upl
402 return true;
403 }
404
380 bool ServiceWorkerContextWrapper::IsRunningNavigationHintTask( 405 bool ServiceWorkerContextWrapper::IsRunningNavigationHintTask(
381 int render_process_id) const { 406 int render_process_id) const {
382 DCHECK_CURRENTLY_ON(BrowserThread::UI); 407 DCHECK_CURRENTLY_ON(BrowserThread::UI);
383 return base::ContainsKey(navigation_hint_task_count_per_process_, 408 return base::ContainsKey(navigation_hint_task_count_per_process_,
384 render_process_id); 409 render_process_id);
385 } 410 }
386 411
387 void ServiceWorkerContextWrapper::StartServiceWorker( 412 void ServiceWorkerContextWrapper::StartServiceWorker(
388 const GURL& pattern, 413 const GURL& pattern,
389 const StatusCallback& callback) { 414 const StatusCallback& callback) {
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 user_data_directory, std::move(database_task_manager), disk_cache_thread, 843 user_data_directory, std::move(database_task_manager), disk_cache_thread,
819 quota_manager_proxy, special_storage_policy, observer_list_.get(), this)); 844 quota_manager_proxy, special_storage_policy, observer_list_.get(), this));
820 } 845 }
821 846
822 void ServiceWorkerContextWrapper::ShutdownOnIO() { 847 void ServiceWorkerContextWrapper::ShutdownOnIO() {
823 DCHECK_CURRENTLY_ON(BrowserThread::IO); 848 DCHECK_CURRENTLY_ON(BrowserThread::IO);
824 resource_context_ = nullptr; 849 resource_context_ = nullptr;
825 context_core_.reset(); 850 context_core_.reset();
826 } 851 }
827 852
853 bool ServiceWorkerContextWrapper::IncrementPendingActivity(
854 int64_t service_worker_version_id,
855 const std::string& request_uuid) {
856 DCHECK_CURRENTLY_ON(BrowserThread::IO);
857 ServiceWorkerVersion* version =
858 context()->GetLiveVersion(service_worker_version_id);
859 if (!version)
860 return false;
861
862 if (version->running_status() != EmbeddedWorkerStatus::RUNNING)
Devlin 2016/10/05 17:02:25 Can this legitimately happen? If so, how? (Maybe
lazyboy 2016/10/06 01:02:06 Added comment.
863 return false;
864
865 int request_id = version->StartRequest(
michaeln 2016/10/05 19:22:53 version->StartExternalRequest(uuid); // no error
lazyboy 2016/10/06 01:02:06 OK. Moved the map inside ServiceWorkerVersion and
866 ServiceWorkerMetrics::EventType::EXTERNAL_REQUEST,
867 base::Bind(&ServiceWorkerContextWrapper::ExternalRequestErrorCallback,
868 this, service_worker_version_id, request_uuid));
869 pending_external_requests_[service_worker_version_id].insert(
870 std::make_pair(request_uuid, request_id));
871 return true;
872 }
873
874 bool ServiceWorkerContextWrapper::DecrementPendingActivity(
875 int64_t service_worker_version_id,
876 const std::string& request_uuid) {
877 DCHECK_CURRENTLY_ON(BrowserThread::IO);
878 int request_id = -1;
879 bool had_request = RemovePendingExternalRequest(service_worker_version_id,
880 request_uuid, &request_id);
881 if (!had_request) {
882 // It is possible that the request was cancelled or timed out before and we
883 // won't find it in |worker_requests|.
884 return true; // Return true so we don't kill the process.
885 }
886
887 ServiceWorkerVersion* version =
888 context()->GetLiveVersion(service_worker_version_id);
889
890 // The service worker should be present and running.
891 if (!version || version->running_status() != EmbeddedWorkerStatus::RUNNING)
Devlin 2016/10/05 17:02:25 ditto
lazyboy 2016/10/06 01:02:06 Done.
892 return false;
893
894 return version->FinishRequest(request_id, true, base::Time::Now());
michaeln 2016/10/05 19:22:53 version->FinishExternalRequest(uuid);
lazyboy 2016/10/06 01:02:06 Done.
895 }
896
828 void ServiceWorkerContextWrapper::DidDeleteAndStartOver( 897 void ServiceWorkerContextWrapper::DidDeleteAndStartOver(
829 ServiceWorkerStatusCode status) { 898 ServiceWorkerStatusCode status) {
830 DCHECK_CURRENTLY_ON(BrowserThread::IO); 899 DCHECK_CURRENTLY_ON(BrowserThread::IO);
831 if (status != SERVICE_WORKER_OK) { 900 if (status != SERVICE_WORKER_OK) {
832 context_core_.reset(); 901 context_core_.reset();
833 return; 902 return;
834 } 903 }
835 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); 904 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this));
836 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; 905 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully.";
837 906
838 observer_list_->Notify(FROM_HERE, 907 observer_list_->Notify(FROM_HERE,
839 &ServiceWorkerContextObserver::OnStorageWiped); 908 &ServiceWorkerContextObserver::OnStorageWiped);
840 } 909 }
841 910
842 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { 911 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() {
843 DCHECK_CURRENTLY_ON(BrowserThread::IO); 912 DCHECK_CURRENTLY_ON(BrowserThread::IO);
844 return context_core_.get(); 913 return context_core_.get();
845 } 914 }
846 915
847 } // namespace content 916 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698