| 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 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "extensions/browser/extension_host.h" | 22 #include "extensions/browser/extension_host.h" |
| 23 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/browser/process_manager.h" | 24 #include "extensions/browser/process_manager.h" |
| 25 #include "extensions/common/constants.h" | 25 #include "extensions/common/constants.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using content::DevToolsAgentHost; | 28 using content::DevToolsAgentHost; |
| 29 using content::WebContents; | 29 using content::WebContents; |
| 30 | 30 |
| 31 const char DevToolsTargetImpl::kTargetTypeApp[] = "app"; | |
| 32 const char DevToolsTargetImpl::kTargetTypeBackgroundPage[] = "background_page"; | |
| 33 const char DevToolsTargetImpl::kTargetTypePage[] = "page"; | |
| 34 const char DevToolsTargetImpl::kTargetTypeWorker[] = "worker"; | |
| 35 const char DevToolsTargetImpl::kTargetTypeWebView[] = "webview"; | |
| 36 const char DevToolsTargetImpl::kTargetTypeIFrame[] = "iframe"; | |
| 37 const char DevToolsTargetImpl::kTargetTypeNode[] = "node"; | |
| 38 const char DevToolsTargetImpl::kTargetTypeOther[] = "other"; | |
| 39 const char DevToolsTargetImpl::kTargetTypeServiceWorker[] = "service_worker"; | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 // WebContentsTarget -------------------------------------------------------- | |
| 44 | |
| 45 class WebContentsTarget : public DevToolsTargetImpl { | |
| 46 public: | |
| 47 WebContentsTarget(WebContents* web_contents, bool is_tab); | |
| 48 | |
| 49 // DevToolsTargetImpl overrides. | |
| 50 int GetTabId() const override; | |
| 51 std::string GetExtensionId() const override; | |
| 52 void Inspect(Profile* profile) const override; | |
| 53 | |
| 54 private: | |
| 55 int tab_id_; | |
| 56 std::string extension_id_; | |
| 57 }; | |
| 58 | |
| 59 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab) | |
| 60 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)), | |
| 61 tab_id_(-1) { | |
| 62 set_type(kTargetTypeOther); | |
| 63 | |
| 64 guest_view::GuestViewBase* guest = | |
| 65 guest_view::GuestViewBase::FromWebContents(web_contents); | |
| 66 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL; | |
| 67 if (guest_contents) { | |
| 68 set_type(kTargetTypeWebView); | |
| 69 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents)->GetId()); | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 if (is_tab) { | |
| 74 set_type(kTargetTypePage); | |
| 75 tab_id_ = extensions::ExtensionTabUtil::GetTabId(web_contents); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 const extensions::Extension* extension = extensions::ExtensionRegistry::Get( | |
| 80 web_contents->GetBrowserContext())->enabled_extensions().GetByID( | |
| 81 GetURL().host()); | |
| 82 if (!extension) | |
| 83 return; | |
| 84 | |
| 85 Profile* profile = | |
| 86 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 87 if (!profile) | |
| 88 return; | |
| 89 set_title(extension->name()); | |
| 90 extensions::ExtensionHost* extension_host = | |
| 91 extensions::ProcessManager::Get(profile) | |
| 92 ->GetBackgroundHostForExtension(extension->id()); | |
| 93 if (extension_host && | |
| 94 extension_host->host_contents() == web_contents) { | |
| 95 set_type(kTargetTypeBackgroundPage); | |
| 96 extension_id_ = extension->id(); | |
| 97 } else if (extension->is_hosted_app() | |
| 98 || extension->is_legacy_packaged_app() | |
| 99 || extension->is_platform_app()) { | |
| 100 set_type(kTargetTypeApp); | |
| 101 } | |
| 102 set_favicon_url(extensions::ExtensionIconSource::GetIconURL( | |
| 103 extension, extension_misc::EXTENSION_ICON_SMALLISH, | |
| 104 ExtensionIconSet::MATCH_BIGGER, false, NULL)); | |
| 105 } | |
| 106 | |
| 107 int WebContentsTarget::GetTabId() const { | |
| 108 return tab_id_; | |
| 109 } | |
| 110 | |
| 111 std::string WebContentsTarget::GetExtensionId() const { | |
| 112 return extension_id_; | |
| 113 } | |
| 114 | |
| 115 void WebContentsTarget::Inspect(Profile* profile) const { | |
| 116 WebContents* web_contents = GetWebContents(); | |
| 117 if (!web_contents) | |
| 118 return; | |
| 119 DevToolsWindow::OpenDevToolsWindow(web_contents); | |
| 120 } | |
| 121 | |
| 122 // FrameTarget ---------------------------------------------------------------- | |
| 123 | |
| 124 class FrameTarget : public DevToolsTargetImpl { | |
| 125 public: | |
| 126 explicit FrameTarget(scoped_refptr<DevToolsAgentHost> agent_host); | |
| 127 | |
| 128 // DevToolsTargetImpl overrides: | |
| 129 void Inspect(Profile* profile) const override; | |
| 130 }; | |
| 131 | |
| 132 FrameTarget::FrameTarget(scoped_refptr<DevToolsAgentHost> agent_host) | |
| 133 : DevToolsTargetImpl(agent_host) { | |
| 134 set_type(kTargetTypeIFrame); | |
| 135 WebContents* wc = agent_host->GetWebContents(); | |
| 136 DCHECK(DevToolsAgentHost::GetOrCreateFor(wc).get() != agent_host.get()); | |
| 137 set_parent_id(DevToolsAgentHost::GetOrCreateFor(wc)->GetId()); | |
| 138 } | |
| 139 | |
| 140 void FrameTarget::Inspect(Profile* profile) const { | |
| 141 DevToolsWindow::OpenDevToolsWindow(profile, GetAgentHost()); | |
| 142 } | |
| 143 | |
| 144 // WorkerTarget ---------------------------------------------------------------- | |
| 145 | |
| 146 class WorkerTarget : public DevToolsTargetImpl { | |
| 147 public: | |
| 148 explicit WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host); | |
| 149 | |
| 150 // DevToolsTargetImpl overrides: | |
| 151 void Inspect(Profile* profile) const override; | |
| 152 }; | |
| 153 | |
| 154 WorkerTarget::WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host) | |
| 155 : DevToolsTargetImpl(agent_host) { | |
| 156 switch (agent_host->GetType()) { | |
| 157 case DevToolsAgentHost::TYPE_SHARED_WORKER: | |
| 158 set_type(kTargetTypeWorker); | |
| 159 break; | |
| 160 case DevToolsAgentHost::TYPE_SERVICE_WORKER: | |
| 161 set_type(kTargetTypeServiceWorker); | |
| 162 break; | |
| 163 default: | |
| 164 NOTREACHED(); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 void WorkerTarget::Inspect(Profile* profile) const { | |
| 169 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost()); | |
| 170 } | |
| 171 | |
| 172 } // namespace | |
| 173 | |
| 174 // DevToolsTargetImpl ---------------------------------------------------------- | |
| 175 | |
| 176 DevToolsTargetImpl::~DevToolsTargetImpl() { | 31 DevToolsTargetImpl::~DevToolsTargetImpl() { |
| 177 } | 32 } |
| 178 | 33 |
| 179 DevToolsTargetImpl::DevToolsTargetImpl( | 34 DevToolsTargetImpl::DevToolsTargetImpl( |
| 180 scoped_refptr<DevToolsAgentHost> agent_host) | 35 scoped_refptr<DevToolsAgentHost> agent_host) |
| 181 : devtools_discovery::BasicTargetDescriptor(agent_host) { | 36 : devtools_discovery::BasicTargetDescriptor(agent_host) { |
| 182 } | 37 } |
| 183 | 38 |
| 184 int DevToolsTargetImpl::GetTabId() const { | |
| 185 return -1; | |
| 186 } | |
| 187 | |
| 188 WebContents* DevToolsTargetImpl::GetWebContents() const { | |
| 189 return GetAgentHost()->GetWebContents(); | |
| 190 } | |
| 191 | |
| 192 std::string DevToolsTargetImpl::GetExtensionId() const { | |
| 193 return std::string(); | |
| 194 } | |
| 195 | |
| 196 void DevToolsTargetImpl::Inspect(Profile* /*profile*/) const { | |
| 197 } | |
| 198 | |
| 199 void DevToolsTargetImpl::Reload() const { | |
| 200 } | |
| 201 | |
| 202 // static | |
| 203 std::unique_ptr<DevToolsTargetImpl> DevToolsTargetImpl::CreateForTab( | |
| 204 content::WebContents* web_contents) { | |
| 205 // TODO(dgozman): these checks should not be necessary. See | |
| 206 // http://crbug.com/489664. | |
| 207 if (!web_contents) | |
| 208 return nullptr; | |
| 209 if (!DevToolsAgentHost::GetOrCreateFor(web_contents)) | |
| 210 return nullptr; | |
| 211 return std::unique_ptr<DevToolsTargetImpl>( | |
| 212 new WebContentsTarget(web_contents, true)); | |
| 213 } | |
| 214 | |
| 215 // static | 39 // static |
| 216 std::vector<DevToolsTargetImpl*> DevToolsTargetImpl::EnumerateAll() { | 40 std::vector<DevToolsTargetImpl*> DevToolsTargetImpl::EnumerateAll() { |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 218 | 42 |
| 219 std::set<WebContents*> tab_web_contents; | |
| 220 for (TabContentsIterator it; !it.done(); it.Next()) | |
| 221 tab_web_contents.insert(*it); | |
| 222 | |
| 223 std::vector<DevToolsTargetImpl*> result; | 43 std::vector<DevToolsTargetImpl*> result; |
| 224 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 44 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
| 225 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 45 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
| 226 it != agents.end(); ++it) { | 46 it != agents.end(); ++it) { |
| 227 DevToolsAgentHost* agent_host = (*it).get(); | 47 result.push_back(new DevToolsTargetImpl(*it)); |
| 228 switch (agent_host->GetType()) { | |
| 229 case DevToolsAgentHost::TYPE_WEB_CONTENTS: | |
| 230 if (WebContents* web_contents = agent_host->GetWebContents()) { | |
| 231 const bool is_tab = | |
| 232 tab_web_contents.find(web_contents) != tab_web_contents.end(); | |
| 233 result.push_back(new WebContentsTarget(web_contents, is_tab)); | |
| 234 } | |
| 235 break; | |
| 236 case DevToolsAgentHost::TYPE_FRAME: | |
| 237 result.push_back(new FrameTarget(agent_host)); | |
| 238 break; | |
| 239 case DevToolsAgentHost::TYPE_SHARED_WORKER: | |
| 240 result.push_back(new WorkerTarget(agent_host)); | |
| 241 break; | |
| 242 case DevToolsAgentHost::TYPE_SERVICE_WORKER: | |
| 243 result.push_back(new WorkerTarget(agent_host)); | |
| 244 break; | |
| 245 default: | |
| 246 break; | |
| 247 } | |
| 248 } | 48 } |
| 249 return result; | 49 return result; |
| 250 } | 50 } |
| OLD | NEW |