[Presentation API, Android] Implement basic messaging
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_target_impl.cc
blob9d5cac50834fbf834804365a598ee999c14623f9
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
3 // found in the LICENSE file.
5 #include "chrome/browser/devtools/devtools_target_impl.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/devtools/devtools_window.h"
10 #include "chrome/browser/extensions/extension_tab_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
13 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "components/guest_view/browser/guest_view_base.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/devtools_agent_host.h"
18 #include "content/public/browser/favicon_status.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "extensions/browser/extension_host.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/browser/process_manager.h"
25 #include "extensions/common/constants.h"
27 using content::BrowserThread;
28 using content::DevToolsAgentHost;
29 using content::WebContents;
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::kTargetTypeOther[] = "other";
38 const char DevToolsTargetImpl::kTargetTypeServiceWorker[] = "service_worker";
40 namespace {
42 // WebContentsTarget --------------------------------------------------------
44 class WebContentsTarget : public DevToolsTargetImpl {
45 public:
46 WebContentsTarget(WebContents* web_contents, bool is_tab);
48 // DevToolsTargetImpl overrides.
49 int GetTabId() const override;
50 std::string GetExtensionId() const override;
51 void Inspect(Profile* profile) const override;
53 private:
54 int tab_id_;
55 std::string extension_id_;
58 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab)
59 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)),
60 tab_id_(-1) {
61 set_type(kTargetTypeOther);
63 guest_view::GuestViewBase* guest =
64 guest_view::GuestViewBase::FromWebContents(web_contents);
65 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL;
66 if (guest_contents) {
67 set_type(kTargetTypeWebView);
68 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents)->GetId());
69 return;
72 if (is_tab) {
73 set_type(kTargetTypePage);
74 tab_id_ = extensions::ExtensionTabUtil::GetTabId(web_contents);
75 return;
78 const extensions::Extension* extension = extensions::ExtensionRegistry::Get(
79 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
80 GetURL().host());
81 if (!extension)
82 return;
84 Profile* profile =
85 Profile::FromBrowserContext(web_contents->GetBrowserContext());
86 if (!profile)
87 return;
88 set_title(extension->name());
89 extensions::ExtensionHost* extension_host =
90 extensions::ProcessManager::Get(profile)
91 ->GetBackgroundHostForExtension(extension->id());
92 if (extension_host &&
93 extension_host->host_contents() == web_contents) {
94 set_type(kTargetTypeBackgroundPage);
95 extension_id_ = extension->id();
96 } else if (extension->is_hosted_app()
97 || extension->is_legacy_packaged_app()
98 || extension->is_platform_app()) {
99 set_type(kTargetTypeApp);
101 set_favicon_url(extensions::ExtensionIconSource::GetIconURL(
102 extension, extension_misc::EXTENSION_ICON_SMALLISH,
103 ExtensionIconSet::MATCH_BIGGER, false, NULL));
106 int WebContentsTarget::GetTabId() const {
107 return tab_id_;
110 std::string WebContentsTarget::GetExtensionId() const {
111 return extension_id_;
114 void WebContentsTarget::Inspect(Profile* profile) const {
115 WebContents* web_contents = GetWebContents();
116 if (!web_contents)
117 return;
118 DevToolsWindow::OpenDevToolsWindow(web_contents);
121 // FrameTarget ----------------------------------------------------------------
123 class FrameTarget : public DevToolsTargetImpl {
124 public:
125 explicit FrameTarget(scoped_refptr<DevToolsAgentHost> agent_host);
127 // DevToolsTargetImpl overrides:
128 void Inspect(Profile* profile) const override;
131 FrameTarget::FrameTarget(scoped_refptr<DevToolsAgentHost> agent_host)
132 : DevToolsTargetImpl(agent_host) {
133 set_type(kTargetTypeIFrame);
134 WebContents* wc = agent_host->GetWebContents();
135 DCHECK(DevToolsAgentHost::GetOrCreateFor(wc).get() != agent_host.get());
136 set_parent_id(DevToolsAgentHost::GetOrCreateFor(wc)->GetId());
139 void FrameTarget::Inspect(Profile* profile) const {
140 DevToolsWindow::OpenDevToolsWindow(profile, GetAgentHost());
143 // WorkerTarget ----------------------------------------------------------------
145 class WorkerTarget : public DevToolsTargetImpl {
146 public:
147 explicit WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host);
149 // DevToolsTargetImpl overrides:
150 void Inspect(Profile* profile) const override;
153 WorkerTarget::WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host)
154 : DevToolsTargetImpl(agent_host) {
155 switch (agent_host->GetType()) {
156 case DevToolsAgentHost::TYPE_SHARED_WORKER:
157 set_type(kTargetTypeWorker);
158 break;
159 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
160 set_type(kTargetTypeServiceWorker);
161 break;
162 default:
163 NOTREACHED();
167 void WorkerTarget::Inspect(Profile* profile) const {
168 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost());
171 } // namespace
173 // DevToolsTargetImpl ----------------------------------------------------------
175 DevToolsTargetImpl::~DevToolsTargetImpl() {
178 DevToolsTargetImpl::DevToolsTargetImpl(
179 scoped_refptr<DevToolsAgentHost> agent_host)
180 : devtools_discovery::BasicTargetDescriptor(agent_host) {
183 int DevToolsTargetImpl::GetTabId() const {
184 return -1;
187 WebContents* DevToolsTargetImpl::GetWebContents() const {
188 return GetAgentHost()->GetWebContents();
191 std::string DevToolsTargetImpl::GetExtensionId() const {
192 return std::string();
195 void DevToolsTargetImpl::Inspect(Profile* /*profile*/) const {
198 void DevToolsTargetImpl::Reload() const {
201 // static
202 scoped_ptr<DevToolsTargetImpl> DevToolsTargetImpl::CreateForTab(
203 content::WebContents* web_contents) {
204 // TODO(dgozman): these checks should not be necessary. See
205 // http://crbug.com/489664.
206 if (!web_contents)
207 return nullptr;
208 if (!DevToolsAgentHost::GetOrCreateFor(web_contents))
209 return nullptr;
210 return scoped_ptr<DevToolsTargetImpl>(
211 new WebContentsTarget(web_contents, true));
214 // static
215 std::vector<DevToolsTargetImpl*> DevToolsTargetImpl::EnumerateAll() {
216 DCHECK_CURRENTLY_ON(BrowserThread::UI);
218 std::set<WebContents*> tab_web_contents;
219 for (TabContentsIterator it; !it.done(); it.Next())
220 tab_web_contents.insert(*it);
222 std::vector<DevToolsTargetImpl*> result;
223 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
224 for (DevToolsAgentHost::List::iterator it = agents.begin();
225 it != agents.end(); ++it) {
226 DevToolsAgentHost* agent_host = (*it).get();
227 switch (agent_host->GetType()) {
228 case DevToolsAgentHost::TYPE_WEB_CONTENTS:
229 if (WebContents* web_contents = agent_host->GetWebContents()) {
230 const bool is_tab =
231 tab_web_contents.find(web_contents) != tab_web_contents.end();
232 result.push_back(new WebContentsTarget(web_contents, is_tab));
234 break;
235 case DevToolsAgentHost::TYPE_FRAME:
236 result.push_back(new FrameTarget(agent_host));
237 break;
238 case DevToolsAgentHost::TYPE_SHARED_WORKER:
239 result.push_back(new WorkerTarget(agent_host));
240 break;
241 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
242 result.push_back(new WorkerTarget(agent_host));
243 break;
244 default:
245 break;
248 return result;