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