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 "chrome/browser/devtools/devtools_target_impl.h" | 5 #include "chrome/browser/devtools/devtools_target_impl.h" |
6 | 6 |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/devtools/devtools_window.h" | 9 #include "chrome/browser/devtools/devtools_window.h" |
10 #include "chrome/browser/extensions/extension_tab_util.h" | 10 #include "chrome/browser/extensions/extension_tab_util.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 using content::DevToolsAgentHost; | 29 using content::DevToolsAgentHost; |
30 using content::RenderViewHost; | 30 using content::RenderViewHost; |
31 using content::WebContents; | 31 using content::WebContents; |
32 using content::WorkerService; | 32 using content::WorkerService; |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 const char kTargetTypeApp[] = "app"; | 36 const char kTargetTypeApp[] = "app"; |
37 const char kTargetTypeBackgroundPage[] = "background_page"; | 37 const char kTargetTypeBackgroundPage[] = "background_page"; |
38 const char kTargetTypePage[] = "page"; | 38 const char kTargetTypePage[] = "page"; |
39 const char kTargetTypeWorker[] = "worker"; | |
40 const char kTargetTypeWebView[] = "webview"; | 39 const char kTargetTypeWebView[] = "webview"; |
41 const char kTargetTypeIFrame[] = "iframe"; | 40 const char kTargetTypeIFrame[] = "iframe"; |
42 const char kTargetTypeOther[] = "other"; | 41 const char kTargetTypeOther[] = "other"; |
43 | 42 |
44 // WebContentsTarget -------------------------------------------------------- | 43 // WebContentsTarget -------------------------------------------------------- |
45 | 44 |
46 class WebContentsTarget : public DevToolsTargetImpl { | 45 class WebContentsTarget : public DevToolsTargetImpl { |
47 public: | 46 public: |
48 WebContentsTarget(WebContents* web_contents, bool is_tab); | 47 WebContentsTarget(WebContents* web_contents, bool is_tab); |
49 | 48 |
50 // DevToolsTargetImpl overrides: | 49 // DevToolsTargetImpl overrides: |
51 virtual bool Activate() const OVERRIDE; | |
52 virtual bool Close() const OVERRIDE; | |
53 virtual WebContents* GetWebContents() const OVERRIDE; | 50 virtual WebContents* GetWebContents() const OVERRIDE; |
54 virtual int GetTabId() const OVERRIDE; | 51 virtual int GetTabId() const OVERRIDE; |
55 virtual std::string GetExtensionId() const OVERRIDE; | 52 virtual std::string GetExtensionId() const OVERRIDE; |
56 virtual void Inspect(Profile* profile) const OVERRIDE; | 53 virtual void Inspect(Profile* profile) const OVERRIDE; |
57 | 54 |
58 private: | 55 private: |
59 int tab_id_; | 56 int tab_id_; |
60 std::string extension_id_; | 57 std::string extension_id_; |
61 }; | 58 }; |
62 | 59 |
63 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) | 60 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) |
64 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), | 61 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), |
65 tab_id_(-1) { | 62 tab_id_(-1) { |
66 set_type(kTargetTypeOther); | 63 set_type(kTargetTypeOther); |
67 | 64 |
68 content::RenderFrameHost* rfh = | 65 content::RenderFrameHost* rfh = |
69 web_contents->GetRenderViewHost()->GetMainFrame(); | 66 web_contents->GetRenderViewHost()->GetMainFrame(); |
70 if (rfh->IsCrossProcessSubframe()) { | 67 if (rfh->IsCrossProcessSubframe()) { |
71 set_url(rfh->GetLastCommittedURL()); | 68 set_url(rfh->GetLastCommittedURL()); |
72 set_type(kTargetTypeIFrame); | 69 set_type(kTargetTypeIFrame); |
73 // TODO(pfeldman) Update for out of process iframes. | 70 // TODO(pfeldman) Update for out of process iframes. |
74 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); | 71 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); |
75 set_parent_id(DevToolsAgentHost::GetOrCreateFor( | 72 set_parent_id(DevToolsAgentHost::GetOrCreateFor( |
76 WebContents::FromRenderViewHost(parent_rvh))->GetId()); | 73 WebContents::FromRenderViewHost(parent_rvh))->GetId()); |
77 return; | 74 return; |
78 } | 75 } |
79 | 76 |
80 set_title(base::UTF16ToUTF8(web_contents->GetTitle())); | |
81 set_url(web_contents->GetURL()); | |
82 content::NavigationController& controller = web_contents->GetController(); | 77 content::NavigationController& controller = web_contents->GetController(); |
83 content::NavigationEntry* entry = controller.GetActiveEntry(); | 78 content::NavigationEntry* entry = controller.GetActiveEntry(); |
84 if (entry != NULL && entry->GetURL().is_valid()) | 79 if (entry != NULL && entry->GetURL().is_valid()) |
85 set_favicon_url(entry->GetFavicon().url); | 80 set_favicon_url(entry->GetFavicon().url); |
86 set_last_activity_time(web_contents->GetLastActiveTime()); | 81 set_last_activity_time(web_contents->GetLastActiveTime()); |
87 | 82 |
88 extensions::GuestViewBase* guest = | 83 extensions::GuestViewBase* guest = |
89 extensions::GuestViewBase::FromWebContents(web_contents); | 84 extensions::GuestViewBase::FromWebContents(web_contents); |
90 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; | 85 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; |
91 if (guest_contents) { | 86 if (guest_contents) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 } else if (extension->is_hosted_app() | 118 } else if (extension->is_hosted_app() |
124 || extension->is_legacy_packaged_app() | 119 || extension->is_legacy_packaged_app() |
125 || extension->is_platform_app()) { | 120 || extension->is_platform_app()) { |
126 set_type(kTargetTypeApp); | 121 set_type(kTargetTypeApp); |
127 } | 122 } |
128 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( | 123 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( |
129 extension, extension_misc::EXTENSION_ICON_SMALLISH, | 124 extension, extension_misc::EXTENSION_ICON_SMALLISH, |
130 ExtensionIconSet::MATCH_BIGGER, false, NULL)); | 125 ExtensionIconSet::MATCH_BIGGER, false, NULL)); |
131 } | 126 } |
132 | 127 |
133 bool WebContentsTarget::Activate() const { | |
134 WebContents* web_contents = GetWebContents(); | |
135 if (!web_contents) | |
136 return false; | |
137 web_contents->GetDelegate()->ActivateContents(web_contents); | |
138 return true; | |
139 } | |
140 | |
141 bool WebContentsTarget::Close() const { | |
142 WebContents* web_contents = GetWebContents(); | |
143 if (!web_contents) | |
144 return false; | |
145 web_contents->GetRenderViewHost()->ClosePage(); | |
146 return true; | |
147 } | |
148 | |
149 WebContents* WebContentsTarget::GetWebContents() const { | 128 WebContents* WebContentsTarget::GetWebContents() const { |
150 return GetAgentHost()->GetWebContents(); | 129 return GetAgentHost()->GetWebContents(); |
151 } | 130 } |
152 | 131 |
153 int WebContentsTarget::GetTabId() const { | 132 int WebContentsTarget::GetTabId() const { |
154 return tab_id_; | 133 return tab_id_; |
155 } | 134 } |
156 | 135 |
157 std::string WebContentsTarget::GetExtensionId() const { | 136 std::string WebContentsTarget::GetExtensionId() const { |
158 return extension_id_; | 137 return extension_id_; |
(...skipping 19 matching lines...) Expand all Loading... | |
178 virtual void Inspect(Profile* profile) const OVERRIDE; | 157 virtual void Inspect(Profile* profile) const OVERRIDE; |
179 | 158 |
180 private: | 159 private: |
181 int process_id_; | 160 int process_id_; |
182 int route_id_; | 161 int route_id_; |
183 }; | 162 }; |
184 | 163 |
185 WorkerTarget::WorkerTarget(const WorkerService::WorkerInfo& worker) | 164 WorkerTarget::WorkerTarget(const WorkerService::WorkerInfo& worker) |
186 : DevToolsTargetImpl(DevToolsAgentHost::GetForWorker(worker.process_id, | 165 : DevToolsTargetImpl(DevToolsAgentHost::GetForWorker(worker.process_id, |
187 worker.route_id)) { | 166 worker.route_id)) { |
188 set_type(kTargetTypeWorker); | |
189 set_title(base::UTF16ToUTF8(worker.name)); | 167 set_title(base::UTF16ToUTF8(worker.name)); |
190 set_description(base::StringPrintf("Worker pid:%d", | 168 set_description(base::StringPrintf("Worker pid:%d", |
191 base::GetProcId(worker.handle))); | 169 base::GetProcId(worker.handle))); |
192 set_url(worker.url); | 170 set_url(worker.url); |
193 | 171 |
194 process_id_ = worker.process_id; | 172 process_id_ = worker.process_id; |
195 route_id_ = worker.route_id; | 173 route_id_ = worker.route_id; |
196 } | 174 } |
197 | 175 |
198 static void TerminateWorker(int process_id, int route_id) { | 176 static void TerminateWorker(int process_id, int route_id) { |
199 WorkerService::GetInstance()->TerminateWorker(process_id, route_id); | 177 WorkerService::GetInstance()->TerminateWorker(process_id, route_id); |
200 } | 178 } |
201 | 179 |
202 bool WorkerTarget::Close() const { | 180 bool WorkerTarget::Close() const { |
203 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 181 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
204 base::Bind(&TerminateWorker, process_id_, route_id_)); | 182 base::Bind(&TerminateWorker, process_id_, route_id_)); |
205 return true; | 183 return true; |
206 } | 184 } |
207 | 185 |
208 void WorkerTarget::Inspect(Profile* profile) const { | 186 void WorkerTarget::Inspect(Profile* profile) const { |
209 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost()); | 187 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost()); |
210 } | 188 } |
211 | 189 |
212 } // namespace | 190 } // namespace |
213 | 191 |
192 // ServiceWorkerTarget --------------------------------------------------------- | |
193 | |
194 class ServiceWorkerTarget : public DevToolsTargetImpl { | |
dgozman
2014/08/21 11:50:24
You should remove this class and update WorkerTarg
vkuzkokov
2014/08/21 14:17:52
Done.
| |
195 public: | |
196 explicit ServiceWorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host); | |
197 virtual void Inspect(Profile* profile) const OVERRIDE; | |
198 }; | |
199 | |
200 ServiceWorkerTarget::ServiceWorkerTarget( | |
201 scoped_refptr<DevToolsAgentHost> agent_host) | |
202 : DevToolsTargetImpl(agent_host) { | |
203 } | |
204 | |
205 void ServiceWorkerTarget::Inspect(Profile* profile) const { | |
206 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost()); | |
207 } | |
208 | |
214 // DevToolsTargetImpl ---------------------------------------------------------- | 209 // DevToolsTargetImpl ---------------------------------------------------------- |
215 | 210 |
216 DevToolsTargetImpl::~DevToolsTargetImpl() { | 211 DevToolsTargetImpl::~DevToolsTargetImpl() { |
217 } | 212 } |
218 | 213 |
219 DevToolsTargetImpl::DevToolsTargetImpl( | 214 DevToolsTargetImpl::DevToolsTargetImpl( |
220 scoped_refptr<DevToolsAgentHost> agent_host) | 215 scoped_refptr<DevToolsAgentHost> agent_host) |
221 : agent_host_(agent_host) { | 216 : agent_host_(agent_host), |
217 type_(agent_host->GetType()), | |
218 title_(agent_host->GetTitle()), | |
219 url_(agent_host->GetURL()) { | |
222 } | 220 } |
223 | 221 |
224 std::string DevToolsTargetImpl::GetParentId() const { | 222 std::string DevToolsTargetImpl::GetParentId() const { |
225 return parent_id_; | 223 return parent_id_; |
226 } | 224 } |
227 | 225 |
228 std::string DevToolsTargetImpl::GetId() const { | 226 std::string DevToolsTargetImpl::GetId() const { |
229 return agent_host_->GetId(); | 227 return agent_host_->GetId(); |
230 } | 228 } |
231 | 229 |
(...skipping 24 matching lines...) Expand all Loading... | |
256 scoped_refptr<content::DevToolsAgentHost> | 254 scoped_refptr<content::DevToolsAgentHost> |
257 DevToolsTargetImpl::GetAgentHost() const { | 255 DevToolsTargetImpl::GetAgentHost() const { |
258 return agent_host_; | 256 return agent_host_; |
259 } | 257 } |
260 | 258 |
261 bool DevToolsTargetImpl::IsAttached() const { | 259 bool DevToolsTargetImpl::IsAttached() const { |
262 return agent_host_->IsAttached(); | 260 return agent_host_->IsAttached(); |
263 } | 261 } |
264 | 262 |
265 bool DevToolsTargetImpl::Activate() const { | 263 bool DevToolsTargetImpl::Activate() const { |
266 return false; | 264 return agent_host_->Activate(); |
267 } | 265 } |
268 | 266 |
269 bool DevToolsTargetImpl::Close() const { | 267 bool DevToolsTargetImpl::Close() const { |
270 return false; | 268 return agent_host_->Close(); |
271 } | 269 } |
272 | 270 |
273 int DevToolsTargetImpl::GetTabId() const { | 271 int DevToolsTargetImpl::GetTabId() const { |
274 return -1; | 272 return -1; |
275 } | 273 } |
276 | 274 |
277 WebContents* DevToolsTargetImpl::GetWebContents() const { | 275 WebContents* DevToolsTargetImpl::GetWebContents() const { |
278 return NULL; | 276 return NULL; |
279 } | 277 } |
280 | 278 |
(...skipping 16 matching lines...) Expand all Loading... | |
297 } | 295 } |
298 | 296 |
299 // static | 297 // static |
300 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateWebContentsTargets() { | 298 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateWebContentsTargets() { |
301 std::set<WebContents*> tab_web_contents; | 299 std::set<WebContents*> tab_web_contents; |
302 for (TabContentsIterator it; !it.done(); it.Next()) | 300 for (TabContentsIterator it; !it.done(); it.Next()) |
303 tab_web_contents.insert(*it); | 301 tab_web_contents.insert(*it); |
304 | 302 |
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
306 DevToolsTargetImpl::List result; | 304 DevToolsTargetImpl::List result; |
307 std::vector<WebContents*> wc_list = | 305 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
308 content::DevToolsAgentHost::GetInspectableWebContents(); | 306 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
309 for (std::vector<WebContents*>::iterator it = wc_list.begin(); | 307 it != agents.end(); ++it) { |
310 it != wc_list.end(); | 308 if (WebContents* web_contents = (*it)->GetWebContents()) { |
311 ++it) { | 309 bool is_tab = |
312 bool is_tab = tab_web_contents.find(*it) != tab_web_contents.end(); | 310 tab_web_contents.find(web_contents) != tab_web_contents.end(); |
313 result.push_back(new WebContentsTarget(*it, is_tab)); | 311 result.push_back(new WebContentsTarget(web_contents, is_tab)); |
312 } | |
314 } | 313 } |
315 return result; | 314 return result; |
316 } | 315 } |
317 | 316 |
318 static void CreateWorkerTargets( | 317 static void CreateWorkerTargets( |
319 const std::vector<WorkerService::WorkerInfo>& worker_info, | 318 const std::vector<WorkerService::WorkerInfo>& worker_info, |
320 DevToolsTargetImpl::Callback callback) { | 319 DevToolsTargetImpl::Callback callback) { |
321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
322 DevToolsTargetImpl::List result; | 321 DevToolsTargetImpl::List result; |
323 for (size_t i = 0; i < worker_info.size(); ++i) { | 322 for (size_t i = 0; i < worker_info.size(); ++i) { |
324 result.push_back(new WorkerTarget(worker_info[i])); | 323 result.push_back(new WorkerTarget(worker_info[i])); |
325 } | 324 } |
326 callback.Run(result); | 325 callback.Run(result); |
327 } | 326 } |
328 | 327 |
329 // static | 328 // static |
330 void DevToolsTargetImpl::EnumerateWorkerTargets(Callback callback) { | 329 void DevToolsTargetImpl::EnumerateWorkerTargets(Callback callback) { |
dgozman
2014/08/21 11:50:24
We should remove this method and it's usages in a
vkuzkokov
2014/08/21 14:17:52
Acknowledged.
| |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
332 content::BrowserThread::PostTask( | 331 content::BrowserThread::PostTask( |
333 content::BrowserThread::UI, | 332 content::BrowserThread::UI, |
334 FROM_HERE, | 333 FROM_HERE, |
335 base::Bind(&CreateWorkerTargets, | 334 base::Bind(&CreateWorkerTargets, |
336 WorkerService::GetInstance()->GetWorkers(), | 335 WorkerService::GetInstance()->GetWorkers(), |
337 callback)); | 336 callback)); |
338 } | 337 } |
339 | 338 |
340 static void CollectAllTargets( | 339 static void CollectAllTargets( |
341 DevToolsTargetImpl::Callback callback, | 340 DevToolsTargetImpl::Callback callback, |
342 const DevToolsTargetImpl::List& worker_targets) { | 341 const DevToolsTargetImpl::List& worker_targets) { |
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
344 DevToolsTargetImpl::List result = | 343 DevToolsTargetImpl::List result = |
345 DevToolsTargetImpl::EnumerateWebContentsTargets(); | 344 DevToolsTargetImpl::EnumerateWebContentsTargets(); |
346 result.insert(result.begin(), worker_targets.begin(), worker_targets.end()); | 345 result.insert(result.end(), worker_targets.begin(), worker_targets.end()); |
346 | |
347 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | |
348 for (DevToolsAgentHost::List::iterator it = agents.begin(); | |
349 it != agents.end(); ++it) { | |
350 if ((*it)->GetType() == DevToolsAgentHost::kTypeServiceWorker) | |
351 result.push_back(new ServiceWorkerTarget(*it)); | |
352 } | |
347 callback.Run(result); | 353 callback.Run(result); |
348 } | 354 } |
349 | 355 |
350 // static | 356 // static |
351 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { | 357 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { |
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
353 content::BrowserThread::PostTask( | 359 content::BrowserThread::PostTask( |
354 content::BrowserThread::IO, | 360 content::BrowserThread::IO, |
355 FROM_HERE, | 361 FROM_HERE, |
356 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, | 362 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, |
357 base::Bind(&CollectAllTargets, callback))); | 363 base::Bind(&CollectAllTargets, callback))); |
358 } | 364 } |
OLD | NEW |