OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_process_manager.h" | 5 #include "content/browser/service_worker/service_worker_process_manager.h" |
6 | 6 |
7 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | |
7 #include "content/browser/renderer_host/render_process_host_impl.h" | 8 #include "content/browser/renderer_host/render_process_host_impl.h" |
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/site_instance.h" | 11 #include "content/public/browser/site_instance.h" |
11 #include "url/gurl.h" | 12 #include "url/gurl.h" |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
16 namespace { | |
17 | |
18 void GetRoutingIDAndNotifyWorkerCreated(RenderProcessHost* rph, | |
19 const base::FilePath& path, | |
20 const GURL& scope, | |
21 int* route_id, | |
22 bool* pause_on_start) { | |
23 if (!rph) { | |
24 // |rph| may NULL in unit tests. | |
kinuko
2014/05/02 11:12:45
nit: may -> may be
horo
2014/05/07 08:34:44
Done.
| |
25 *route_id = MSG_ROUTING_NONE; | |
26 *pause_on_start = false; | |
27 return; | |
28 } | |
29 *route_id = rph->GetNextRoutingID(); | |
30 *pause_on_start = | |
31 EmbeddedWorkerDevToolsManager::GetInstance()->ServiceWorkerCreated( | |
32 rph->GetID(), *route_id, path, scope); | |
33 } | |
34 | |
35 } // namespace | |
36 | |
15 ServiceWorkerProcessManager::ServiceWorkerProcessManager( | 37 ServiceWorkerProcessManager::ServiceWorkerProcessManager( |
16 ServiceWorkerContextWrapper* context_wrapper) | 38 ServiceWorkerContextWrapper* context_wrapper) |
17 : context_wrapper_(context_wrapper), | 39 : context_wrapper_(context_wrapper), |
18 weak_this_factory_(this), | 40 weak_this_factory_(this), |
19 weak_this_(weak_this_factory_.GetWeakPtr()) { | 41 weak_this_(weak_this_factory_.GetWeakPtr()) { |
20 } | 42 } |
21 | 43 |
22 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { | 44 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { |
23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 45 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
24 } | 46 } |
25 | 47 |
26 void ServiceWorkerProcessManager::AllocateWorkerProcess( | 48 void ServiceWorkerProcessManager::AllocateWorkerProcess( |
27 const std::vector<int>& process_ids, | 49 const std::vector<int>& process_ids, |
50 const base::FilePath& path, | |
51 const GURL& scope, | |
28 const GURL& script_url, | 52 const GURL& script_url, |
29 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 53 const base::Callback<void(ServiceWorkerStatusCode, |
30 callback) const { | 54 int process_id, |
55 int worker_devtools_agent_route_id, | |
56 bool pause_on_start)>& callback) const { | |
31 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 57 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
32 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
33 BrowserThread::UI, | 59 BrowserThread::UI, |
34 FROM_HERE, | 60 FROM_HERE, |
35 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, | 61 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, |
36 weak_this_, | 62 weak_this_, |
37 process_ids, | 63 process_ids, |
64 path, | |
65 scope, | |
38 script_url, | 66 script_url, |
39 callback)); | 67 callback)); |
40 return; | 68 return; |
41 } | 69 } |
42 | 70 int route_id = MSG_ROUTING_NONE; |
71 bool pause_on_start = false; | |
43 for (std::vector<int>::const_iterator it = process_ids.begin(); | 72 for (std::vector<int>::const_iterator it = process_ids.begin(); |
44 it != process_ids.end(); | 73 it != process_ids.end(); |
45 ++it) { | 74 ++it) { |
46 if (IncrementWorkerRefcountByPid(*it)) { | 75 if (IncrementWorkerRefcountByPid(*it)) { |
47 BrowserThread::PostTask(BrowserThread::IO, | 76 GetRoutingIDAndNotifyWorkerCreated(RenderProcessHost::FromID(*it), |
48 FROM_HERE, | 77 path, |
49 base::Bind(callback, SERVICE_WORKER_OK, *it)); | 78 scope, |
79 &route_id, | |
80 &pause_on_start); | |
81 BrowserThread::PostTask( | |
82 BrowserThread::IO, | |
83 FROM_HERE, | |
84 base::Bind( | |
85 callback, SERVICE_WORKER_OK, *it, route_id, pause_on_start)); | |
50 return; | 86 return; |
51 } | 87 } |
52 } | 88 } |
53 | 89 |
54 if (!context_wrapper_->browser_context_) { | 90 if (!context_wrapper_->browser_context_) { |
55 // Shutdown has started. | 91 // Shutdown has started. |
56 BrowserThread::PostTask( | 92 BrowserThread::PostTask(BrowserThread::IO, |
57 BrowserThread::IO, | 93 FROM_HERE, |
58 FROM_HERE, | 94 base::Bind(callback, |
59 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 95 SERVICE_WORKER_ERROR_START_WORKER_FAILED, |
96 -1, | |
97 route_id, | |
98 pause_on_start)); | |
60 return; | 99 return; |
61 } | 100 } |
62 // No existing processes available; start a new one. | 101 // No existing processes available; start a new one. |
63 scoped_refptr<SiteInstance> site_instance = SiteInstance::CreateForURL( | 102 scoped_refptr<SiteInstance> site_instance = SiteInstance::CreateForURL( |
64 context_wrapper_->browser_context_, script_url); | 103 context_wrapper_->browser_context_, script_url); |
65 RenderProcessHost* rph = site_instance->GetProcess(); | 104 RenderProcessHost* rph = site_instance->GetProcess(); |
66 // This Init() call posts a task to the IO thread that adds the RPH's | 105 // This Init() call posts a task to the IO thread that adds the RPH's |
67 // ServiceWorkerDispatcherHost to the | 106 // ServiceWorkerDispatcherHost to the |
68 // EmbeddedWorkerRegistry::process_sender_map_. | 107 // EmbeddedWorkerRegistry::process_sender_map_. |
69 if (!rph->Init()) { | 108 if (!rph->Init()) { |
70 LOG(ERROR) << "Couldn't start a new process!"; | 109 LOG(ERROR) << "Couldn't start a new process!"; |
71 BrowserThread::PostTask( | 110 BrowserThread::PostTask(BrowserThread::IO, |
72 BrowserThread::IO, | 111 FROM_HERE, |
73 FROM_HERE, | 112 base::Bind(callback, |
74 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 113 SERVICE_WORKER_ERROR_START_WORKER_FAILED, |
114 -1, | |
115 route_id, | |
116 pause_on_start)); | |
75 return; | 117 return; |
76 } | 118 } |
77 | 119 |
78 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | 120 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
121 GetRoutingIDAndNotifyWorkerCreated( | |
122 rph, path, scope, &route_id, &pause_on_start); | |
79 BrowserThread::PostTask( | 123 BrowserThread::PostTask( |
80 BrowserThread::IO, | 124 BrowserThread::IO, |
81 FROM_HERE, | 125 FROM_HERE, |
82 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); | 126 base::Bind( |
127 callback, SERVICE_WORKER_OK, rph->GetID(), route_id, pause_on_start)); | |
83 } | 128 } |
84 | 129 |
85 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int process_id) { | 130 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int process_id) { |
86 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 131 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
87 BrowserThread::PostTask( | 132 BrowserThread::PostTask( |
88 BrowserThread::UI, | 133 BrowserThread::UI, |
89 FROM_HERE, | 134 FROM_HERE, |
90 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, | 135 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, |
91 weak_this_, | 136 weak_this_, |
92 process_id)); | 137 process_id)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 namespace base { | 181 namespace base { |
137 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the | 182 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the |
138 // member WeakPtr to safely guard the object's lifetime when used on that | 183 // member WeakPtr to safely guard the object's lifetime when used on that |
139 // thread. | 184 // thread. |
140 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( | 185 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( |
141 content::ServiceWorkerProcessManager* ptr) const { | 186 content::ServiceWorkerProcessManager* ptr) const { |
142 content::BrowserThread::DeleteSoon( | 187 content::BrowserThread::DeleteSoon( |
143 content::BrowserThread::UI, FROM_HERE, ptr); | 188 content::BrowserThread::UI, FROM_HERE, ptr); |
144 } | 189 } |
145 } // namespace base | 190 } // namespace base |
OLD | NEW |