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