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 13 matching lines...) Expand all Loading... | |
59 } | 54 } |
60 | 55 |
61 std::string GetDescription() const override { | 56 std::string GetDescription() const override { |
62 return std::string(); | 57 return std::string(); |
63 } | 58 } |
64 | 59 |
65 GURL GetURL() const override { | 60 GURL GetURL() const override { |
66 return url_; | 61 return url_; |
67 } | 62 } |
68 | 63 |
69 GURL GetFaviconURL() const override { | 64 GURL GetFaviconURL() const override { |
dgozman
2015/05/13 01:12:21
Here is the similar method. Please rename private
vkuzkokov
2015/05/13 16:40:56
Done.
| |
70 return favicon_url_; | 65 return favicon_url_; |
71 } | 66 } |
72 | 67 |
73 base::TimeTicks GetLastActivityTime() const override { | 68 base::TimeTicks GetLastActivityTime() const override { |
74 return last_activity_time_; | 69 return last_activity_time_; |
75 } | 70 } |
76 | 71 |
77 std::string GetId() const override { | 72 std::string GetId() const override { |
78 return base::IntToString(tab_id_); | 73 return base::IntToString(tab_id_); |
79 } | 74 } |
(...skipping 51 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_(GetFaviconURL()), |
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_(GetFaviconURL()) { | |
145 } | |
146 | |
147 GURL GetFaviconURL() { | |
148 TabModel* model; | |
149 int index; | |
150 if (FindTab(&model, &index)) { | |
151 if (TabAndroid* tab = model->GetTabAt(index)) { | |
dgozman
2015/05/13 01:12:22
Let's have early return instead.
vkuzkokov
2015/05/13 16:40:56
Done.
| |
152 SkBitmap bitmap = tab->GetFaviconBitmap(); | |
153 if (bitmap.empty()) | |
154 return GURL(); | |
155 | |
156 std::vector<unsigned char> data; | |
157 SkAutoLockPixels lock_image(bitmap); | |
158 bool encoded = gfx::PNGCodec::Encode( | |
dgozman
2015/05/13 01:12:21
I don't like sync encoding on UI. Let's make GetFa
vkuzkokov
2015/05/13 16:40:56
With FastEncodeBGRASkBitmap it's 1ms per tab compa
dgozman
2015/05/13 21:58:04
You are right, it's just a small favicon.
| |
159 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
160 gfx::PNGCodec::FORMAT_SkBitmap, | |
161 gfx::Size(bitmap.width(), bitmap.height()), | |
162 bitmap.width() * bitmap.bytesPerPixel(), | |
163 false, std::vector<gfx::PNGCodec::Comment>(), &data); | |
dgozman
2015/05/13 01:12:21
nit: one parameter per line?
vkuzkokov
2015/05/13 16:40:56
Done.
| |
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 } | |
173 } | |
174 return GURL(); | |
149 } | 175 } |
150 | 176 |
151 bool FindTab(TabModel** model_result, int* index_result) const { | 177 bool FindTab(TabModel** model_result, int* index_result) const { |
152 for (TabModelList::const_iterator iter = TabModelList::begin(); | 178 for (TabModelList::const_iterator iter = TabModelList::begin(); |
153 iter != TabModelList::end(); ++iter) { | 179 iter != TabModelList::end(); ++iter) { |
154 TabModel* model = *iter; | 180 TabModel* model = *iter; |
155 for (int i = 0; i < model->GetTabCount(); ++i) { | 181 for (int i = 0; i < model->GetTabCount(); ++i) { |
156 TabAndroid* tab = model->GetTabAt(i); | 182 TabAndroid* tab = model->GetTabAt(i); |
157 if (tab && tab->GetAndroidId() == tab_id_) { | 183 if (tab && tab->GetAndroidId() == tab_id_) { |
158 *model_result = model; | 184 *model_result = model; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 } | 269 } |
244 | 270 |
245 // static | 271 // static |
246 void DevToolsDiscoveryProviderAndroid::Install() { | 272 void DevToolsDiscoveryProviderAndroid::Install() { |
247 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | 273 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = |
248 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | 274 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); |
249 discovery_manager->AddProvider( | 275 discovery_manager->AddProvider( |
250 make_scoped_ptr(new DevToolsDiscoveryProviderAndroid())); | 276 make_scoped_ptr(new DevToolsDiscoveryProviderAndroid())); |
251 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | 277 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); |
252 } | 278 } |
OLD | NEW |