| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chrome_devtools_manager_delegate.h" | 5 #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 9 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" | 8 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" |
| 10 #include "components/devtools_discovery/devtools_discovery_manager.h" | 9 #include "components/devtools_discovery/devtools_discovery_manager.h" |
| 11 #include "content/public/browser/devtools_agent_host.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 | 10 |
| 14 #if !defined(OS_ANDROID) | 11 #if !defined(OS_ANDROID) |
| 15 #include "chrome/browser/devtools/devtools_window.h" | 12 #include "chrome/browser/devtools/devtools_window.h" |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 14 #include "content/public/browser/devtools_agent_host.h" |
| 19 #include "content/public/browser/render_frame_host.h" | |
| 20 #include "extensions/browser/extension_host.h" | |
| 21 #include "extensions/browser/extension_registry.h" | |
| 22 #include "extensions/browser/process_manager.h" | |
| 23 #else // !defined(OS_ANDROID) | |
| 24 #include "chrome/browser/android/tab_android.h" | |
| 25 #include "chrome/browser/ui/android/tab_model/tab_model.h" | |
| 26 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | |
| 27 #endif // !defined(OS_ANDROID) | 15 #endif // !defined(OS_ANDROID) |
| 28 | 16 |
| 29 using devtools_discovery::DevToolsDiscoveryManager; | 17 using devtools_discovery::DevToolsDiscoveryManager; |
| 30 | 18 |
| 31 char ChromeDevToolsManagerDelegate::kTypeApp[] = "app"; | |
| 32 char ChromeDevToolsManagerDelegate::kTypeBackgroundPage[] = "background_page"; | |
| 33 | |
| 34 ChromeDevToolsManagerDelegate::ChromeDevToolsManagerDelegate() | 19 ChromeDevToolsManagerDelegate::ChromeDevToolsManagerDelegate() |
| 35 : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) { | 20 : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) { |
| 36 } | 21 } |
| 37 | 22 |
| 38 ChromeDevToolsManagerDelegate::~ChromeDevToolsManagerDelegate() { | 23 ChromeDevToolsManagerDelegate::~ChromeDevToolsManagerDelegate() { |
| 39 } | 24 } |
| 40 | 25 |
| 41 void ChromeDevToolsManagerDelegate::Inspect( | 26 void ChromeDevToolsManagerDelegate::Inspect( |
| 27 content::BrowserContext* browser_context, |
| 42 content::DevToolsAgentHost* agent_host) { | 28 content::DevToolsAgentHost* agent_host) { |
| 43 #if !defined(OS_ANDROID) | 29 #if !defined(OS_ANDROID) |
| 44 Profile* profile = | 30 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 45 Profile::FromBrowserContext(agent_host->GetBrowserContext()); | |
| 46 if (!profile) | 31 if (!profile) |
| 47 return; | 32 return; |
| 48 std::string type = agent_host->GetType(); | 33 content::DevToolsAgentHost::Type type = agent_host->GetType(); |
| 49 if (type == content::DevToolsAgentHost::kTypeSharedWorker || | 34 if (type == content::DevToolsAgentHost::TYPE_SHARED_WORKER || |
| 50 type == content::DevToolsAgentHost::kTypeServiceWorker) { | 35 type == content::DevToolsAgentHost::TYPE_SERVICE_WORKER) { |
| 51 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); | 36 DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host); |
| 52 return; | 37 return; |
| 53 } | 38 } |
| 54 content::WebContents* web_contents = agent_host->GetWebContents(); | 39 if (type == content::DevToolsAgentHost::TYPE_WEB_CONTENTS) { |
| 55 if (web_contents) | 40 content::WebContents* web_contents = agent_host->GetWebContents(); |
| 41 DCHECK(web_contents); |
| 56 DevToolsWindow::OpenDevToolsWindow(web_contents); | 42 DevToolsWindow::OpenDevToolsWindow(web_contents); |
| 43 } |
| 57 #endif // !defined(OS_ANDROID) | 44 #endif // !defined(OS_ANDROID) |
| 58 } | 45 } |
| 59 | 46 |
| 60 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( | 47 base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand( |
| 61 content::DevToolsAgentHost* agent_host, | 48 content::DevToolsAgentHost* agent_host, |
| 62 base::DictionaryValue* command_dict) { | 49 base::DictionaryValue* command_dict) { |
| 63 std::unique_ptr<base::DictionaryValue> result = | 50 std::unique_ptr<base::DictionaryValue> result = |
| 64 DevToolsDiscoveryManager::GetInstance()->HandleCreateTargetCommand( | 51 DevToolsDiscoveryManager::GetInstance()->HandleCreateTargetCommand( |
| 65 command_dict); | 52 command_dict); |
| 66 if (result) | 53 if (result) |
| 67 return result.release(); // Caller takes ownership. | 54 return result.release(); // Caller takes ownership. |
| 68 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | 55 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 69 } | 56 } |
| 70 | 57 |
| 71 std::string ChromeDevToolsManagerDelegate::GetTargetType( | |
| 72 content::RenderFrameHost* host) { | |
| 73 content::WebContents* web_contents = | |
| 74 content::WebContents::FromRenderFrameHost(host); | |
| 75 #if !defined(OS_ANDROID) | |
| 76 for (TabContentsIterator it; !it.done(); it.Next()) { | |
| 77 if (*it == web_contents) | |
| 78 return content::DevToolsAgentHost::kTypePage; | |
| 79 } | |
| 80 | |
| 81 if (host->GetParent()) | |
| 82 return content::DevToolsAgentHost::kTypeFrame; | |
| 83 | |
| 84 const extensions::Extension* extension = extensions::ExtensionRegistry::Get( | |
| 85 web_contents->GetBrowserContext())->enabled_extensions().GetByID( | |
| 86 host->GetLastCommittedURL().host()); | |
| 87 if (!extension) | |
| 88 return content::DevToolsAgentHost::kTypeOther; | |
| 89 | |
| 90 Profile* profile = | |
| 91 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 92 if (!profile) | |
| 93 return content::DevToolsAgentHost::kTypeOther; | |
| 94 | |
| 95 extensions::ExtensionHost* extension_host = | |
| 96 extensions::ProcessManager::Get(profile) | |
| 97 ->GetBackgroundHostForExtension(extension->id()); | |
| 98 if (extension_host && | |
| 99 extension_host->host_contents() == web_contents) { | |
| 100 return kTypeBackgroundPage; | |
| 101 } else if (extension->is_hosted_app() | |
| 102 || extension->is_legacy_packaged_app() | |
| 103 || extension->is_platform_app()) { | |
| 104 return kTypeApp; | |
| 105 } | |
| 106 #else // !defined(OS_ANDROID) | |
| 107 for (TabModelList::const_iterator iter = TabModelList::begin(); | |
| 108 iter != TabModelList::end(); ++iter) { | |
| 109 TabModel* model = *iter; | |
| 110 for (int i = 0; i < model->GetTabCount(); ++i) { | |
| 111 TabAndroid* tab = model->GetTabAt(i); | |
| 112 if (tab && web_contents == tab->web_contents()) | |
| 113 return content::DevToolsAgentHost::kTypePage; | |
| 114 } | |
| 115 } | |
| 116 #endif // !defined(OS_ANDROID) | |
| 117 return content::DevToolsAgentHost::kTypeOther; | |
| 118 } | |
| 119 | |
| 120 std::string ChromeDevToolsManagerDelegate::GetTargetTitle( | |
| 121 content::RenderFrameHost* host) { | |
| 122 #if !defined(OS_ANDROID) | |
| 123 content::WebContents* web_contents = | |
| 124 content::WebContents::FromRenderFrameHost(host); | |
| 125 if (host->GetParent()) | |
| 126 return host->GetLastCommittedURL().spec(); | |
| 127 for (TabContentsIterator it; !it.done(); it.Next()) { | |
| 128 if (*it == web_contents) | |
| 129 return base::UTF16ToUTF8(web_contents->GetTitle()); | |
| 130 } | |
| 131 const extensions::Extension* extension = extensions::ExtensionRegistry::Get( | |
| 132 web_contents->GetBrowserContext())->enabled_extensions().GetByID( | |
| 133 host->GetLastCommittedURL().host()); | |
| 134 if (extension) | |
| 135 return extension->name(); | |
| 136 #endif // !defined(OS_ANDROID) | |
| 137 return ""; | |
| 138 } | |
| 139 | |
| 140 void ChromeDevToolsManagerDelegate::DevToolsAgentStateChanged( | 58 void ChromeDevToolsManagerDelegate::DevToolsAgentStateChanged( |
| 141 content::DevToolsAgentHost* agent_host, | 59 content::DevToolsAgentHost* agent_host, |
| 142 bool attached) { | 60 bool attached) { |
| 143 network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached); | 61 network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached); |
| 144 } | 62 } |
| OLD | NEW |