| 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" |
| 21 #include "content/public/browser/devtools_external_agent_proxy.h" |
| 22 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
| 20 #include "content/public/browser/favicon_status.h" | 23 #include "content/public/browser/favicon_status.h" |
| 21 #include "content/public/browser/navigation_entry.h" | 24 #include "content/public/browser/navigation_entry.h" |
| 22 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 23 | 26 |
| 24 using content::DevToolsAgentHost; | 27 using content::DevToolsAgentHost; |
| 25 using content::WebContents; | 28 using content::WebContents; |
| 29 using devtools_discovery::DevToolsTargetDescriptor; |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 GURL GetFaviconURLForContents(WebContents* web_contents) { | 33 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, |
| 30 content::NavigationController& controller = web_contents->GetController(); | 34 public content::DevToolsAgentHostClient { |
| 31 content::NavigationEntry* entry = controller.GetActiveEntry(); | |
| 32 if (entry != NULL && entry->GetURL().is_valid()) | |
| 33 return entry->GetFavicon().url; | |
| 34 return GURL(); | |
| 35 } | |
| 36 | |
| 37 class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { | |
| 38 public: | 35 public: |
| 39 static TabDescriptor* CreateForWebContents(int tab_id, | 36 explicit TabProxyDelegate(TabAndroid* tab) |
| 40 WebContents* web_contents) { | 37 : tab_id_(tab->GetAndroidId()), |
| 41 return new TabDescriptor(tab_id, web_contents); | 38 title_(base::UTF16ToUTF8(tab->GetTitle())), |
| 39 url_(tab->GetURL()), |
| 40 agent_host_(tab->web_contents() ? |
| 41 DevToolsAgentHost::GetOrCreateFor(tab->web_contents()) : nullptr) { |
| 42 } | 42 } |
| 43 | 43 |
| 44 static TabDescriptor* CreateForUnloadedTab(int tab_id, | 44 ~TabProxyDelegate() override { |
| 45 const base::string16& title, | |
| 46 const GURL& url) { | |
| 47 return new TabDescriptor(tab_id, title, url); | |
| 48 } | 45 } |
| 49 | 46 |
| 50 ~TabDescriptor() override { | 47 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
| 48 const std::string& message) override { |
| 49 proxy_->DispatchOnClientHost(message); |
| 51 } | 50 } |
| 52 | 51 |
| 53 // devtools_discovery::DevToolsTargetDescriptor implementation. | 52 void AgentHostClosed(DevToolsAgentHost* agent_host, |
| 54 std::string GetParentId() const override { | 53 bool replaced_with_another_client) override { |
| 55 return std::string(); | 54 proxy_->ConnectionClosed(); |
| 56 } | 55 } |
| 57 | 56 |
| 58 std::string GetTitle() const override { | 57 void Attach(content::DevToolsExternalAgentProxy* proxy) override { |
| 59 return title_; | 58 proxy_ = proxy; |
| 59 MaterializeAgentHost(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::string GetDescription() const override { | 62 void Detach() override { |
| 63 return std::string(); | 63 if (agent_host_) |
| 64 agent_host_->DetachClient(this); |
| 65 agent_host_ = nullptr; |
| 66 proxy_ = nullptr; |
| 64 } | 67 } |
| 65 | 68 |
| 66 GURL GetURL() const override { | 69 std::string GetId() override { |
| 67 return url_; | |
| 68 } | |
| 69 | |
| 70 GURL GetFaviconURL() const override { | |
| 71 return favicon_url_; | |
| 72 } | |
| 73 | |
| 74 base::TimeTicks GetLastActivityTime() const override { | |
| 75 return last_activity_time_; | |
| 76 } | |
| 77 | |
| 78 std::string GetId() const override { | |
| 79 return base::IntToString(tab_id_); | 70 return base::IntToString(tab_id_); |
| 80 } | 71 } |
| 81 | 72 |
| 82 std::string GetType() const override { | 73 std::string GetType() override { |
| 83 return devtools_discovery::BasicTargetDescriptor::kTypePage; | 74 return agent_host_ ? agent_host_->GetType() : DevToolsAgentHost::kTypePage; |
| 84 } | 75 } |
| 85 | 76 |
| 86 bool IsAttached() const override { | 77 std::string GetTitle() override { |
| 78 return agent_host_ ? agent_host_->GetTitle() : title_; |
| 79 } |
| 80 |
| 81 std::string GetDescription() override { |
| 82 return agent_host_ ? agent_host_->GetDescription() : ""; |
| 83 } |
| 84 |
| 85 GURL GetURL() override { |
| 86 return agent_host_ ? agent_host_->GetURL() : url_; |
| 87 } |
| 88 |
| 89 GURL GetFaviconURL() override { |
| 90 return agent_host_ ? agent_host_->GetFaviconURL() : GURL(); |
| 91 } |
| 92 |
| 93 bool Activate() override { |
| 87 TabModel* model; | 94 TabModel* model; |
| 88 int index; | 95 int index; |
| 89 if (!FindTab(&model, &index)) | 96 if (!FindTab(&model, &index)) |
| 90 return false; | |
| 91 WebContents* web_contents = model->GetWebContentsAt(index); | |
| 92 if (!web_contents) | |
| 93 return false; | |
| 94 return DevToolsAgentHost::IsDebuggerAttached(web_contents); | |
| 95 } | |
| 96 | |
| 97 scoped_refptr<DevToolsAgentHost> GetAgentHost() const override { | |
| 98 TabModel* model; | |
| 99 int index; | |
| 100 if (!FindTab(&model, &index)) | |
| 101 return NULL; | |
| 102 WebContents* web_contents = model->GetWebContentsAt(index); | |
| 103 if (!web_contents) { | |
| 104 // The tab has been pushed out of memory, pull it back. | |
| 105 TabAndroid* tab = model->GetTabAt(index); | |
| 106 if (!tab) | |
| 107 return NULL; | |
| 108 | |
| 109 if (!tab->LoadIfNeeded()) | |
| 110 return NULL; | |
| 111 | |
| 112 web_contents = model->GetWebContentsAt(index); | |
| 113 if (!web_contents) | |
| 114 return NULL; | |
| 115 } | |
| 116 return DevToolsAgentHost::GetOrCreateFor(web_contents); | |
| 117 } | |
| 118 | |
| 119 bool Activate() const override { | |
| 120 TabModel* model; | |
| 121 int index; | |
| 122 if (!FindTab(&model, &index)) | |
| 123 return false; | 97 return false; |
| 124 model->SetActiveIndex(index); | 98 model->SetActiveIndex(index); |
| 125 return true; | 99 return true; |
| 126 } | 100 } |
| 127 | 101 |
| 128 bool Close() const override { | 102 bool Inspect() override { |
| 103 MaterializeAgentHost(); |
| 104 if (agent_host_) |
| 105 return agent_host_->Inspect(); |
| 106 return false; |
| 107 } |
| 108 |
| 109 void Reload() override { |
| 110 MaterializeAgentHost(); |
| 111 if (agent_host_) |
| 112 agent_host_->Reload(); |
| 113 } |
| 114 |
| 115 bool Close() override { |
| 129 TabModel* model; | 116 TabModel* model; |
| 130 int index; | 117 int index; |
| 131 if (!FindTab(&model, &index)) | 118 if (!FindTab(&model, &index)) |
| 132 return false; | 119 return false; |
| 133 model->CloseTabAt(index); | 120 model->CloseTabAt(index); |
| 134 return true; | 121 return true; |
| 135 } | 122 } |
| 136 | 123 |
| 137 private: | 124 void SendMessageToBackend(const std::string& message) override { |
| 138 TabDescriptor(int tab_id, WebContents* web_contents) | 125 if (agent_host_) |
| 139 : tab_id_(tab_id), | 126 agent_host_->DispatchProtocolMessage(this, message); |
| 140 title_(base::UTF16ToUTF8(web_contents->GetTitle())), | |
| 141 url_(web_contents->GetURL()), | |
| 142 favicon_url_(GetFaviconURLForContents(web_contents)), | |
| 143 last_activity_time_(web_contents->GetLastActiveTime()) { | |
| 144 } | 127 } |
| 145 | 128 |
| 146 TabDescriptor(int tab_id, const base::string16& title, const GURL& url) | 129 private: |
| 147 : tab_id_(tab_id), | 130 void MaterializeAgentHost() { |
| 148 title_(base::UTF16ToUTF8(title)), | 131 if (agent_host_) |
| 149 url_(url) { | 132 return; |
| 133 TabModel* model; |
| 134 int index; |
| 135 if (!FindTab(&model, &index)) |
| 136 return; |
| 137 WebContents* web_contents = model->GetWebContentsAt(index); |
| 138 if (!web_contents) |
| 139 return; |
| 140 agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents); |
| 150 } | 141 } |
| 151 | 142 |
| 152 bool FindTab(TabModel** model_result, int* index_result) const { | 143 bool FindTab(TabModel** model_result, int* index_result) const { |
| 153 for (TabModelList::const_iterator iter = TabModelList::begin(); | 144 for (TabModelList::const_iterator iter = TabModelList::begin(); |
| 154 iter != TabModelList::end(); ++iter) { | 145 iter != TabModelList::end(); ++iter) { |
| 155 TabModel* model = *iter; | 146 TabModel* model = *iter; |
| 156 for (int i = 0; i < model->GetTabCount(); ++i) { | 147 for (int i = 0; i < model->GetTabCount(); ++i) { |
| 157 TabAndroid* tab = model->GetTabAt(i); | 148 TabAndroid* tab = model->GetTabAt(i); |
| 158 if (tab && tab->GetAndroidId() == tab_id_) { | 149 if (tab && tab->GetAndroidId() == tab_id_) { |
| 159 *model_result = model; | 150 *model_result = model; |
| 160 *index_result = i; | 151 *index_result = i; |
| 161 return true; | 152 return true; |
| 162 } | 153 } |
| 163 } | 154 } |
| 164 } | 155 } |
| 165 return false; | 156 return false; |
| 166 } | 157 } |
| 167 | 158 |
| 168 const int tab_id_; | 159 const int tab_id_; |
| 169 const std::string title_; | 160 const std::string title_; |
| 170 const GURL url_; | 161 const GURL url_; |
| 171 const GURL favicon_url_; | 162 scoped_refptr<content::DevToolsAgentHost> agent_host_; |
| 172 const base::TimeTicks last_activity_time_; | 163 content::DevToolsExternalAgentProxy* proxy_; |
| 173 | 164 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); |
| 174 DISALLOW_COPY_AND_ASSIGN(TabDescriptor); | |
| 175 }; | 165 }; |
| 176 | 166 |
| 177 std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor> | 167 std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor> |
| 178 CreateNewAndroidTab(const GURL& url) { | 168 CreateNewAndroidTab(const GURL& url) { |
| 179 if (TabModelList::empty()) | 169 if (TabModelList::empty()) |
| 180 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 170 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
| 181 | 171 |
| 182 TabModel* tab_model = TabModelList::get(0); | 172 TabModel* tab_model = TabModelList::get(0); |
| 183 if (!tab_model) | 173 if (!tab_model) |
| 184 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 174 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
| 185 | 175 |
| 186 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); | 176 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); |
| 187 if (!web_contents) | 177 if (!web_contents) |
| 188 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 178 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
| 189 | 179 |
| 190 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 180 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 191 if (!tab) | 181 if (!tab) |
| 192 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); | 182 return std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
| 193 | 183 |
| 194 return base::WrapUnique( | 184 scoped_refptr<content::DevToolsAgentHost> host = |
| 195 TabDescriptor::CreateForWebContents(tab->GetAndroidId(), web_contents)); | 185 DevToolsAgentHost::Create(new TabProxyDelegate(tab)); |
| 186 return base::WrapUnique(new devtools_discovery::BasicTargetDescriptor(host)); |
| 196 } | 187 } |
| 197 | 188 |
| 198 } // namespace | 189 } // namespace |
| 199 | 190 |
| 200 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { | 191 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { |
| 201 } | 192 } |
| 202 | 193 |
| 203 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { | 194 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { |
| 204 } | 195 } |
| 205 | 196 |
| 206 devtools_discovery::DevToolsTargetDescriptor::List | 197 devtools_discovery::DevToolsTargetDescriptor::List |
| 207 DevToolsDiscoveryProviderAndroid::GetDescriptors() { | 198 DevToolsDiscoveryProviderAndroid::GetDescriptors() { |
| 208 devtools_discovery::DevToolsTargetDescriptor::List result; | 199 devtools_discovery::DevToolsTargetDescriptor::List result; |
| 209 | 200 |
| 210 // Enumerate existing tabs, including the ones with no WebContents. | 201 // Enumerate existing tabs, including the ones with no WebContents. |
| 211 std::set<WebContents*> tab_web_contents; | 202 std::set<WebContents*> tab_web_contents; |
| 212 for (TabModelList::const_iterator iter = TabModelList::begin(); | 203 for (TabModelList::const_iterator iter = TabModelList::begin(); |
| 213 iter != TabModelList::end(); ++iter) { | 204 iter != TabModelList::end(); ++iter) { |
| 214 TabModel* model = *iter; | 205 TabModel* model = *iter; |
| 215 for (int i = 0; i < model->GetTabCount(); ++i) { | 206 for (int i = 0; i < model->GetTabCount(); ++i) { |
| 216 TabAndroid* tab = model->GetTabAt(i); | 207 TabAndroid* tab = model->GetTabAt(i); |
| 217 if (!tab) | 208 if (!tab) |
| 218 continue; | 209 continue; |
| 219 | 210 |
| 220 WebContents* web_contents = tab->web_contents(); | 211 scoped_refptr<content::DevToolsAgentHost> host = |
| 221 if (web_contents) { | 212 DevToolsAgentHost::Create(new TabProxyDelegate(tab)); |
| 222 tab_web_contents.insert(web_contents); | |
| 223 result.push_back(TabDescriptor::CreateForWebContents( | |
| 224 tab->GetAndroidId(), web_contents)); | |
| 225 } else { | |
| 226 result.push_back(TabDescriptor::CreateForUnloadedTab( | |
| 227 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); | |
| 228 } | |
| 229 } | 213 } |
| 230 } | 214 } |
| 231 | 215 |
| 232 // Add descriptors for targets not associated with any tabs. | 216 // Add descriptors for targets not associated with any tabs. |
| 233 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 217 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
| 234 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 218 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
| 235 it != agents.end(); ++it) { | 219 it != agents.end(); ++it) { |
| 236 if (WebContents* web_contents = (*it)->GetWebContents()) { | 220 if (WebContents* web_contents = (*it)->GetWebContents()) { |
| 237 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) | 221 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) |
| 238 continue; | 222 continue; |
| 239 } | 223 } |
| 240 result.push_back(new devtools_discovery::BasicTargetDescriptor(*it)); | 224 result.push_back(new devtools_discovery::BasicTargetDescriptor(*it)); |
| 241 } | 225 } |
| 242 | 226 |
| 243 return result; | 227 return result; |
| 244 } | 228 } |
| 245 | 229 |
| 246 // static | 230 // static |
| 247 void DevToolsDiscoveryProviderAndroid::Install() { | 231 void DevToolsDiscoveryProviderAndroid::Install() { |
| 248 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | 232 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = |
| 249 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | 233 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); |
| 250 discovery_manager->AddProvider( | 234 discovery_manager->AddProvider( |
| 251 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); | 235 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); |
| 252 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | 236 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); |
| 253 } | 237 } |
| OLD | NEW |