OLD | NEW |
1 // Copyright 2014 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_manager_delegate_android.h" | 5 #include "chrome/browser/android/dev_tools_discovery_provider_android.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/android/tab_android.h" | 12 #include "chrome/browser/android/tab_android.h" |
12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/history/top_sites_factory.h" | |
14 #include "chrome/browser/profiles/profile_manager.h" | |
15 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 14 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 15 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
17 #include "components/history/core/browser/top_sites.h" | 16 #include "components/devtools_discovery/basic_target_descriptor.h" |
| 17 #include "components/devtools_discovery/devtools_discovery_manager.h" |
18 #include "content/public/browser/devtools_agent_host.h" | 18 #include "content/public/browser/devtools_agent_host.h" |
19 #include "content/public/browser/devtools_target.h" | 19 #include "content/public/browser/devtools_target.h" |
20 #include "content/public/browser/favicon_status.h" | 20 #include "content/public/browser/favicon_status.h" |
21 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
23 | 23 |
24 using content::DevToolsAgentHost; | 24 using content::DevToolsAgentHost; |
25 using content::WebContents; | 25 using content::WebContents; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const char kTargetTypePage[] = "page"; | |
30 const char kTargetTypeServiceWorker[] = "service_worker"; | |
31 const char kTargetTypeOther[] = "other"; | |
32 | |
33 GURL GetFaviconURLForContents(WebContents* web_contents) { | 29 GURL GetFaviconURLForContents(WebContents* web_contents) { |
34 content::NavigationController& controller = web_contents->GetController(); | 30 content::NavigationController& controller = web_contents->GetController(); |
35 content::NavigationEntry* entry = controller.GetActiveEntry(); | 31 content::NavigationEntry* entry = controller.GetActiveEntry(); |
36 if (entry != NULL && entry->GetURL().is_valid()) | 32 if (entry != NULL && entry->GetURL().is_valid()) |
37 return entry->GetFavicon().url; | 33 return entry->GetFavicon().url; |
38 return GURL(); | 34 return GURL(); |
39 } | 35 } |
40 | 36 |
41 GURL GetFaviconURLForAgentHost( | 37 class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { |
42 scoped_refptr<DevToolsAgentHost> agent_host) { | 38 public: |
43 if (WebContents* web_contents = agent_host->GetWebContents()) | 39 static TabDescriptor* CreateForWebContents(int tab_id, |
44 return GetFaviconURLForContents(web_contents); | 40 WebContents* web_contents) { |
45 return GURL(); | 41 return new TabDescriptor(tab_id, web_contents); |
46 } | 42 } |
47 | 43 |
48 base::TimeTicks GetLastActiveTimeForAgentHost( | 44 static TabDescriptor* CreateForUnloadedTab(int tab_id, |
49 scoped_refptr<DevToolsAgentHost> agent_host) { | 45 const base::string16& title, |
50 if (WebContents* web_contents = agent_host->GetWebContents()) | 46 const GURL& url) { |
51 return web_contents->GetLastActiveTime(); | 47 return new TabDescriptor(tab_id, title, url); |
52 return base::TimeTicks(); | 48 } |
53 } | |
54 | 49 |
55 class TargetBase : public content::DevToolsTarget { | 50 ~TabDescriptor() override { |
56 public: | 51 } |
57 // content::DevToolsTarget implementation: | |
58 std::string GetParentId() const override { return std::string(); } | |
59 | 52 |
60 std::string GetTitle() const override { return title_; } | 53 // devtools_discovery::DevToolsTargetDescriptor implementation. |
| 54 std::string GetParentId() const override { |
| 55 return std::string(); |
| 56 } |
61 | 57 |
62 std::string GetDescription() const override { return std::string(); } | 58 std::string GetTitle() const override { |
| 59 return title_; |
| 60 } |
63 | 61 |
64 GURL GetURL() const override { return url_; } | 62 std::string GetDescription() const override { |
| 63 return std::string(); |
| 64 } |
65 | 65 |
66 GURL GetFaviconURL() const override { return favicon_url_; } | 66 GURL GetURL() const override { |
| 67 return url_; |
| 68 } |
| 69 |
| 70 GURL GetFaviconURL() const override { |
| 71 return favicon_url_; |
| 72 } |
67 | 73 |
68 base::TimeTicks GetLastActivityTime() const override { | 74 base::TimeTicks GetLastActivityTime() const override { |
69 return last_activity_time_; | 75 return last_activity_time_; |
70 } | 76 } |
71 | 77 |
72 protected: | 78 std::string GetId() const override { |
73 explicit TargetBase(WebContents* web_contents) | 79 return base::IntToString(tab_id_); |
74 : title_(base::UTF16ToUTF8(web_contents->GetTitle())), | |
75 url_(web_contents->GetURL()), | |
76 favicon_url_(GetFaviconURLForContents(web_contents)), | |
77 last_activity_time_(web_contents->GetLastActiveTime()) { | |
78 } | 80 } |
79 | 81 |
80 explicit TargetBase(scoped_refptr<DevToolsAgentHost> agent_host) | 82 std::string GetType() const override { |
81 : title_(agent_host->GetTitle()), | 83 return devtools_discovery::BasicTargetDescriptor::kTypePage; |
82 url_(agent_host->GetURL()), | |
83 favicon_url_(GetFaviconURLForAgentHost(agent_host)), | |
84 last_activity_time_(GetLastActiveTimeForAgentHost(agent_host)) { | |
85 } | 84 } |
86 | 85 |
87 TargetBase(const std::string& title, const GURL& url) | |
88 : title_(title), | |
89 url_(url) { | |
90 } | |
91 | |
92 private: | |
93 const std::string title_; | |
94 const GURL url_; | |
95 const GURL favicon_url_; | |
96 const base::TimeTicks last_activity_time_; | |
97 }; | |
98 | |
99 class TabTarget : public TargetBase { | |
100 public: | |
101 static TabTarget* CreateForWebContents(int tab_id, | |
102 WebContents* web_contents) { | |
103 return new TabTarget(tab_id, web_contents); | |
104 } | |
105 | |
106 static TabTarget* CreateForUnloadedTab(int tab_id, | |
107 const base::string16& title, | |
108 const GURL& url) { | |
109 return new TabTarget(tab_id, title, url); | |
110 } | |
111 | |
112 // content::DevToolsTarget implementation: | |
113 std::string GetId() const override { return base::IntToString(tab_id_); } | |
114 | |
115 std::string GetType() const override { return kTargetTypePage; } | |
116 | |
117 bool IsAttached() const override { | 86 bool IsAttached() const override { |
118 TabModel* model; | 87 TabModel* model; |
119 int index; | 88 int index; |
120 if (!FindTab(&model, &index)) | 89 if (!FindTab(&model, &index)) |
121 return false; | 90 return false; |
122 WebContents* web_contents = model->GetWebContentsAt(index); | 91 WebContents* web_contents = model->GetWebContentsAt(index); |
123 if (!web_contents) | 92 if (!web_contents) |
124 return false; | 93 return false; |
125 return DevToolsAgentHost::IsDebuggerAttached(web_contents); | 94 return DevToolsAgentHost::IsDebuggerAttached(web_contents); |
126 } | 95 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 bool Close() const override { | 128 bool Close() const override { |
160 TabModel* model; | 129 TabModel* model; |
161 int index; | 130 int index; |
162 if (!FindTab(&model, &index)) | 131 if (!FindTab(&model, &index)) |
163 return false; | 132 return false; |
164 model->CloseTabAt(index); | 133 model->CloseTabAt(index); |
165 return true; | 134 return true; |
166 } | 135 } |
167 | 136 |
168 private: | 137 private: |
169 TabTarget(int tab_id, WebContents* web_contents) | 138 TabDescriptor(int tab_id, WebContents* web_contents) |
170 : TargetBase(web_contents), | 139 : tab_id_(tab_id), |
171 tab_id_(tab_id) { | 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()) { |
172 } | 144 } |
173 | 145 |
174 TabTarget(int tab_id, const base::string16& title, const GURL& url) | 146 TabDescriptor(int tab_id, const base::string16& title, const GURL& url) |
175 : TargetBase(base::UTF16ToUTF8(title), url), | 147 : tab_id_(tab_id), |
176 tab_id_(tab_id) { | 148 title_(base::UTF16ToUTF8(title)), |
| 149 url_(url) { |
177 } | 150 } |
178 | 151 |
179 bool FindTab(TabModel** model_result, int* index_result) const { | 152 bool FindTab(TabModel** model_result, int* index_result) const { |
180 for (TabModelList::const_iterator iter = TabModelList::begin(); | 153 for (TabModelList::const_iterator iter = TabModelList::begin(); |
181 iter != TabModelList::end(); ++iter) { | 154 iter != TabModelList::end(); ++iter) { |
182 TabModel* model = *iter; | 155 TabModel* model = *iter; |
183 for (int i = 0; i < model->GetTabCount(); ++i) { | 156 for (int i = 0; i < model->GetTabCount(); ++i) { |
184 TabAndroid* tab = model->GetTabAt(i); | 157 TabAndroid* tab = model->GetTabAt(i); |
185 if (tab && tab->GetAndroidId() == tab_id_) { | 158 if (tab && tab->GetAndroidId() == tab_id_) { |
186 *model_result = model; | 159 *model_result = model; |
187 *index_result = i; | 160 *index_result = i; |
188 return true; | 161 return true; |
189 } | 162 } |
190 } | 163 } |
191 } | 164 } |
192 return false; | 165 return false; |
193 } | 166 } |
194 | 167 |
195 const int tab_id_; | 168 const int tab_id_; |
| 169 const std::string title_; |
| 170 const GURL url_; |
| 171 const GURL favicon_url_; |
| 172 const base::TimeTicks last_activity_time_; |
| 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(TabDescriptor); |
196 }; | 175 }; |
197 | 176 |
198 class NonTabTarget : public TargetBase { | 177 scoped_ptr<devtools_discovery::DevToolsTargetDescriptor> |
199 public: | 178 CreateNewAndroidTab(const GURL& url) { |
200 explicit NonTabTarget(scoped_refptr<DevToolsAgentHost> agent_host) | 179 if (TabModelList::empty()) |
201 : TargetBase(agent_host), | 180 return scoped_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
202 agent_host_(agent_host) { | |
203 } | |
204 | 181 |
205 // content::DevToolsTarget implementation: | 182 TabModel* tab_model = TabModelList::get(0); |
206 std::string GetId() const override { return agent_host_->GetId(); } | 183 if (!tab_model) |
| 184 return scoped_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
207 | 185 |
208 std::string GetType() const override { | 186 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); |
209 switch (agent_host_->GetType()) { | 187 if (!web_contents) |
210 case DevToolsAgentHost::TYPE_WEB_CONTENTS: | 188 return scoped_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
211 if (TabModelList::begin() == TabModelList::end()) { | |
212 // If there are no tab models we must be running in ChromeShell. | |
213 // Return the 'page' target type for backwards compatibility. | |
214 return kTargetTypePage; | |
215 } | |
216 break; | |
217 case DevToolsAgentHost::TYPE_SERVICE_WORKER: | |
218 return kTargetTypeServiceWorker; | |
219 default: | |
220 break; | |
221 } | |
222 return kTargetTypeOther; | |
223 } | |
224 | 189 |
225 bool IsAttached() const override { return agent_host_->IsAttached(); } | 190 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 191 if (!tab) |
| 192 return scoped_ptr<devtools_discovery::DevToolsTargetDescriptor>(); |
226 | 193 |
227 scoped_refptr<DevToolsAgentHost> GetAgentHost() const override { | 194 return make_scoped_ptr(TabDescriptor::CreateForWebContents( |
228 return agent_host_; | 195 tab->GetAndroidId(), web_contents)); |
229 } | 196 } |
230 | |
231 bool Activate() const override { return agent_host_->Activate(); } | |
232 | |
233 bool Close() const override { return agent_host_->Close(); } | |
234 | |
235 private: | |
236 scoped_refptr<DevToolsAgentHost> agent_host_; | |
237 }; | |
238 | 197 |
239 } // namespace | 198 } // namespace |
240 | 199 |
241 DevToolsManagerDelegateAndroid::DevToolsManagerDelegateAndroid() | 200 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { |
242 : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) { | |
243 } | 201 } |
244 | 202 |
245 DevToolsManagerDelegateAndroid::~DevToolsManagerDelegateAndroid() { | 203 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { |
246 } | 204 } |
247 | 205 |
248 void DevToolsManagerDelegateAndroid::Inspect( | 206 devtools_discovery::DevToolsTargetDescriptor::List |
249 content::BrowserContext* browser_context, | 207 DevToolsDiscoveryProviderAndroid::GetDescriptors() { |
250 content::DevToolsAgentHost* agent_host) { | 208 devtools_discovery::DevToolsTargetDescriptor::List result; |
251 } | |
252 | |
253 base::DictionaryValue* DevToolsManagerDelegateAndroid::HandleCommand( | |
254 content::DevToolsAgentHost* agent_host, | |
255 base::DictionaryValue* command_dict) { | |
256 return network_protocol_handler_->HandleCommand(agent_host, command_dict); | |
257 } | |
258 | |
259 void DevToolsManagerDelegateAndroid::DevToolsAgentStateChanged( | |
260 content::DevToolsAgentHost* agent_host, | |
261 bool attached) { | |
262 network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached); | |
263 } | |
264 | |
265 scoped_ptr<content::DevToolsTarget> | |
266 DevToolsManagerDelegateAndroid::CreateNewTarget(const GURL& url) { | |
267 if (TabModelList::empty()) | |
268 return scoped_ptr<content::DevToolsTarget>(); | |
269 | |
270 TabModel* tab_model = TabModelList::get(0); | |
271 if (!tab_model) | |
272 return scoped_ptr<content::DevToolsTarget>(); | |
273 | |
274 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); | |
275 if (!web_contents) | |
276 return scoped_ptr<content::DevToolsTarget>(); | |
277 | |
278 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
279 if (!tab) | |
280 return scoped_ptr<content::DevToolsTarget>(); | |
281 | |
282 return scoped_ptr<content::DevToolsTarget>( | |
283 TabTarget::CreateForWebContents(tab->GetAndroidId(), web_contents)); | |
284 } | |
285 | |
286 void DevToolsManagerDelegateAndroid::EnumerateTargets(TargetCallback callback) { | |
287 TargetList targets; | |
288 | 209 |
289 // Enumerate existing tabs, including the ones with no WebContents. | 210 // Enumerate existing tabs, including the ones with no WebContents. |
290 std::set<WebContents*> tab_web_contents; | 211 std::set<WebContents*> tab_web_contents; |
291 for (TabModelList::const_iterator iter = TabModelList::begin(); | 212 for (TabModelList::const_iterator iter = TabModelList::begin(); |
292 iter != TabModelList::end(); ++iter) { | 213 iter != TabModelList::end(); ++iter) { |
293 TabModel* model = *iter; | 214 TabModel* model = *iter; |
294 for (int i = 0; i < model->GetTabCount(); ++i) { | 215 for (int i = 0; i < model->GetTabCount(); ++i) { |
295 TabAndroid* tab = model->GetTabAt(i); | 216 TabAndroid* tab = model->GetTabAt(i); |
296 if (!tab) | 217 if (!tab) |
297 continue; | 218 continue; |
298 | 219 |
299 WebContents* web_contents = model->GetWebContentsAt(i); | 220 WebContents* web_contents = tab->web_contents(); |
300 if (web_contents) { | 221 if (web_contents) { |
301 tab_web_contents.insert(web_contents); | 222 tab_web_contents.insert(web_contents); |
302 targets.push_back(TabTarget::CreateForWebContents(tab->GetAndroidId(), | 223 result.push_back(TabDescriptor::CreateForWebContents( |
303 web_contents)); | 224 tab->GetAndroidId(), web_contents)); |
304 } else { | 225 } else { |
305 targets.push_back(TabTarget::CreateForUnloadedTab(tab->GetAndroidId(), | 226 result.push_back(TabDescriptor::CreateForUnloadedTab( |
306 tab->GetTitle(), | 227 tab->GetAndroidId(), tab->GetTitle(), tab->GetURL())); |
307 tab->GetURL())); | |
308 } | 228 } |
309 } | 229 } |
310 } | 230 } |
311 | 231 |
312 // Add targets for WebContents not associated with any tabs. | 232 // Add descriptors for targets not associated with any tabs. |
313 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 233 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); |
314 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 234 for (DevToolsAgentHost::List::iterator it = agents.begin(); |
315 it != agents.end(); ++it) { | 235 it != agents.end(); ++it) { |
316 if (WebContents* web_contents = (*it)->GetWebContents()) { | 236 if (WebContents* web_contents = (*it)->GetWebContents()) { |
317 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) | 237 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) |
318 continue; | 238 continue; |
319 } | 239 } |
320 targets.push_back(new NonTabTarget(*it)); | 240 result.push_back(new devtools_discovery::BasicTargetDescriptor(*it)); |
321 } | 241 } |
322 | 242 |
323 callback.Run(targets); | 243 return result; |
324 } | 244 } |
325 | 245 |
326 std::string DevToolsManagerDelegateAndroid::GetPageThumbnailData( | 246 // static |
327 const GURL& url) { | 247 void DevToolsDiscoveryProviderAndroid::Install() { |
328 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | 248 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = |
329 scoped_refptr<history::TopSites> top_sites = | 249 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); |
330 TopSitesFactory::GetForProfile(profile); | 250 discovery_manager->AddProvider( |
331 if (top_sites) { | 251 make_scoped_ptr(new DevToolsDiscoveryProviderAndroid())); |
332 scoped_refptr<base::RefCountedMemory> data; | 252 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); |
333 if (top_sites->GetPageThumbnail(url, false, &data)) | |
334 return std::string(data->front_as<char>(), data->size()); | |
335 } | |
336 return std::string(); | |
337 } | 253 } |
OLD | NEW |