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/base64.h" | |
8 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
9 #include "base/bind.h" | 8 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/android/tab_android.h" | 12 #include "chrome/browser/android/tab_android.h" |
14 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.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/devtools_discovery/basic_target_descriptor.h" | 16 #include "components/devtools_discovery/basic_target_descriptor.h" |
18 #include "components/devtools_discovery/devtools_discovery_manager.h" | 17 #include "components/devtools_discovery/devtools_discovery_manager.h" |
19 #include "content/public/browser/devtools_agent_host.h" | 18 #include "content/public/browser/devtools_agent_host.h" |
20 #include "content/public/browser/favicon_status.h" | 19 #include "content/public/browser/favicon_status.h" |
21 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
22 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
23 #include "third_party/skia/include/core/SkBitmap.h" | |
24 #include "ui/gfx/codec/png_codec.h" | |
25 | 22 |
26 using content::DevToolsAgentHost; | 23 using content::DevToolsAgentHost; |
27 using content::WebContents; | 24 using content::WebContents; |
28 | 25 |
29 namespace { | 26 namespace { |
30 | 27 |
| 28 GURL GetFaviconURLForContents(WebContents* web_contents) { |
| 29 content::NavigationController& controller = web_contents->GetController(); |
| 30 content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 31 if (entry != NULL && entry->GetURL().is_valid()) |
| 32 return entry->GetFavicon().url; |
| 33 return GURL(); |
| 34 } |
| 35 |
31 class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { | 36 class TabDescriptor : public devtools_discovery::DevToolsTargetDescriptor { |
32 public: | 37 public: |
33 static TabDescriptor* CreateForWebContents(int tab_id, | 38 static TabDescriptor* CreateForWebContents(int tab_id, |
34 WebContents* web_contents) { | 39 WebContents* web_contents) { |
35 return new TabDescriptor(tab_id, web_contents); | 40 return new TabDescriptor(tab_id, web_contents); |
36 } | 41 } |
37 | 42 |
38 static TabDescriptor* CreateForUnloadedTab(int tab_id, | 43 static TabDescriptor* CreateForUnloadedTab(int tab_id, |
39 const base::string16& title, | 44 const base::string16& title, |
40 const GURL& url) { | 45 const GURL& url) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 return false; | 131 return false; |
127 model->CloseTabAt(index); | 132 model->CloseTabAt(index); |
128 return true; | 133 return true; |
129 } | 134 } |
130 | 135 |
131 private: | 136 private: |
132 TabDescriptor(int tab_id, WebContents* web_contents) | 137 TabDescriptor(int tab_id, WebContents* web_contents) |
133 : tab_id_(tab_id), | 138 : tab_id_(tab_id), |
134 title_(base::UTF16ToUTF8(web_contents->GetTitle())), | 139 title_(base::UTF16ToUTF8(web_contents->GetTitle())), |
135 url_(web_contents->GetURL()), | 140 url_(web_contents->GetURL()), |
136 favicon_url_(CalculateFaviconURL()), | 141 favicon_url_(GetFaviconURLForContents(web_contents)), |
137 last_activity_time_(web_contents->GetLastActiveTime()) { | 142 last_activity_time_(web_contents->GetLastActiveTime()) { |
138 } | 143 } |
139 | 144 |
140 TabDescriptor(int tab_id, const base::string16& title, const GURL& url) | 145 TabDescriptor(int tab_id, const base::string16& title, const GURL& url) |
141 : tab_id_(tab_id), | 146 : tab_id_(tab_id), |
142 title_(base::UTF16ToUTF8(title)), | 147 title_(base::UTF16ToUTF8(title)), |
143 url_(url), | 148 url_(url) { |
144 favicon_url_(CalculateFaviconURL()) { | |
145 } | |
146 | |
147 GURL CalculateFaviconURL() { | |
148 TabModel* model; | |
149 int index; | |
150 if (!FindTab(&model, &index)) | |
151 return GURL(); | |
152 | |
153 TabAndroid* tab = model->GetTabAt(index); | |
154 if (!tab) | |
155 return GURL(); | |
156 | |
157 SkBitmap bitmap = tab->GetFaviconBitmap(); | |
158 if (bitmap.empty()) | |
159 return GURL(); | |
160 | |
161 std::vector<unsigned char> data; | |
162 SkAutoLockPixels lock_image(bitmap); | |
163 bool encoded = gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &data); | |
164 if (!encoded) | |
165 return GURL(); | |
166 | |
167 std::string base_64_data; | |
168 base::Base64Encode( | |
169 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), | |
170 &base_64_data); | |
171 return GURL("data:image/png;base64," + base_64_data); | |
172 } | 149 } |
173 | 150 |
174 bool FindTab(TabModel** model_result, int* index_result) const { | 151 bool FindTab(TabModel** model_result, int* index_result) const { |
175 for (TabModelList::const_iterator iter = TabModelList::begin(); | 152 for (TabModelList::const_iterator iter = TabModelList::begin(); |
176 iter != TabModelList::end(); ++iter) { | 153 iter != TabModelList::end(); ++iter) { |
177 TabModel* model = *iter; | 154 TabModel* model = *iter; |
178 for (int i = 0; i < model->GetTabCount(); ++i) { | 155 for (int i = 0; i < model->GetTabCount(); ++i) { |
179 TabAndroid* tab = model->GetTabAt(i); | 156 TabAndroid* tab = model->GetTabAt(i); |
180 if (tab && tab->GetAndroidId() == tab_id_) { | 157 if (tab && tab->GetAndroidId() == tab_id_) { |
181 *model_result = model; | 158 *model_result = model; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 } | 243 } |
267 | 244 |
268 // static | 245 // static |
269 void DevToolsDiscoveryProviderAndroid::Install() { | 246 void DevToolsDiscoveryProviderAndroid::Install() { |
270 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | 247 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = |
271 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | 248 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); |
272 discovery_manager->AddProvider( | 249 discovery_manager->AddProvider( |
273 make_scoped_ptr(new DevToolsDiscoveryProviderAndroid())); | 250 make_scoped_ptr(new DevToolsDiscoveryProviderAndroid())); |
274 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | 251 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); |
275 } | 252 } |
OLD | NEW |