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

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: send increment/decrement request from renderer/ process Created 4 years, 3 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_registry.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 14 matching lines...) Expand all
49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure); 50 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
50 } 51 }
51 52
52 void WorkerStarted(const ServiceWorkerContextWrapper::StatusCallback& callback, 53 void WorkerStarted(const ServiceWorkerContextWrapper::StatusCallback& callback,
53 ServiceWorkerStatusCode status) { 54 ServiceWorkerStatusCode status) {
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); 55 DCHECK_CURRENTLY_ON(BrowserThread::IO);
55 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 56 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
56 base::Bind(callback, status)); 57 base::Bind(callback, status));
57 } 58 }
58 59
60 void DoNothing(ServiceWorkerStatusCode status) {}
falken 2016/09/23 02:27:35 you can use ServiceWorkerUtils::NoOpStatusCallback
lazyboy 2016/09/27 21:26:45 Done.
61
59 void StartActiveWorkerOnIO( 62 void StartActiveWorkerOnIO(
60 const ServiceWorkerContextWrapper::StatusCallback& callback, 63 const ServiceWorkerContextWrapper::StatusCallback& callback,
61 ServiceWorkerStatusCode status, 64 ServiceWorkerStatusCode status,
62 scoped_refptr<ServiceWorkerRegistration> registration) { 65 scoped_refptr<ServiceWorkerRegistration> registration) {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO); 66 DCHECK_CURRENTLY_ON(BrowserThread::IO);
64 if (status == SERVICE_WORKER_OK) { 67 if (status == SERVICE_WORKER_OK) {
65 // Pass the reference of |registration| to WorkerStarted callback to prevent 68 // Pass the reference of |registration| to WorkerStarted callback to prevent
66 // it from being deleted while starting the worker. If the refcount of 69 // it from being deleted while starting the worker. If the refcount of
67 // |registration| is 1, it will be deleted after WorkerStarted is called. 70 // |registration| is 1, it will be deleted after WorkerStarted is called.
68 registration->active_version()->StartWorker( 71 registration->active_version()->StartWorker(
(...skipping 26 matching lines...) Expand all
95 NAVIGATION_HINT_LINK_TAP_UNCONFIRMED; 98 NAVIGATION_HINT_LINK_TAP_UNCONFIRMED;
96 case blink::WebNavigationHintType::LinkTapDown: 99 case blink::WebNavigationHintType::LinkTapDown:
97 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN; 100 return ServiceWorkerMetrics::EventType::NAVIGATION_HINT_LINK_TAP_DOWN;
98 } 101 }
99 NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type); 102 NOTREACHED() << "Unexpected navigation hint" << static_cast<int>(type);
100 return ServiceWorkerMetrics::EventType::UNKNOWN; 103 return ServiceWorkerMetrics::EventType::UNKNOWN;
101 } 104 }
102 105
103 } // namespace 106 } // namespace
104 107
108 bool ServiceWorkerContextWrapper::IncrementPendingActivity(
109 int64_t service_worker_version_id) {
110 return IncrementPendingActivityOnIO(service_worker_version_id);
falken 2016/09/23 02:27:35 Can the *OnIO functions just be removed?
lazyboy 2016/09/27 21:26:45 Oh yes, these used to do thread hops, done.
111 }
112
113 bool ServiceWorkerContextWrapper::DecrementPendingActivity(
114 int64_t service_worker_version_id) {
115 return DecrementPendingActivityOnIO(service_worker_version_id);
116 }
117
105 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( 118 void ServiceWorkerContext::AddExcludedHeadersForFetchEvent(
106 const std::set<std::string>& header_names) { 119 const std::set<std::string>& header_names) {
107 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. 120 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
108 tracked_objects::ScopedTracker tracking_profile( 121 tracked_objects::ScopedTracker tracking_profile(
109 FROM_HERE_WITH_EXPLICIT_FUNCTION( 122 FROM_HERE_WITH_EXPLICIT_FUNCTION(
110 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent")); 123 "477117 ServiceWorkerContext::AddExcludedHeadersForFetchEvent"));
111 DCHECK_CURRENTLY_ON(BrowserThread::IO); 124 DCHECK_CURRENTLY_ON(BrowserThread::IO);
112 g_excluded_header_name_set.Get().insert(header_names.begin(), 125 g_excluded_header_name_set.Get().insert(header_names.begin(),
113 header_names.end()); 126 header_names.end());
114 } 127 }
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 user_data_directory, std::move(database_task_manager), disk_cache_thread, 831 user_data_directory, std::move(database_task_manager), disk_cache_thread,
819 quota_manager_proxy, special_storage_policy, observer_list_.get(), this)); 832 quota_manager_proxy, special_storage_policy, observer_list_.get(), this));
820 } 833 }
821 834
822 void ServiceWorkerContextWrapper::ShutdownOnIO() { 835 void ServiceWorkerContextWrapper::ShutdownOnIO() {
823 DCHECK_CURRENTLY_ON(BrowserThread::IO); 836 DCHECK_CURRENTLY_ON(BrowserThread::IO);
824 resource_context_ = nullptr; 837 resource_context_ = nullptr;
825 context_core_.reset(); 838 context_core_.reset();
826 } 839 }
827 840
841 bool ServiceWorkerContextWrapper::IncrementPendingActivityOnIO(
842 int64_t service_worker_version_id) {
843 DCHECK_CURRENTLY_ON(BrowserThread::IO);
844 ServiceWorkerVersion* version =
845 context()->GetLiveVersion(service_worker_version_id);
846 if (!version) {
falken 2016/09/23 02:27:35 You should also confirm the version is RUNNING. It
lazyboy 2016/09/27 21:26:45 Done.
847 LOG(WARNING) << "ServiceWorkerVersion not found: "
848 << service_worker_version_id;
falken 2016/09/23 02:27:35 do you need these logs in production code? DVLOG(1
lazyboy 2016/09/27 21:26:45 No, changed to DVLOG, thanks.
849 return false;
850 }
851 int request_id =
852 version->StartRequest(ServiceWorkerMetrics::EventType::EXTERNAL_REQUEST,
853 base::Bind(&DoNothing));
854 pending_external_requests_[service_worker_version_id].insert(request_id);
855 return true;
856 }
857
858 bool ServiceWorkerContextWrapper::DecrementPendingActivityOnIO(
859 int64_t service_worker_version_id) {
860 DCHECK_CURRENTLY_ON(BrowserThread::IO);
861 ServiceWorkerVersion* version =
862 context()->GetLiveVersion(service_worker_version_id);
863 if (!version) {
falken 2016/09/23 02:27:35 I'm not too sure when we get in this state: the re
lazyboy 2016/09/27 21:26:45 Changed to DCHECK. This could uncover any timing i
864 LOG(WARNING) << "ServiceWorkerVersion not found: "
865 << service_worker_version_id;
866 return false;
867 }
868 std::set<int>& pending_requests =
869 pending_external_requests_[service_worker_version_id];
870 DCHECK(pending_requests.size() > 0);
871 int request_id = *pending_requests.begin();
872 pending_requests.erase(request_id);
873 version->FinishRequest(request_id, true, base::Time::Now());
874 return true;
falken 2016/09/23 02:27:35 This ignores FinishRequest's return value. Maybe i
lazyboy 2016/09/27 21:26:45 Done.
875 }
876
828 void ServiceWorkerContextWrapper::DidDeleteAndStartOver( 877 void ServiceWorkerContextWrapper::DidDeleteAndStartOver(
829 ServiceWorkerStatusCode status) { 878 ServiceWorkerStatusCode status) {
830 DCHECK_CURRENTLY_ON(BrowserThread::IO); 879 DCHECK_CURRENTLY_ON(BrowserThread::IO);
831 if (status != SERVICE_WORKER_OK) { 880 if (status != SERVICE_WORKER_OK) {
832 context_core_.reset(); 881 context_core_.reset();
833 return; 882 return;
834 } 883 }
835 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); 884 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this));
836 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; 885 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully.";
837 886
838 observer_list_->Notify(FROM_HERE, 887 observer_list_->Notify(FROM_HERE,
839 &ServiceWorkerContextObserver::OnStorageWiped); 888 &ServiceWorkerContextObserver::OnStorageWiped);
840 } 889 }
841 890
842 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { 891 ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() {
843 DCHECK_CURRENTLY_ON(BrowserThread::IO); 892 DCHECK_CURRENTLY_ON(BrowserThread::IO);
844 return context_core_.get(); 893 return context_core_.get();
845 } 894 }
846 895
847 } // namespace content 896 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698