Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_target_impl.cc
blob23c94112b623b8f11e2e37e98f1ef3bfc3f9e709
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 "content/public/browser/browser_thread.h"
16 #include "content/public/browser/favicon_status.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/browser/extension_host.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/guest_view/guest_view_base.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::RenderViewHost;
30 using content::WebContents;
31 using content::WorkerService;
33 const char DevToolsTargetImpl::kTargetTypeApp[] = "app";
34 const char DevToolsTargetImpl::kTargetTypeBackgroundPage[] = "background_page";
35 const char DevToolsTargetImpl::kTargetTypePage[] = "page";
36 const char DevToolsTargetImpl::kTargetTypeWorker[] = "worker";
37 const char DevToolsTargetImpl::kTargetTypeWebView[] = "webview";
38 const char DevToolsTargetImpl::kTargetTypeIFrame[] = "iframe";
39 const char DevToolsTargetImpl::kTargetTypeOther[] = "other";
40 const char DevToolsTargetImpl::kTargetTypeServiceWorker[] = "service_worker";
42 namespace {
44 // WebContentsTarget --------------------------------------------------------
46 class WebContentsTarget : public DevToolsTargetImpl {
47 public:
48 WebContentsTarget(WebContents* web_contents, bool is_tab);
50 // DevToolsTargetImpl overrides:
51 WebContents* GetWebContents() const override;
52 int GetTabId() const override;
53 std::string GetExtensionId() const override;
54 void Inspect(Profile* profile) const override;
56 private:
57 int tab_id_;
58 std::string extension_id_;
61 WebContentsTarget::WebContentsTarget(WebContents* web_contents, bool is_tab)
62 : DevToolsTargetImpl(DevToolsAgentHost::GetOrCreateFor(web_contents)),
63 tab_id_(-1) {
64 set_type(kTargetTypeOther);
66 content::RenderFrameHost* rfh =
67 web_contents->GetRenderViewHost()->GetMainFrame();
68 if (rfh->IsCrossProcessSubframe()) {
69 set_url(rfh->GetLastCommittedURL());
70 set_type(kTargetTypeIFrame);
71 // TODO(pfeldman) Update for out of process iframes.
72 RenderViewHost* parent_rvh = rfh->GetParent()->GetRenderViewHost();
73 set_parent_id(DevToolsAgentHost::GetOrCreateFor(
74 WebContents::FromRenderViewHost(parent_rvh))->GetId());
75 return;
78 content::NavigationController& controller = web_contents->GetController();
79 content::NavigationEntry* entry = controller.GetActiveEntry();
80 if (entry != NULL && entry->GetURL().is_valid())
81 set_favicon_url(entry->GetFavicon().url);
82 set_last_activity_time(web_contents->GetLastActiveTime());
84 extensions::GuestViewBase* guest =
85 extensions::GuestViewBase::FromWebContents(web_contents);
86 WebContents* guest_contents = guest ? guest->embedder_web_contents() : NULL;
87 if (guest_contents) {
88 set_type(kTargetTypeWebView);
89 set_parent_id(DevToolsAgentHost::GetOrCreateFor(guest_contents)->GetId());
90 return;
93 if (is_tab) {
94 set_type(kTargetTypePage);
95 tab_id_ = extensions::ExtensionTabUtil::GetTabId(web_contents);
96 return;
99 const extensions::Extension* extension = extensions::ExtensionRegistry::Get(
100 web_contents->GetBrowserContext())->enabled_extensions().GetByID(
101 GetURL().host());
102 if (!extension)
103 return;
105 Profile* profile =
106 Profile::FromBrowserContext(web_contents->GetBrowserContext());
107 if (!profile)
108 return;
109 set_title(extension->name());
110 extensions::ExtensionHost* extension_host =
111 extensions::ProcessManager::Get(profile)
112 ->GetBackgroundHostForExtension(extension->id());
113 if (extension_host &&
114 extension_host->host_contents() == web_contents) {
115 set_type(kTargetTypeBackgroundPage);
116 extension_id_ = extension->id();
117 } else if (extension->is_hosted_app()
118 || extension->is_legacy_packaged_app()
119 || extension->is_platform_app()) {
120 set_type(kTargetTypeApp);
122 set_favicon_url(extensions::ExtensionIconSource::GetIconURL(
123 extension, extension_misc::EXTENSION_ICON_SMALLISH,
124 ExtensionIconSet::MATCH_BIGGER, false, NULL));
127 WebContents* WebContentsTarget::GetWebContents() const {
128 return GetAgentHost()->GetWebContents();
131 int WebContentsTarget::GetTabId() const {
132 return tab_id_;
135 std::string WebContentsTarget::GetExtensionId() const {
136 return extension_id_;
139 void WebContentsTarget::Inspect(Profile* profile) const {
140 WebContents* web_contents = GetWebContents();
141 if (!web_contents)
142 return;
143 DevToolsWindow::OpenDevToolsWindow(web_contents);
146 // WorkerTarget ----------------------------------------------------------------
148 class WorkerTarget : public DevToolsTargetImpl {
149 public:
150 explicit WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host);
152 // DevToolsTargetImpl overrides:
153 void Inspect(Profile* profile) const override;
156 WorkerTarget::WorkerTarget(scoped_refptr<DevToolsAgentHost> agent_host)
157 : DevToolsTargetImpl(agent_host) {
158 switch (agent_host->GetType()) {
159 case DevToolsAgentHost::TYPE_SHARED_WORKER:
160 set_type(kTargetTypeWorker);
161 break;
162 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
163 set_type(kTargetTypeServiceWorker);
164 break;
165 default:
166 NOTREACHED();
170 void WorkerTarget::Inspect(Profile* profile) const {
171 DevToolsWindow::OpenDevToolsWindowForWorker(profile, GetAgentHost());
174 } // namespace
176 // DevToolsTargetImpl ----------------------------------------------------------
178 DevToolsTargetImpl::~DevToolsTargetImpl() {
181 DevToolsTargetImpl::DevToolsTargetImpl(
182 scoped_refptr<DevToolsAgentHost> agent_host)
183 : agent_host_(agent_host),
184 title_(agent_host->GetTitle()),
185 url_(agent_host->GetURL()) {
188 std::string DevToolsTargetImpl::GetParentId() const {
189 return parent_id_;
192 std::string DevToolsTargetImpl::GetId() const {
193 return agent_host_->GetId();
196 std::string DevToolsTargetImpl::GetType() const {
197 return type_;
200 std::string DevToolsTargetImpl::GetTitle() const {
201 return title_;
204 std::string DevToolsTargetImpl::GetDescription() const {
205 return description_;
208 GURL DevToolsTargetImpl::GetURL() const {
209 return url_;
212 GURL DevToolsTargetImpl::GetFaviconURL() const {
213 return favicon_url_;
216 base::TimeTicks DevToolsTargetImpl::GetLastActivityTime() const {
217 return last_activity_time_;
220 scoped_refptr<content::DevToolsAgentHost>
221 DevToolsTargetImpl::GetAgentHost() const {
222 return agent_host_;
225 bool DevToolsTargetImpl::IsAttached() const {
226 return agent_host_->IsAttached();
229 bool DevToolsTargetImpl::Activate() const {
230 return agent_host_->Activate();
233 bool DevToolsTargetImpl::Close() const {
234 return agent_host_->Close();
237 int DevToolsTargetImpl::GetTabId() const {
238 return -1;
241 WebContents* DevToolsTargetImpl::GetWebContents() const {
242 return NULL;
245 std::string DevToolsTargetImpl::GetExtensionId() const {
246 return std::string();
249 void DevToolsTargetImpl::Inspect(Profile* /*profile*/) const {
252 void DevToolsTargetImpl::Reload() const {
255 // static
256 scoped_ptr<DevToolsTargetImpl> DevToolsTargetImpl::CreateForWebContents(
257 content::WebContents* web_contents,
258 bool is_tab) {
259 return scoped_ptr<DevToolsTargetImpl>(
260 new WebContentsTarget(web_contents, is_tab));
263 // static
264 void DevToolsTargetImpl::EnumerateAllTargets(Callback callback) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
267 std::set<WebContents*> tab_web_contents;
268 for (TabContentsIterator it; !it.done(); it.Next())
269 tab_web_contents.insert(*it);
271 DevToolsTargetImpl::List result;
272 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
273 for (DevToolsAgentHost::List::iterator it = agents.begin();
274 it != agents.end(); ++it) {
275 DevToolsAgentHost* agent_host = (*it).get();
276 switch (agent_host->GetType()) {
277 case DevToolsAgentHost::TYPE_WEB_CONTENTS:
278 if (WebContents* web_contents = agent_host->GetWebContents()) {
279 const bool is_tab =
280 tab_web_contents.find(web_contents) != tab_web_contents.end();
281 result.push_back(new WebContentsTarget(web_contents, is_tab));
283 break;
284 case DevToolsAgentHost::TYPE_SHARED_WORKER:
285 result.push_back(new WorkerTarget(agent_host));
286 break;
287 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
288 result.push_back(new WorkerTarget(agent_host));
289 break;
290 default:
291 break;
295 callback.Run(result);