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