| 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 23 matching lines...) Expand all Loading... |
| 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 | 43 |
| 44 // RenderViewHostTarget -------------------------------------------------------- | 44 // WebContentsTarget -------------------------------------------------------- |
| 45 | 45 |
| 46 class RenderViewHostTarget : public DevToolsTargetImpl { | 46 class WebContentsTarget : public DevToolsTargetImpl { |
| 47 public: | 47 public: |
| 48 explicit RenderViewHostTarget(RenderViewHost* rvh, bool is_tab); | 48 WebContentsTarget(WebContents* web_contents, bool is_tab); |
| 49 | 49 |
| 50 // DevToolsTargetImpl overrides: | 50 // DevToolsTargetImpl overrides: |
| 51 virtual bool Activate() const OVERRIDE; | 51 virtual bool Activate() const OVERRIDE; |
| 52 virtual bool Close() const OVERRIDE; | 52 virtual bool Close() const OVERRIDE; |
| 53 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; | 53 virtual WebContents* GetWebContents() const OVERRIDE; |
| 54 virtual int GetTabId() const OVERRIDE; | 54 virtual int GetTabId() const OVERRIDE; |
| 55 virtual std::string GetExtensionId() const OVERRIDE; | 55 virtual std::string GetExtensionId() const OVERRIDE; |
| 56 virtual void Inspect(Profile* profile) const OVERRIDE; | 56 virtual void Inspect(Profile* profile) const OVERRIDE; |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 int tab_id_; | 59 int tab_id_; |
| 60 std::string extension_id_; | 60 std::string extension_id_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 RenderViewHostTarget::RenderViewHostTarget(RenderViewHost* rvh, bool is_tab) | 63 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) |
| 64 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(rvh)), | 64 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), |
| 65 tab_id_(-1) { | 65 tab_id_(-1) { |
| 66 set_type(kTargetTypeOther); | 66 set_type(kTargetTypeOther); |
| 67 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | |
| 68 if (!web_contents) | |
| 69 return; // Orphan RVH will show up with no title/url/icon in clients. | |
| 70 | 67 |
| 71 content::RenderFrameHost* rfh = rvh->GetMainFrame(); | 68 content::RenderFrameHost* rfh = |
| 69 web_contents->GetRenderViewHost()->GetMainFrame(); |
| 72 if (rfh->IsCrossProcessSubframe()) { | 70 if (rfh->IsCrossProcessSubframe()) { |
| 73 set_url(rfh->GetLastCommittedURL()); | 71 set_url(rfh->GetLastCommittedURL()); |
| 74 set_type(kTargetTypeIFrame); | 72 set_type(kTargetTypeIFrame); |
| 75 // TODO(kaznacheev) Try setting the title when the frame navigation | 73 // TODO(pfeldman) Update for out of process iframes. |
| 76 // refactoring is done. | |
| 77 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); | 74 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost(); |
| 78 set_parent_id(DevToolsAgentHost::GetOrCreateFor(parent_rvh)->GetId()); | 75 set_parent_id(DevToolsAgentHost::GetOrCreateFor( |
| 76 WebContents::FromRenderViewHost(parent_rvh))->GetId()); |
| 79 return; | 77 return; |
| 80 } | 78 } |
| 81 | 79 |
| 82 set_title(base::UTF16ToUTF8(web_contents->GetTitle())); | 80 set_title(base::UTF16ToUTF8(web_contents->GetTitle())); |
| 83 set_url(web_contents->GetURL()); | 81 set_url(web_contents->GetURL()); |
| 84 content::NavigationController& controller = web_contents->GetController(); | 82 content::NavigationController& controller = web_contents->GetController(); |
| 85 content::NavigationEntry* entry = controller.GetActiveEntry(); | 83 content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 86 if (entry != NULL && entry->GetURL().is_valid()) | 84 if (entry != NULL && entry->GetURL().is_valid()) |
| 87 set_favicon_url(entry->GetFavicon().url); | 85 set_favicon_url(entry->GetFavicon().url); |
| 88 set_last_activity_time(web_contents->GetLastActiveTime()); | 86 set_last_activity_time(web_contents->GetLastActiveTime()); |
| 89 | 87 |
| 90 GuestViewBase* guest = GuestViewBase::FromWebContents(web_contents); | 88 GuestViewBase* guest = GuestViewBase::FromWebContents(web_contents); |
| 91 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; | 89 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; |
| 92 RenderViewHost* guest_parent_rvh = | 90 if (guest_contents) { |
| 93 guest_contents ? guest_contents->GetRenderViewHost() : NULL; | |
| 94 if (guest_parent_rvh) { | |
| 95 set_type(kTargetTypeWebView); | 91 set_type(kTargetTypeWebView); |
| 96 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_parent_rvh)->GetId()); | 92 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents)->GetId()); |
| 97 return; | 93 return; |
| 98 } | 94 } |
| 99 | 95 |
| 100 if (is_tab) { | 96 if (is_tab) { |
| 101 set_type(kTargetTypePage); | 97 set_type(kTargetTypePage); |
| 102 tab_id_ = extensions::ExtensionTabUtil::GetTabId(web_contents); | 98 tab_id_ = extensions::ExtensionTabUtil::GetTabId(web_contents); |
| 103 return; | 99 return; |
| 104 } | 100 } |
| 105 | 101 |
| 106 const extensions::Extension* extension = extensions::ExtensionRegistry::Get( | 102 const extensions::Extension* extension = extensions::ExtensionRegistry::Get( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 126 } else if (extension->is_hosted_app() | 122 } else if (extension->is_hosted_app() |
| 127 || extension->is_legacy_packaged_app() | 123 || extension->is_legacy_packaged_app() |
| 128 || extension->is_platform_app()) { | 124 || extension->is_platform_app()) { |
| 129 set_type(kTargetTypeApp); | 125 set_type(kTargetTypeApp); |
| 130 } | 126 } |
| 131 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( | 127 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( |
| 132 extension, extension_misc::EXTENSION_ICON_SMALLISH, | 128 extension, extension_misc::EXTENSION_ICON_SMALLISH, |
| 133 ExtensionIconSet::MATCH_BIGGER, false, NULL)); | 129 ExtensionIconSet::MATCH_BIGGER, false, NULL)); |
| 134 } | 130 } |
| 135 | 131 |
| 136 bool RenderViewHostTarget::Activate() const { | 132 bool WebContentsTarget::Activate() const { |
| 137 RenderViewHost* rvh = GetRenderViewHost(); | 133 WebContents* web_contents = GetWebContents(); |
| 138 if (!rvh) | |
| 139 return false; | |
| 140 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); | |
| 141 if (!web_contents) | 134 if (!web_contents) |
| 142 return false; | 135 return false; |
| 143 web_contents->GetDelegate()->ActivateContents(web_contents); | 136 web_contents->GetDelegate()->ActivateContents(web_contents); |
| 144 return true; | 137 return true; |
| 145 } | 138 } |
| 146 | 139 |
| 147 bool RenderViewHostTarget::Close() const { | 140 bool WebContentsTarget::Close() const { |
| 148 RenderViewHost* rvh = GetRenderViewHost(); | 141 WebContents* web_contents = GetWebContents(); |
| 149 if (!rvh) | 142 if (!web_contents) |
| 150 return false; | 143 return false; |
| 151 rvh->ClosePage(); | 144 web_contents->GetRenderViewHost()->ClosePage(); |
| 152 return true; | 145 return true; |
| 153 } | 146 } |
| 154 | 147 |
| 155 RenderViewHost* RenderViewHostTarget::GetRenderViewHost() const { | 148 WebContents* WebContentsTarget::GetWebContents() const { |
| 156 return GetAgentHost()->GetRenderViewHost(); | 149 return GetAgentHost()->GetWebContents(); |
| 157 } | 150 } |
| 158 | 151 |
| 159 int RenderViewHostTarget::GetTabId() const { | 152 int WebContentsTarget::GetTabId() const { |
| 160 return tab_id_; | 153 return tab_id_; |
| 161 } | 154 } |
| 162 | 155 |
| 163 std::string RenderViewHostTarget::GetExtensionId() const { | 156 std::string WebContentsTarget::GetExtensionId() const { |
| 164 return extension_id_; | 157 return extension_id_; |
| 165 } | 158 } |
| 166 | 159 |
| 167 void RenderViewHostTarget::Inspect(Profile* profile) const { | 160 void WebContentsTarget::Inspect(Profile* profile) const { |
| 168 RenderViewHost* rvh = GetRenderViewHost(); | 161 WebContents* web_contents = GetWebContents(); |
| 169 if (!rvh) | 162 if (!web_contents) |
| 170 return; | 163 return; |
| 171 DevToolsWindow::OpenDevToolsWindow(rvh); | 164 DevToolsWindow::OpenDevToolsWindow(web_contents); |
| 172 } | 165 } |
| 173 | 166 |
| 174 // WorkerTarget ---------------------------------------------------------------- | 167 // WorkerTarget ---------------------------------------------------------------- |
| 175 | 168 |
| 176 class WorkerTarget : public DevToolsTargetImpl { | 169 class WorkerTarget : public DevToolsTargetImpl { |
| 177 public: | 170 public: |
| 178 explicit WorkerTarget(const WorkerService::WorkerInfo& worker_info); | 171 explicit WorkerTarget(const WorkerService::WorkerInfo& worker_info); |
| 179 | 172 |
| 180 // content::DevToolsTarget overrides: | 173 // content::DevToolsTarget overrides: |
| 181 virtual bool Close() const OVERRIDE; | 174 virtual bool Close() const OVERRIDE; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 } | 266 } |
| 274 | 267 |
| 275 bool DevToolsTargetImpl::Close() const { | 268 bool DevToolsTargetImpl::Close() const { |
| 276 return false; | 269 return false; |
| 277 } | 270 } |
| 278 | 271 |
| 279 int DevToolsTargetImpl::GetTabId() const { | 272 int DevToolsTargetImpl::GetTabId() const { |
| 280 return -1; | 273 return -1; |
| 281 } | 274 } |
| 282 | 275 |
| 283 RenderViewHost* DevToolsTargetImpl::GetRenderViewHost() const { | 276 WebContents* DevToolsTargetImpl::GetWebContents() const { |
| 284 return NULL; | 277 return NULL; |
| 285 } | 278 } |
| 286 | 279 |
| 287 std::string DevToolsTargetImpl::GetExtensionId() const { | 280 std::string DevToolsTargetImpl::GetExtensionId() const { |
| 288 return std::string(); | 281 return std::string(); |
| 289 } | 282 } |
| 290 | 283 |
| 291 void DevToolsTargetImpl::Inspect(Profile* /*profile*/) const { | 284 void DevToolsTargetImpl::Inspect(Profile* /*profile*/) const { |
| 292 } | 285 } |
| 293 | 286 |
| 294 void DevToolsTargetImpl::Reload() const { | 287 void DevToolsTargetImpl::Reload() const { |
| 295 } | 288 } |
| 296 | 289 |
| 297 // static | 290 // static |
| 298 scoped_ptr<DevToolsTargetImpl> DevToolsTargetImpl::CreateForRenderViewHost( | 291 scoped_ptr<DevToolsTargetImpl> DevToolsTargetImpl::CreateForWebContents( |
| 299 content::RenderViewHost* rvh, bool is_tab) { | 292 content::WebContents* web_contents, |
| 300 return scoped_ptr<DevToolsTargetImpl>(new RenderViewHostTarget(rvh, is_tab)); | 293 bool is_tab) { |
| 294 return scoped_ptr<DevToolsTargetImpl>( |
| 295 new WebContentsTarget(web_contents, is_tab)); |
| 301 } | 296 } |
| 302 | 297 |
| 303 // static | 298 // static |
| 304 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateRenderViewHostTargets() { | 299 DevToolsTargetImpl::List DevToolsTargetImpl::EnumerateWebContentsTargets() { |
| 305 std::set<RenderViewHost*> tab_rvhs; | 300 std::set<WebContents*> tab_web_contents; |
| 306 for (TabContentsIterator it; !it.done(); it.Next()) | 301 for (TabContentsIterator it; !it.done(); it.Next()) |
| 307 tab_rvhs.insert(it->GetRenderViewHost()); | 302 tab_web_contents.insert(*it); |
| 308 | 303 |
| 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 310 DevToolsTargetImpl::List result; | 305 DevToolsTargetImpl::List result; |
| 311 std::vector<RenderViewHost*> rvh_list = | 306 std::vector<WebContents*> wc_list = |
| 312 content::DevToolsAgentHost::GetValidRenderViewHosts(); | 307 content::DevToolsAgentHost::GetInspectableWebContents(); |
| 313 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); | 308 for (std::vector<WebContents*>::iterator it = wc_list.begin(); |
| 314 it != rvh_list.end(); ++it) { | 309 it != wc_list.end(); |
| 315 bool is_tab = tab_rvhs.find(*it) != tab_rvhs.end(); | 310 ++it) { |
| 316 result.push_back(new RenderViewHostTarget(*it, is_tab)); | 311 bool is_tab = tab_web_contents.find(*it) != tab_web_contents.end(); |
| 312 result.push_back(new WebContentsTarget(*it, is_tab)); |
| 317 } | 313 } |
| 318 return result; | 314 return result; |
| 319 } | 315 } |
| 320 | 316 |
| 321 static void CreateWorkerTargets( | 317 static void CreateWorkerTargets( |
| 322 const std::vector<WorkerService::WorkerInfo>& worker_info, | 318 const std::vector<WorkerService::WorkerInfo>& worker_info, |
| 323 DevToolsTargetImpl::Callback callback) { | 319 DevToolsTargetImpl::Callback callback) { |
| 324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 325 DevToolsTargetImpl::List result; | 321 DevToolsTargetImpl::List result; |
| 326 for (size_t i = 0; i < worker_info.size(); ++i) { | 322 for (size_t i = 0; i < worker_info.size(); ++i) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 338 base::Bind(&CreateWorkerTargets, | 334 base::Bind(&CreateWorkerTargets, |
| 339 WorkerService::GetInstance()->GetWorkers(), | 335 WorkerService::GetInstance()->GetWorkers(), |
| 340 callback)); | 336 callback)); |
| 341 } | 337 } |
| 342 | 338 |
| 343 static void CollectAllTargets( | 339 static void CollectAllTargets( |
| 344 DevToolsTargetImpl::Callback callback, | 340 DevToolsTargetImpl::Callback callback, |
| 345 const DevToolsTargetImpl::List& worker_targets) { | 341 const DevToolsTargetImpl::List& worker_targets) { |
| 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 347 DevToolsTargetImpl::List result = | 343 DevToolsTargetImpl::List result = |
| 348 DevToolsTargetImpl::EnumerateRenderViewHostTargets(); | 344 DevToolsTargetImpl::EnumerateWebContentsTargets(); |
| 349 result.insert(result.begin(), worker_targets.begin(), worker_targets.end()); | 345 result.insert(result.begin(), worker_targets.begin(), worker_targets.end()); |
| 350 callback.Run(result); | 346 callback.Run(result); |
| 351 } | 347 } |
| 352 | 348 |
| 353 // static | 349 // static |
| 354 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { | 350 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) { |
| 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 351 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 356 content::BrowserThread::PostTask( | 352 content::BrowserThread::PostTask( |
| 357 content::BrowserThread::IO, | 353 content::BrowserThread::IO, |
| 358 FROM_HERE, | 354 FROM_HERE, |
| 359 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, | 355 base::Bind(&DevToolsTargetImpl::EnumerateWorkerTargets, |
| 360 base::Bind(&CollectAllTargets, callback))); | 356 base::Bind(&CollectAllTargets, callback))); |
| 361 } | 357 } |
| OLD | NEW |