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 22 matching lines...) Expand all Loading... |
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"; | 39 const char kTargetTypeWorker[] = "worker"; |
40 const char kTargetTypeWebView[] = "webview"; | 40 const char kTargetTypeWebView[] = "webview"; |
41 const char kTargetTypeIFrame[] = "iframe"; | 41 const char kTargetTypeIFrame[] = "iframe"; |
42 const char kTargetTypeOther[] = "other"; | 42 const char kTargetTypeOther[] = "other"; |
| 43 const char kTargetTypeServiceWorker[] = "service_worker"; |
43 | 44 |
44 // WebContentsTarget -------------------------------------------------------- | 45 // WebContentsTarget -------------------------------------------------------- |
45 | 46 |
46 class WebContentsTarget : public DevToolsTargetImpl { | 47 class WebContentsTarget : public DevToolsTargetImpl { |
47 public: | 48 public: |
48 WebContentsTarget(WebContents* web_contents, bool is_tab); | 49 WebContentsTarget(WebContents* web_contents, bool is_tab); |
49 | 50 |
50 // DevToolsTargetImpl overrides: | 51 // DevToolsTargetImpl overrides: |
51 virtual bool Activate() const OVERRIDE; | |
52 virtual bool Close() const OVERRIDE; | |
53 virtual WebContents* GetWebContents() const OVERRIDE; | 52 virtual WebContents* GetWebContents() const OVERRIDE; |
54 virtual int GetTabId() const OVERRIDE; | 53 virtual int GetTabId() const OVERRIDE; |
55 virtual std::string GetExtensionId() const OVERRIDE; | 54 virtual std::string GetExtensionId() const OVERRIDE; |
56 virtual void Inspect(Profile* profile) const OVERRIDE; | 55 virtual void Inspect(Profile* profile) const OVERRIDE; |
57 | 56 |
58 private: | 57 private: |
59 int tab_id_; | 58 int tab_id_; |
60 std::string extension_id_; | 59 std::string extension_id_; |
61 }; | 60 }; |
62 | 61 |
63 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) | 62 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) |
64 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), | 63 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), |
65 tab_id_(-1) { | 64 tab_id_(-1) { |
66 set_type(kTargetTypeOther); | 65 set_type(kTargetTypeOther); |
67 | 66 |
68 content::RenderFrameHost* rfh = | 67 content::RenderFrameHost* rfh = |
69 web_contents->GetRenderViewHost()->GetMainFrame(); | 68 web_contents->GetRenderViewHost()->GetMainFrame(); |
70 if (rfh->IsCrossProcessSubframe()) { | 69 if (rfh->IsCrossProcessSubframe()) { |
71 set_url(rfh->GetLastCommittedURL()); | 70 set_url(rfh->GetLastCommittedURL()); |
72 set_type(kTargetTypeIFrame); | 71 set_type(kTargetTypeIFrame); |
73 // TODO(pfeldman) Update for out of process iframes. | 72 // TODO(pfeldman) Update for out of process iframes. |
74 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); | 73 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); |
75 set_parent_id(DevToolsAgentHost::GetOrCreateFor( | 74 set_parent_id(DevToolsAgentHost::GetOrCreateFor( |
76 WebContents::FromRenderViewHost(parent_rvh))->GetId()); | 75 WebContents::FromRenderViewHost(parent_rvh))->GetId()); |
77 return; | 76 return; |
78 } | 77 } |
79 | 78 |
80 set_title(base::UTF16ToUTF8(web_contents->GetTitle())); | |
81 set_url(web_contents->GetURL()); | |
82 content::NavigationController& controller = web_contents->GetController(); | 79 content::NavigationController& controller = web_contents->GetController(); |
83 content::NavigationEntry* entry = controller.GetActiveEntry(); | 80 content::NavigationEntry* entry = controller.GetActiveEntry(); |
84 if (entry != NULL && entry->GetURL().is_valid()) | 81 if (entry != NULL && entry->GetURL().is_valid()) |
85 set_favicon_url(entry->GetFavicon().url); | 82 set_favicon_url(entry->GetFavicon().url); |
86 set_last_activity_time(web_contents->GetLastActiveTime()); | 83 set_last_activity_time(web_contents->GetLastActiveTime()); |
87 | 84 |
88 extensions::GuestViewBase* guest = | 85 extensions::GuestViewBase* guest = |
89 extensions::GuestViewBase::FromWebContents(web_contents); | 86 extensions::GuestViewBase::FromWebContents(web_contents); |
90 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; | 87 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; |
91 if (guest_contents) { | 88 if (guest_contents) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } else if (extension->is_hosted_app() | 120 } else if (extension->is_hosted_app() |
124 || extension->is_legacy_packaged_app() | 121 || extension->is_legacy_packaged_app() |
125 || extension->is_platform_app()) { | 122 || extension->is_platform_app()) { |
126 set_type(kTargetTypeApp); | 123 set_type(kTargetTypeApp); |
127 } | 124 } |
128 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( | 125 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( |
129 extension, extension_misc::EXTENSION_ICON_SMALLISH, | 126 extension, extension_misc::EXTENSION_ICON_SMALLISH, |
130 ExtensionIconSet::MATCH_BIGGER, false, NULL)); | 127 ExtensionIconSet::MATCH_BIGGER, false, NULL)); |
131 } | 128 } |
132 | 129 |
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 { | 130 WebContents* WebContentsTarget::GetWebContents() const { |
150 return GetAgentHost()->GetWebContents(); | 131 return GetAgentHost()->GetWebContents(); |
151 } | 132 } |
152 | 133 |
153 int WebContentsTarget::GetTabId() const { | 134 int WebContentsTarget::GetTabId() const { |
154 return tab_id_; | 135 return tab_id_; |
155 } | 136 } |
156 | 137 |
157 std::string WebContentsTarget::GetExtensionId() const { | 138 std::string WebContentsTarget::GetExtensionId() const { |
158 return extension_id_; | 139 return extension_id_; |
159 } | 140 } |
160 | 141 |
161 void WebContentsTarget::Inspect(Profile* profile) const { | 142 void WebContentsTarget::Inspect(Profile* profile) const { |
162 WebContents* web_contents = GetWebContents(); | 143 WebContents* web_contents = GetWebContents(); |
163 if (!web_contents) | 144 if (!web_contents) |
164 return; | 145 return; |
165 DevToolsWindow::OpenDevToolsWindow(web_contents); | 146 DevToolsWindow::OpenDevToolsWindow(web_contents); |
166 } | 147 } |
167 | 148 |
168 // WorkerTarget ---------------------------------------------------------------- | 149 // WorkerTarget ---------------------------------------------------------------- |
169 | 150 |
170 class WorkerTarget : public DevToolsTargetImpl { | 151 class WorkerTarget : public DevToolsTargetImpl { |
171 public: | 152 public: |
172 explicit WorkerTarget(const WorkerService::WorkerInfo& worker_info); | 153 explicit WorkerTarget(const WorkerService::WorkerInfo& worker_info); |
173 | 154 |
174 // content::DevToolsTarget overrides: | 155 explicit WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host); |
175 virtual bool Close() const OVERRIDE; | |
176 | 156 |
177 // DevToolsTargetImpl overrides: | 157 // DevToolsTargetImpl overrides: |
| 158 virtual std::string GetType() const OVERRIDE; |
178 virtual void Inspect(Profile* profile) const OVERRIDE; | 159 virtual void Inspect(Profile* profile) const OVERRIDE; |
179 | |
180 private: | |
181 int process_id_; | |
182 int route_id_; | |
183 }; | 160 }; |
184 | 161 |
185 WorkerTarget::WorkerTarget(const WorkerService::WorkerInfo& worker) | 162 WorkerTarget::WorkerTarget(const WorkerService::WorkerInfo& worker) |
186 : DevToolsTargetImpl(DevToolsAgentHost::GetForWorker(worker.process_id, | 163 : DevToolsTargetImpl(DevToolsAgentHost::GetForWorker(worker.process_id, |
187 worker.route_id)) { | 164 worker.route_id)) { |
188 set_type(kTargetTypeWorker); | |
189 set_title(base::UTF16ToUTF8(worker.name)); | |
190 set_description(base::StringPrintf("Worker pid:%d", | 165 set_description(base::StringPrintf("Worker pid:%d", |
191 base::GetProcId(worker.handle))); | 166 base::GetProcId(worker.handle))); |
192 set_url(worker.url); | |
193 | |
194 process_id_ = worker.process_id; | |
195 route_id_ = worker.route_id; | |
196 } | 167 } |
197 | 168 |
198 static void TerminateWorker(int process_id, int route_id) { | 169 WorkerTarget::WorkerTarget( |
199 WorkerService::GetInstance()->TerminateWorker(process_id, route_id); | 170 scoped_refptr<DevToolsAgentHost> agent_host) |
| 171 : DevToolsTargetImpl(agent_host) { |
200 } | 172 } |
201 | 173 |
202 bool WorkerTarget::Close() const { | 174 std::string WorkerTarget::GetType() const { |
203 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 175 switch (GetAgentHost()->GetType()) { |
204 base::Bind(&TerminateWorker, process_id_, route_id_)); | 176 case DevToolsAgentHost::TYPE_SHARED_WORKER: |
205 return true; | 177 return kTargetTypeWorker; |
| 178 case DevToolsAgentHost::TYPE_SERVICE_WORKER: |
| 179 return kTargetTypeServiceWorker; |
| 180 default: |
| 181 break; |
| 182 } |
| 183 return kTargetTypeOther; |
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 |
214 // DevToolsTargetImpl ---------------------------------------------------------- | 192 // DevToolsTargetImpl ---------------------------------------------------------- |
215 | 193 |
216 DevToolsTargetImpl::~DevToolsTargetImpl() { | 194 DevToolsTargetImpl::~DevToolsTargetImpl() { |
217 } | 195 } |
218 | 196 |
219 DevToolsTargetImpl::DevToolsTargetImpl( | 197 DevToolsTargetImpl::DevToolsTargetImpl( |
220 scoped_refptr<DevToolsAgentHost> agent_host) | 198 scoped_refptr<DevToolsAgentHost> agent_host) |
221 : agent_host_(agent_host) { | 199 : agent_host_(agent_host), |
| 200 title_(agent_host->GetTitle()), |
| 201 url_(agent_host->GetURL()) { |
222 } | 202 } |
223 | 203 |
224 std::string DevToolsTargetImpl::GetParentId() const { | 204 std::string DevToolsTargetImpl::GetParentId() const { |
225 return parent_id_; | 205 return parent_id_; |
226 } | 206 } |
227 | 207 |
228 std::string DevToolsTargetImpl::GetId() const { | 208 std::string DevToolsTargetImpl::GetId() const { |
229 return agent_host_->GetId(); | 209 return agent_host_->GetId(); |
230 } | 210 } |
231 | 211 |
(...skipping 24 matching lines...) Expand all Loading... |
256 scoped_refptr<content::DevToolsAgentHost> | 236 scoped_refptr<content::DevToolsAgentHost> |
257 DevToolsTargetImpl::GetAgentHost() const { | 237 DevToolsTargetImpl::GetAgentHost() const { |
258 return agent_host_; | 238 return agent_host_; |
259 } | 239 } |
260 | 240 |
261 bool DevToolsTargetImpl::IsAttached() const { | 241 bool DevToolsTargetImpl::IsAttached() const { |
262 return agent_host_->IsAttached(); | 242 return agent_host_->IsAttached(); |
263 } | 243 } |
264 | 244 |
265 bool DevToolsTargetImpl::Activate() const { | 245 bool DevToolsTargetImpl::Activate() const { |
266 return false; | 246 return agent_host_->Activate(); |
267 } | 247 } |
268 | 248 |
269 bool DevToolsTargetImpl::Close() const { | 249 bool DevToolsTargetImpl::Close() const { |
270 return false; | 250 return agent_host_->Close(); |
271 } | 251 } |
272 | 252 |
273 int DevToolsTargetImpl::GetTabId() const { | 253 int DevToolsTargetImpl::GetTabId() const { |
274 return -1; | 254 return -1; |
275 } | 255 } |
276 | 256 |
277 WebContents* DevToolsTargetImpl::GetWebContents() const { | 257 WebContents* DevToolsTargetImpl::GetWebContents() const { |
278 return NULL; | 258 return NULL; |
279 } | 259 } |
280 | 260 |
(...skipping 16 matching lines...) Expand all Loading... |
297 } | 277 } |
298 | 278 |
299 // static | 279 // static |
300 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateWebContentsTargets() { | 280 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateWebContentsTargets() { |
301 std::set<WebContents*> tab_web_contents; | 281 std::set<WebContents*> tab_web_contents; |
302 for (TabContentsIterator it; !it.done(); it.Next()) | 282 for (TabContentsIterator it; !it.done(); it.Next()) |
303 tab_web_contents.insert(*it); | 283 tab_web_contents.insert(*it); |
304 | 284 |
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
306 DevToolsTargetImpl::List result; | 286 DevToolsTargetImpl::List result; |
307 std::vector<WebContents*> wc_list = | 287 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
308 content::DevToolsAgentHost::GetInspectableWebContents(); | 288 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
309 for (std::vector<WebContents*>::iterator it = wc_list.begin(); | 289 it != agents.end(); ++it) { |
310 it != wc_list.end(); | 290 if (WebContents* web_contents = (*it)->GetWebContents()) { |
311 ++it) { | 291 bool is_tab = |
312 bool is_tab = tab_web_contents.find(*it) != tab_web_contents.end(); | 292 tab_web_contents.find(web_contents) != tab_web_contents.end(); |
313 result.push_back(new WebContentsTarget(*it, is_tab)); | 293 result.push_back(new WebContentsTarget(web_contents, is_tab)); |
| 294 } |
314 } | 295 } |
315 return result; | 296 return result; |
316 } | 297 } |
317 | 298 |
318 static void CreateWorkerTargets( | 299 static void CreateWorkerTargets( |
319 const std::vector<WorkerService::WorkerInfo>& worker_info, | 300 const std::vector<WorkerService::WorkerInfo>& worker_info, |
320 DevToolsTargetImpl::Callback callback) { | 301 DevToolsTargetImpl::Callback callback) { |
321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
322 DevToolsTargetImpl::List result; | 303 DevToolsTargetImpl::List result; |
323 for (size_t i = 0; i < worker_info.size(); ++i) { | 304 for (size_t i = 0; i < worker_info.size(); ++i) { |
(...skipping 12 matching lines...) Expand all Loading... |
336 WorkerService::GetInstance()->GetWorkers(), | 317 WorkerService::GetInstance()->GetWorkers(), |
337 callback)); | 318 callback)); |
338 } | 319 } |
339 | 320 |
340 static void CollectAllTargets( | 321 static void CollectAllTargets( |
341 DevToolsTargetImpl::Callback callback, | 322 DevToolsTargetImpl::Callback callback, |
342 const DevToolsTargetImpl::List& worker_targets) { | 323 const DevToolsTargetImpl::List& worker_targets) { |
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
344 DevToolsTargetImpl::List result = | 325 DevToolsTargetImpl::List result = |
345 DevToolsTargetImpl::EnumerateWebContentsTargets(); | 326 DevToolsTargetImpl::EnumerateWebContentsTargets(); |
346 result.insert(result.begin(), worker_targets.begin(), worker_targets.end()); | 327 result.insert(result.end(), worker_targets.begin(), worker_targets.end()); |
| 328 |
| 329 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
| 330 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
| 331 it != agents.end(); ++it) { |
| 332 if ((*it)->GetType() == DevToolsAgentHost::TYPE_SERVICE_WORKER) |
| 333 result.push_back(new WorkerTarget(*it)); |
| 334 } |
347 callback.Run(result); | 335 callback.Run(result); |
348 } | 336 } |
349 | 337 |
350 // static | 338 // static |
351 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { | 339 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { |
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
353 content::BrowserThread::PostTask( | 341 content::BrowserThread::PostTask( |
354 content::BrowserThread::IO, | 342 content::BrowserThread::IO, |
355 FROM_HERE, | 343 FROM_HERE, |
356 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, | 344 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, |
357 base::Bind(&CollectAllTargets, callback))); | 345 base::Bind(&CollectAllTargets, callback))); |
358 } | 346 } |
OLD | NEW |