OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/workers_ui.h" | 5 #include "chrome/browser/ui/webui/workers_ui.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/debugger/devtools_window.h" | 14 #include "chrome/browser/debugger/devtools_window.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" | 16 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" |
17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
19 #include "content/browser/worker_host/worker_process_host.h" | |
20 #include "content/public/browser/child_process_data.h" | 19 #include "content/public/browser/child_process_data.h" |
21 #include "content/public/browser/devtools_agent_host_registry.h" | 20 #include "content/public/browser/devtools_agent_host_registry.h" |
22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
24 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
25 #include "content/public/browser/worker_service.h" | 24 #include "content/public/browser/worker_service.h" |
26 #include "content/public/browser/worker_service_observer.h" | 25 #include "content/public/browser/worker_service_observer.h" |
27 #include "content/public/browser/web_ui_message_handler.h" | 26 #include "content/public/browser/web_ui_message_handler.h" |
28 #include "content/public/common/process_type.h" | 27 #include "content/public/common/process_type.h" |
29 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
(...skipping 16 matching lines...) Expand all Loading... |
46 | 45 |
47 static const char kWorkerProcessHostIdField[] = "workerProcessHostId"; | 46 static const char kWorkerProcessHostIdField[] = "workerProcessHostId"; |
48 static const char kWorkerRouteIdField[] = "workerRouteId"; | 47 static const char kWorkerRouteIdField[] = "workerRouteId"; |
49 static const char kUrlField[] = "url"; | 48 static const char kUrlField[] = "url"; |
50 static const char kNameField[] = "name"; | 49 static const char kNameField[] = "name"; |
51 static const char kPidField[] = "pid"; | 50 static const char kPidField[] = "pid"; |
52 | 51 |
53 namespace { | 52 namespace { |
54 | 53 |
55 | 54 |
56 DictionaryValue* BuildWorkerData(const ChildProcessData& data, | 55 DictionaryValue* BuildWorkerData( |
57 const WorkerProcessHost::WorkerInstance& instance) { | 56 const GURL& url, |
| 57 const string16& name, |
| 58 int process_id, |
| 59 int route_id, |
| 60 base::ProcessHandle handle) { |
58 DictionaryValue* worker_data = new DictionaryValue(); | 61 DictionaryValue* worker_data = new DictionaryValue(); |
59 worker_data->SetInteger(kWorkerProcessHostIdField, data.id); | 62 worker_data->SetInteger(kWorkerProcessHostIdField, process_id); |
60 worker_data->SetInteger(kWorkerRouteIdField, instance.worker_route_id()); | 63 worker_data->SetInteger(kWorkerRouteIdField, route_id); |
61 worker_data->SetString(kUrlField, instance.url().spec()); | 64 worker_data->SetString(kUrlField, url.spec()); |
62 worker_data->SetString(kNameField, instance.name()); | 65 worker_data->SetString(kNameField, name); |
63 worker_data->SetInteger(kPidField, base::GetProcId(data.handle)); | 66 worker_data->SetInteger(kPidField, base::GetProcId(handle)); |
64 return worker_data; | 67 return worker_data; |
65 } | 68 } |
66 | 69 |
67 class WorkersUIHTMLSource : public ChromeWebUIDataSource { | 70 class WorkersUIHTMLSource : public ChromeWebUIDataSource { |
68 public: | 71 public: |
69 WorkersUIHTMLSource(); | 72 WorkersUIHTMLSource(); |
70 | 73 |
71 virtual void StartDataRequest(const std::string& path, | 74 virtual void StartDataRequest(const std::string& path, |
72 bool is_incognito, | 75 bool is_incognito, |
73 int request_id); | 76 int request_id); |
(...skipping 13 matching lines...) Expand all Loading... |
87 bool is_incognito, | 90 bool is_incognito, |
88 int request_id) { | 91 int request_id) { |
89 if (path == kWorkersDataFile) { | 92 if (path == kWorkersDataFile) { |
90 SendSharedWorkersData(request_id); | 93 SendSharedWorkersData(request_id); |
91 } else { | 94 } else { |
92 ChromeWebUIDataSource::StartDataRequest(path, is_incognito, request_id); | 95 ChromeWebUIDataSource::StartDataRequest(path, is_incognito, request_id); |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 void WorkersUIHTMLSource::SendSharedWorkersData(int request_id) { | 99 void WorkersUIHTMLSource::SendSharedWorkersData(int request_id) { |
97 ListValue workers_list; | 100 ListValue workers_list; |
98 for (WorkerProcessHostIterator iter; !iter.Done(); ++iter) { | 101 std::vector<WorkerService::WorkerInfo> worker_info = |
99 const WorkerProcessHost::Instances& instances = iter->instances(); | 102 WorkerService::GetInstance()->GetWorkers(); |
100 for (WorkerProcessHost::Instances::const_iterator i = instances.begin(); | 103 for (size_t i = 0; i < worker_info.size(); ++i) { |
101 i != instances.end(); ++i) { | 104 workers_list.Append(BuildWorkerData( |
102 workers_list.Append(BuildWorkerData(iter.GetData(), *i)); | 105 worker_info[i].url, worker_info[i].name, worker_info[i].process_id, |
103 } | 106 worker_info[i].route_id, worker_info[i].handle)); |
104 } | 107 } |
105 | 108 |
106 std::string json_string; | 109 std::string json_string; |
107 base::JSONWriter::Write(&workers_list, false, &json_string); | 110 base::JSONWriter::Write(&workers_list, false, &json_string); |
108 | 111 |
109 SendResponse(request_id, base::RefCountedString::TakeString(&json_string)); | 112 SendResponse(request_id, base::RefCountedString::TakeString(&json_string)); |
110 } | 113 } |
111 | 114 |
112 class WorkersDOMHandler : public WebUIMessageHandler { | 115 class WorkersDOMHandler : public WebUIMessageHandler { |
113 public: | 116 public: |
114 WorkersDOMHandler() {} | 117 WorkersDOMHandler() {} |
115 virtual ~WorkersDOMHandler() {} | 118 virtual ~WorkersDOMHandler() {} |
116 | 119 |
117 private: | 120 private: |
118 // WebUIMessageHandler implementation. | 121 // WebUIMessageHandler implementation. |
119 virtual void RegisterMessages() OVERRIDE; | 122 virtual void RegisterMessages() OVERRIDE; |
(...skipping 29 matching lines...) Expand all Loading... |
149 Profile* profile = Profile::FromWebUI(web_ui()); | 152 Profile* profile = Profile::FromWebUI(web_ui()); |
150 if (!profile) | 153 if (!profile) |
151 return; | 154 return; |
152 DevToolsAgentHost* agent_host = | 155 DevToolsAgentHost* agent_host = |
153 DevToolsAgentHostRegistry::GetDevToolsAgentHostForWorker( | 156 DevToolsAgentHostRegistry::GetDevToolsAgentHostForWorker( |
154 worker_process_host_id, | 157 worker_process_host_id, |
155 worker_route_id); | 158 worker_route_id); |
156 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); | 159 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); |
157 } | 160 } |
158 | 161 |
159 static void TerminateWorker(int worker_process_id, int worker_route_id) { | 162 static void TerminateWorker(int process_id, int route_id) { |
160 for (WorkerProcessHostIterator iter; !iter.Done(); ++iter) { | 163 WorkerService::GetInstance()->TerminateWorker(process_id, route_id); |
161 if (iter.GetData().id == worker_process_id) { | |
162 iter->TerminateWorker(worker_route_id); | |
163 return; | |
164 } | |
165 } | |
166 } | 164 } |
167 | 165 |
168 void WorkersDOMHandler::HandleTerminateWorker(const ListValue* args) { | 166 void WorkersDOMHandler::HandleTerminateWorker(const ListValue* args) { |
169 std::string worker_process_host_id_str; | 167 std::string worker_process_host_id_str; |
170 std::string worker_route_id_str; | 168 std::string worker_route_id_str; |
171 int worker_process_host_id; | 169 int worker_process_host_id; |
172 int worker_route_id; | 170 int worker_route_id; |
173 CHECK(args->GetSize() == 2); | 171 CHECK(args->GetSize() == 2); |
174 CHECK(args->GetString(0, &worker_process_host_id_str)); | 172 CHECK(args->GetString(0, &worker_process_host_id_str)); |
175 CHECK(args->GetString(1, &worker_route_id_str)); | 173 CHECK(args->GetString(1, &worker_route_id_str)); |
(...skipping 26 matching lines...) Expand all Loading... |
202 base::Bind(&WorkerCreationDestructionListener::UnregisterObserver, | 200 base::Bind(&WorkerCreationDestructionListener::UnregisterObserver, |
203 this)); | 201 this)); |
204 } | 202 } |
205 | 203 |
206 private: | 204 private: |
207 friend class base::RefCountedThreadSafe<WorkerCreationDestructionListener>; | 205 friend class base::RefCountedThreadSafe<WorkerCreationDestructionListener>; |
208 virtual ~WorkerCreationDestructionListener() { | 206 virtual ~WorkerCreationDestructionListener() { |
209 } | 207 } |
210 | 208 |
211 virtual void WorkerCreated( | 209 virtual void WorkerCreated( |
212 WorkerProcessHost* process, | 210 const GURL& url, |
213 const WorkerProcessHost::WorkerInstance& instance) OVERRIDE { | 211 const string16& name, |
| 212 int process_id, |
| 213 int route_id) OVERRIDE { |
214 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
215 BrowserThread::UI, FROM_HERE, | 215 BrowserThread::UI, FROM_HERE, |
216 base::Bind( | 216 base::Bind(&WorkerCreationDestructionListener::NotifyWorkerCreated, |
217 &WorkerCreationDestructionListener::NotifyWorkerCreated, | 217 this, |
218 this, base::Owned(BuildWorkerData(process->GetData(), instance)))); | 218 base::Owned(BuildWorkerData(url, name, process_id, route_id, |
| 219 base::kNullProcessHandle)))); |
219 } | 220 } |
220 virtual void WorkerDestroyed( | 221 virtual void WorkerDestroyed(int process_id, int route_id) OVERRIDE { |
221 WorkerProcessHost* process, | |
222 int worker_route_id) OVERRIDE { | |
223 DictionaryValue* worker_data = new DictionaryValue(); | 222 DictionaryValue* worker_data = new DictionaryValue(); |
224 worker_data->SetInteger(kWorkerProcessHostIdField, process->GetData().id); | 223 worker_data->SetInteger(kWorkerProcessHostIdField, process_id); |
225 worker_data->SetInteger(kWorkerRouteIdField, worker_route_id); | 224 worker_data->SetInteger(kWorkerRouteIdField, route_id); |
226 | 225 |
227 BrowserThread::PostTask( | 226 BrowserThread::PostTask( |
228 BrowserThread::UI, FROM_HERE, | 227 BrowserThread::UI, FROM_HERE, |
229 base::Bind(&WorkerCreationDestructionListener::NotifyWorkerDestroyed, | 228 base::Bind(&WorkerCreationDestructionListener::NotifyWorkerDestroyed, |
230 this, base::Owned(worker_data))); | 229 this, base::Owned(worker_data))); |
231 } | 230 } |
232 virtual void WorkerContextStarted(WorkerProcessHost*, int) OVERRIDE {} | |
233 | 231 |
234 void NotifyWorkerCreated(DictionaryValue* worker_data) { | 232 void NotifyWorkerCreated(DictionaryValue* worker_data) { |
235 if (workers_ui_) { | 233 if (workers_ui_) { |
236 workers_ui_->web_ui()->CallJavascriptFunction( | 234 workers_ui_->web_ui()->CallJavascriptFunction( |
237 "workerCreated", *worker_data); | 235 "workerCreated", *worker_data); |
238 } | 236 } |
239 } | 237 } |
240 | 238 |
241 void NotifyWorkerDestroyed(DictionaryValue* worker_data) { | 239 void NotifyWorkerDestroyed(DictionaryValue* worker_data) { |
242 if (workers_ui_) { | 240 if (workers_ui_) { |
(...skipping 21 matching lines...) Expand all Loading... |
264 | 262 |
265 // Set up the chrome://workers/ source. | 263 // Set up the chrome://workers/ source. |
266 Profile* profile = Profile::FromWebUI(web_ui); | 264 Profile* profile = Profile::FromWebUI(web_ui); |
267 profile->GetChromeURLDataManager()->AddDataSource(html_source); | 265 profile->GetChromeURLDataManager()->AddDataSource(html_source); |
268 } | 266 } |
269 | 267 |
270 WorkersUI::~WorkersUI() { | 268 WorkersUI::~WorkersUI() { |
271 observer_->WorkersUIDestroyed(); | 269 observer_->WorkersUIDestroyed(); |
272 observer_ = NULL; | 270 observer_ = NULL; |
273 } | 271 } |
OLD | NEW |