| 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/dev_tools_discovery_provider_android.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/android/tab_android.h" | 13 #include "chrome/browser/android/tab_android.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 15 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 17 #include "components/devtools_discovery/basic_target_descriptor.h" | 17 #include "components/devtools_discovery/basic_target_descriptor.h" |
| 18 #include "components/devtools_discovery/devtools_discovery_manager.h" | 18 #include "components/devtools_discovery/devtools_discovery_manager.h" |
| 19 #include "content/public/browser/devtools_agent_host.h" | 19 #include "content/public/browser/devtools_agent_host.h" |
| 20 #include "content/public/browser/devtools_agent_host_client.h" | 20 #include "content/public/browser/devtools_agent_host_client.h" |
| 21 #include "content/public/browser/devtools_external_agent_proxy.h" | 21 #include "content/public/browser/devtools_external_agent_proxy.h" |
| 22 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 22 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
| 23 #include "content/public/browser/favicon_status.h" | 23 #include "content/public/browser/favicon_status.h" |
| 24 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 | 26 |
| 27 using content::DevToolsAgentHost; | 27 using content::DevToolsAgentHost; |
| 28 using content::WebContents; | 28 using content::WebContents; |
| 29 using devtools_discovery::DevToolsTargetDescriptor; | |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, | 32 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, |
| 34 public content::DevToolsAgentHostClient { | 33 public content::DevToolsAgentHostClient { |
| 35 public: | 34 public: |
| 36 TabProxyDelegate(int tab_id, const base::string16& title, const GURL& url) | 35 TabProxyDelegate(int tab_id, const base::string16& title, const GURL& url) |
| 37 : tab_id_(tab_id), | 36 : tab_id_(tab_id), |
| 38 title_(base::UTF16ToUTF8(title)), | 37 title_(base::UTF16ToUTF8(title)), |
| 39 url_(url) { | 38 url_(url) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 150 } |
| 152 | 151 |
| 153 const int tab_id_; | 152 const int tab_id_; |
| 154 const std::string title_; | 153 const std::string title_; |
| 155 const GURL url_; | 154 const GURL url_; |
| 156 scoped_refptr<content::DevToolsAgentHost> agent_host_; | 155 scoped_refptr<content::DevToolsAgentHost> agent_host_; |
| 157 content::DevToolsExternalAgentProxy* proxy_; | 156 content::DevToolsExternalAgentProxy* proxy_; |
| 158 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); | 157 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); |
| 159 }; | 158 }; |
| 160 | 159 |
| 161 std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor> | 160 scoped_refptr<content::DevToolsAgentHost> CreateNewAndroidTab(const GURL& url) { |
| 162 CreateNewAndroidTab(const GURL& url) { | |
| 163 if (TabModelList::empty()) | 161 if (TabModelList::empty()) |
| 164 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 162 return nullptr; |
| 165 | 163 |
| 166 TabModel* tab_model = TabModelList::get(0); | 164 TabModel* tab_model = TabModelList::get(0); |
| 167 if (!tab_model) | 165 if (!tab_model) |
| 168 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 166 return nullptr; |
| 169 | 167 |
| 170 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); | 168 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); |
| 171 if (!web_contents) | 169 if (!web_contents) |
| 172 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 170 return nullptr; |
| 173 | 171 |
| 174 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 172 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 175 if (!tab) | 173 if (!tab) |
| 176 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 174 return nullptr; |
| 177 | 175 |
| 178 scoped_refptr<content::DevToolsAgentHost> host = | 176 return content::DevToolsAgentHost::Create(new TabProxyDelegate( |
| 179 content::DevToolsAgentHost::Create(new TabProxyDelegate( | |
| 180 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); | 177 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); |
| 181 return base::WrapUnique(new devtools_discovery::BasicTargetDescriptor(host)); | |
| 182 } | 178 } |
| 183 | 179 |
| 184 } // namespace | 180 } // namespace |
| 185 | 181 |
| 186 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { | 182 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { |
| 187 } | 183 } |
| 188 | 184 |
| 189 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { | 185 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { |
| 190 } | 186 } |
| 191 | 187 |
| 192 devtools_discovery::DevToolsTargetDescriptor::List | 188 content::DevToolsAgentHost::List |
| 193 DevToolsDiscoveryProviderAndroid::GetDescriptors() { | 189 DevToolsDiscoveryProviderAndroid::GetDescriptors() { |
| 194 devtools_discovery::DevToolsTargetDescriptor::List result; | 190 content::DevToolsAgentHost::List result; |
| 195 | 191 |
| 196 // Enumerate existing tabs, including the ones with no WebContents. | 192 // Enumerate existing tabs, including the ones with no WebContents. |
| 197 std::set<WebContents*> tab_web_contents; | 193 std::set<WebContents*> tab_web_contents; |
| 198 for (TabModelList::const_iterator iter = TabModelList::begin(); | 194 for (TabModelList::const_iterator iter = TabModelList::begin(); |
| 199 iter != TabModelList::end(); ++iter) { | 195 iter != TabModelList::end(); ++iter) { |
| 200 TabModel* model = *iter; | 196 TabModel* model = *iter; |
| 201 for (int i = 0; i < model->GetTabCount(); ++i) { | 197 for (int i = 0; i < model->GetTabCount(); ++i) { |
| 202 TabAndroid* tab = model->GetTabAt(i); | 198 TabAndroid* tab = model->GetTabAt(i); |
| 203 if (!tab) | 199 if (!tab) |
| 204 continue; | 200 continue; |
| 205 | 201 |
| 206 WebContents* web_contents = tab->web_contents(); | 202 WebContents* web_contents = tab->web_contents(); |
| 207 if (web_contents) { | 203 if (web_contents) { |
| 208 tab_web_contents.insert(web_contents); | 204 tab_web_contents.insert(web_contents); |
| 209 result.push_back(new devtools_discovery::BasicTargetDescriptor( | 205 result.push_back( |
| 210 content::DevToolsAgentHost::GetOrCreateFor(web_contents))); | 206 content::DevToolsAgentHost::GetOrCreateFor(web_contents)); |
| 211 } else { | 207 } else { |
| 212 scoped_refptr<content::DevToolsAgentHost> host = | 208 scoped_refptr<content::DevToolsAgentHost> host = |
| 213 content::DevToolsAgentHost::Create(new TabProxyDelegate( | 209 content::DevToolsAgentHost::Create(new TabProxyDelegate( |
| 214 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); | 210 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); |
| 215 result.push_back(new devtools_discovery::BasicTargetDescriptor(host)); | 211 result.push_back(host); |
| 216 } | 212 } |
| 217 } | 213 } |
| 218 } | 214 } |
| 219 | 215 |
| 220 // Add descriptors for targets not associated with any tabs. | 216 // Add descriptors for targets not associated with any tabs. |
| 221 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 217 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
| 222 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 218 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
| 223 it != agents.end(); ++it) { | 219 it != agents.end(); ++it) { |
| 224 if (WebContents* web_contents = (*it)->GetWebContents()) { | 220 if (WebContents* web_contents = (*it)->GetWebContents()) { |
| 225 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) | 221 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) |
| 226 continue; | 222 continue; |
| 227 } | 223 } |
| 228 result.push_back(new devtools_discovery::BasicTargetDescriptor(*it)); | 224 result.push_back(*it); |
| 229 } | 225 } |
| 230 | 226 |
| 231 return result; | 227 return result; |
| 232 } | 228 } |
| 233 | 229 |
| 234 // static | 230 // static |
| 235 void DevToolsDiscoveryProviderAndroid::Install() { | 231 void DevToolsDiscoveryProviderAndroid::Install() { |
| 236 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | 232 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = |
| 237 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | 233 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); |
| 238 discovery_manager->AddProvider( | 234 discovery_manager->AddProvider( |
| 239 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); | 235 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); |
| 240 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | 236 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); |
| 241 } | 237 } |
| OLD | NEW |