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/shortcut_data_fetcher.h" | 5 #include "chrome/browser/android/shortcut_data_fetcher.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/task/cancelable_task_tracker.h" | 10 #include "base/task/cancelable_task_tracker.h" |
11 #include "chrome/browser/favicon/favicon_service_factory.h" | 11 #include "chrome/browser/favicon/favicon_service_factory.h" |
12 #include "chrome/browser/manifest/manifest_icon_downloader.h" | |
12 #include "chrome/browser/manifest/manifest_icon_selector.h" | 13 #include "chrome/browser/manifest/manifest_icon_selector.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
16 #include "chrome/common/web_application_info.h" | 17 #include "chrome/common/web_application_info.h" |
17 #include "components/dom_distiller/core/url_utils.h" | 18 #include "components/dom_distiller/core/url_utils.h" |
18 #include "components/favicon/core/favicon_service.h" | 19 #include "components/favicon/core/favicon_service.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/user_metrics.h" | 21 #include "content/public/browser/user_metrics.h" |
21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
38 content::WebContents* web_contents, | 39 content::WebContents* web_contents, |
39 Observer* observer) | 40 Observer* observer) |
40 : WebContentsObserver(web_contents), | 41 : WebContentsObserver(web_contents), |
41 weak_observer_(observer), | 42 weak_observer_(observer), |
42 is_waiting_for_web_application_info_(false), | 43 is_waiting_for_web_application_info_(false), |
43 is_icon_saved_(false), | 44 is_icon_saved_(false), |
44 is_ready_(false), | 45 is_ready_(false), |
45 icon_timeout_timer_(false, false), | 46 icon_timeout_timer_(false, false), |
46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 47 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
47 web_contents->GetURL())), | 48 web_contents->GetURL())), |
49 icon_downloader_(new ManifestIconDownloader(web_contents)), | |
48 preferred_icon_size_in_px_(kPreferredIconSizeInDp * | 50 preferred_icon_size_in_px_(kPreferredIconSizeInDp * |
49 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> | 51 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> |
50 GetPrimaryDisplay().device_scale_factor()) { | 52 GetPrimaryDisplay().device_scale_factor()) { |
51 // Send a message to the renderer to retrieve information about the page. | 53 // Send a message to the renderer to retrieve information about the page. |
52 is_waiting_for_web_application_info_ = true; | 54 is_waiting_for_web_application_info_ = true; |
53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 55 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
54 } | 56 } |
55 | 57 |
56 void ShortcutDataFetcher::OnDidGetWebApplicationInfo( | 58 void ShortcutDataFetcher::OnDidGetWebApplicationInfo( |
57 const WebApplicationInfo& received_web_app_info) { | 59 const WebApplicationInfo& received_web_app_info) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 content::RecordAction( | 104 content::RecordAction( |
103 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 105 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
104 } | 106 } |
105 | 107 |
106 shortcut_info_.UpdateFromManifest(manifest); | 108 shortcut_info_.UpdateFromManifest(manifest); |
107 | 109 |
108 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 110 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
109 manifest.icons, | 111 manifest.icons, |
110 kPreferredIconSizeInDp, | 112 kPreferredIconSizeInDp, |
111 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 113 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
112 if (icon_src.is_valid()) { | 114 |
113 // Grab the best icon from the manifest. | 115 // If fetching the Manifest icon fails, fallback to the best favicon |
114 web_contents()->DownloadImage( | 116 // for the page. |
117 if (!icon_downloader_->Download( | |
115 icon_src, | 118 icon_src, |
116 false, | 119 kPreferredIconSizeInDp, |
117 preferred_icon_size_in_px_, | 120 base::Bind(&ShortcutDataFetcher::NotifyObserver, |
118 false, | 121 this))) { |
119 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched, | |
120 this)); | |
121 } else { | |
122 // Grab the best favicon for the page. | |
123 FetchFavicon(); | 122 FetchFavicon(); |
124 } | 123 } |
125 | 124 |
126 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 125 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
127 | 126 |
128 // Kick off a timeout for downloading the icon. If an icon isn't set within | 127 // Kick off a timeout for downloading the icon. If an icon isn't set within |
129 // the timeout, fall back to using a dynamically-generated launcher icon. | 128 // the timeout, fall back to using a dynamically-generated launcher icon. |
130 icon_timeout_timer_.Start(FROM_HERE, | 129 icon_timeout_timer_.Start(FROM_HERE, |
131 base::TimeDelta::FromMilliseconds(3000), | 130 base::TimeDelta::FromMilliseconds(3000), |
132 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, | 131 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 favicon_service->GetLargestRawFaviconForPageURL( | 174 favicon_service->GetLargestRawFaviconForPageURL( |
176 shortcut_info_.url, | 175 shortcut_info_.url, |
177 icon_types, | 176 icon_types, |
178 threshold_to_get_any_largest_icon, | 177 threshold_to_get_any_largest_icon, |
179 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), | 178 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), |
180 &favicon_task_tracker_); | 179 &favicon_task_tracker_); |
181 } | 180 } |
182 | 181 |
183 void ShortcutDataFetcher::OnFaviconFetched( | 182 void ShortcutDataFetcher::OnFaviconFetched( |
184 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 183 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
185 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | 184 if (!web_contents() || !weak_observer_ || is_icon_saved_) return; |
mlamouri (slow - plz ping)
2015/08/10 15:08:08
nit: in C++, |return;| should be in a new line, co
Lalit Maganti
2015/08/10 16:55:45
Done - although there are a lot of cases in this f
| |
186 return; | |
187 } | |
188 | 185 |
189 content::BrowserThread::PostTask( | 186 content::BrowserThread::PostTask( |
190 content::BrowserThread::IO, | 187 content::BrowserThread::IO, |
191 FROM_HERE, | 188 FROM_HERE, |
192 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, | 189 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, |
193 this, | 190 this, |
194 bitmap_result)); | 191 bitmap_result)); |
195 } | 192 } |
196 | 193 |
197 void ShortcutDataFetcher::CreateLauncherIcon( | 194 void ShortcutDataFetcher::CreateLauncherIcon( |
(...skipping 12 matching lines...) Expand all Loading... | |
210 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, | 207 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, |
211 shortcut_info_.url); | 208 shortcut_info_.url); |
212 } | 209 } |
213 | 210 |
214 content::BrowserThread::PostTask( | 211 content::BrowserThread::PostTask( |
215 content::BrowserThread::UI, | 212 content::BrowserThread::UI, |
216 FROM_HERE, | 213 FROM_HERE, |
217 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); | 214 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); |
218 } | 215 } |
219 | 216 |
220 void ShortcutDataFetcher::OnManifestIconFetched( | |
221 int id, | |
222 int http_status_code, | |
223 const GURL& url, | |
224 const std::vector<SkBitmap>& bitmaps, | |
225 const std::vector<gfx::Size>& sizes) { | |
226 if (!web_contents() || !weak_observer_) return; | |
227 | |
228 // If getting the candidate manifest icon failed, the ShortcutHelper should | |
229 // fallback to the favicon. | |
230 // Otherwise, it sets the state as if there was no manifest icon pending. | |
231 if (bitmaps.empty()) { | |
232 FetchFavicon(); | |
233 return; | |
234 } | |
235 | |
236 // There might be multiple bitmaps returned. The one to pick is bigger or | |
237 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | |
238 int preferred_bitmap_index = 0; | |
239 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
240 if (bitmaps[i].height() < preferred_icon_size_in_px_) | |
241 break; | |
242 preferred_bitmap_index = i; | |
243 } | |
244 | |
245 NotifyObserver(bitmaps[preferred_bitmap_index]); | |
246 } | |
247 | |
248 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 217 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 218 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
250 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 219 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
251 return; | 220 return; |
252 | 221 |
253 is_icon_saved_ = true; | 222 is_icon_saved_ = true; |
254 shortcut_icon_ = bitmap; | 223 shortcut_icon_ = bitmap; |
255 is_ready_ = true; | 224 is_ready_ = true; |
256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 225 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
257 } | 226 } |
OLD | NEW |