OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/android/dev_tools_discovery_provider_android.h" | 5 #include "chrome/browser/android/devtools_manager_delegate_android.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" |
13 #include "chrome/browser/android/tab_android.h" | 11 #include "chrome/browser/android/tab_android.h" |
14 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/devtools/devtools_network_protocol_handler.h" |
15 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 13 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 14 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
17 #include "components/devtools_discovery/devtools_discovery_manager.h" | 15 #include "chrome/common/features.h" |
18 #include "content/public/browser/devtools_agent_host.h" | 16 #include "content/public/browser/devtools_agent_host.h" |
19 #include "content/public/browser/devtools_agent_host_client.h" | 17 #include "content/public/browser/devtools_agent_host_client.h" |
20 #include "content/public/browser/devtools_external_agent_proxy.h" | 18 #include "content/public/browser/devtools_external_agent_proxy.h" |
21 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 19 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
22 #include "content/public/browser/favicon_status.h" | |
23 #include "content/public/browser/navigation_entry.h" | |
24 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
25 | 21 |
26 using content::DevToolsAgentHost; | 22 using content::DevToolsAgentHost; |
27 using content::WebContents; | 23 using content::WebContents; |
28 | 24 |
29 namespace { | 25 namespace { |
30 | 26 |
31 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, | 27 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, |
32 public content::DevToolsAgentHostClient { | 28 public content::DevToolsAgentHostClient { |
33 public: | 29 public: |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 return true; | 146 return true; |
151 } | 147 } |
152 } | 148 } |
153 } | 149 } |
154 return false; | 150 return false; |
155 } | 151 } |
156 | 152 |
157 const int tab_id_; | 153 const int tab_id_; |
158 const std::string title_; | 154 const std::string title_; |
159 const GURL url_; | 155 const GURL url_; |
160 scoped_refptr<content::DevToolsAgentHost> agent_host_; | 156 scoped_refptr<DevToolsAgentHost> agent_host_; |
161 content::DevToolsExternalAgentProxy* proxy_; | 157 content::DevToolsExternalAgentProxy* proxy_; |
162 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); | 158 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); |
163 }; | 159 }; |
164 | 160 |
165 scoped_refptr<content::DevToolsAgentHost> CreateNewAndroidTab(const GURL& url) { | 161 DevToolsAgentHost::List GetDescriptors() { |
166 if (TabModelList::empty()) | 162 DevToolsAgentHost::List result; |
167 return nullptr; | |
168 | |
169 TabModel* tab_model = TabModelList::get(0); | |
170 if (!tab_model) | |
171 return nullptr; | |
172 | |
173 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); | |
174 if (!web_contents) | |
175 return nullptr; | |
176 | |
177 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
178 if (!tab) | |
179 return nullptr; | |
180 | |
181 return content::DevToolsAgentHost::Create(new TabProxyDelegate(tab)); | |
182 } | |
183 | |
184 } // namespace | |
185 | |
186 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { | |
187 } | |
188 | |
189 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { | |
190 } | |
191 | |
192 content::DevToolsAgentHost::List | |
193 DevToolsDiscoveryProviderAndroid::GetDescriptors() { | |
194 content::DevToolsAgentHost::List result; | |
195 | 163 |
196 // Enumerate existing tabs, including the ones with no WebContents. | 164 // Enumerate existing tabs, including the ones with no WebContents. |
197 std::set<WebContents*> tab_web_contents; | 165 std::set<WebContents*> tab_web_contents; |
198 for (TabModelList::const_iterator iter = TabModelList::begin(); | 166 for (TabModelList::const_iterator iter = TabModelList::begin(); |
199 iter != TabModelList::end(); ++iter) { | 167 iter != TabModelList::end(); ++iter) { |
200 TabModel* model = *iter; | 168 TabModel* model = *iter; |
201 for (int i = 0; i < model->GetTabCount(); ++i) { | 169 for (int i = 0; i < model->GetTabCount(); ++i) { |
202 TabAndroid* tab = model->GetTabAt(i); | 170 TabAndroid* tab = model->GetTabAt(i); |
203 if (!tab) | 171 if (!tab) |
204 continue; | 172 continue; |
205 | 173 |
206 scoped_refptr<content::DevToolsAgentHost> host = | 174 scoped_refptr<DevToolsAgentHost> host = |
207 DevToolsAgentHost::Create(new TabProxyDelegate(tab)); | 175 DevToolsAgentHost::Create(new TabProxyDelegate(tab)); |
208 } | 176 } |
209 } | 177 } |
210 | 178 |
211 // Add descriptors for targets not associated with any tabs. | 179 // Add descriptors for targets not associated with any tabs. |
212 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 180 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
213 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 181 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
214 it != agents.end(); ++it) { | 182 it != agents.end(); ++it) { |
215 if (WebContents* web_contents = (*it)->GetWebContents()) { | 183 if (WebContents* web_contents = (*it)->GetWebContents()) { |
216 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) | 184 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) |
217 continue; | 185 continue; |
218 } | 186 } |
219 result.push_back(*it); | 187 result.push_back(*it); |
220 } | 188 } |
221 | 189 |
222 return result; | 190 return result; |
223 } | 191 } |
224 | 192 |
225 // static | 193 } // namespace |
226 void DevToolsDiscoveryProviderAndroid::Install() { | 194 |
227 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | 195 DevToolsManagerDelegateAndroid::DevToolsManagerDelegateAndroid() |
228 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | 196 : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) { |
229 discovery_manager->AddProvider( | 197 #if BUILDFLAG(ANDROID_JAVA_UI) |
230 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); | 198 DevToolsAgentHost::AddDiscoveryProvider(base::Bind(&GetDescriptors)); |
231 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | 199 #endif // BUILDFLAG(ANDROID_JAVA_UI) |
232 } | 200 } |
| 201 |
| 202 DevToolsManagerDelegateAndroid::~DevToolsManagerDelegateAndroid() { |
| 203 } |
| 204 |
| 205 void DevToolsManagerDelegateAndroid::Inspect( |
| 206 DevToolsAgentHost* agent_host) { |
| 207 } |
| 208 |
| 209 base::DictionaryValue* DevToolsManagerDelegateAndroid::HandleCommand( |
| 210 DevToolsAgentHost* agent_host, |
| 211 base::DictionaryValue* command_dict) { |
| 212 return network_protocol_handler_->HandleCommand(agent_host, command_dict); |
| 213 } |
| 214 |
| 215 std::string DevToolsManagerDelegateAndroid::GetTargetType( |
| 216 content::RenderFrameHost* host) { |
| 217 content::WebContents* web_contents = |
| 218 content::WebContents::FromRenderFrameHost(host); |
| 219 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) |
| 220 : nullptr; |
| 221 return tab ? DevToolsAgentHost::kTypePage : |
| 222 DevToolsAgentHost::kTypeOther; |
| 223 } |
| 224 |
| 225 std::string DevToolsManagerDelegateAndroid::GetTargetTitle( |
| 226 content::RenderFrameHost* host) { |
| 227 content::WebContents* web_contents = |
| 228 content::WebContents::FromRenderFrameHost(host); |
| 229 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 230 return tab ? base::UTF16ToUTF8(tab->GetTitle()) : ""; |
| 231 } |
| 232 |
| 233 scoped_refptr<DevToolsAgentHost> |
| 234 DevToolsManagerDelegateAndroid::CreateNewTarget(const GURL& url) { |
| 235 if (TabModelList::empty()) |
| 236 return nullptr; |
| 237 |
| 238 TabModel* tab_model = TabModelList::get(0); |
| 239 if (!tab_model) |
| 240 return nullptr; |
| 241 |
| 242 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); |
| 243 if (!web_contents) |
| 244 return nullptr; |
| 245 |
| 246 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 247 if (!tab) |
| 248 return nullptr; |
| 249 |
| 250 return DevToolsAgentHost::Create(new TabProxyDelegate(tab)); |
| 251 } |
| 252 |
| 253 void DevToolsManagerDelegateAndroid::DevToolsAgentStateChanged( |
| 254 DevToolsAgentHost* agent_host, bool attached) { |
| 255 network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached); |
| 256 } |
OLD | NEW |