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/webapps/add_to_homescreen_data_fetcher.h" | 5 #include "chrome/browser/android/webapps/add_to_homescreen_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()), |
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 AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( | 58 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
57 const WebApplicationInfo& received_web_app_info) { | 59 const WebApplicationInfo& received_web_app_info) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (!manifest.IsEmpty()) { | 106 if (!manifest.IsEmpty()) { |
105 content::RecordAction( | 107 content::RecordAction( |
106 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 108 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
107 shortcut_info_.UpdateFromManifest(manifest); | 109 shortcut_info_.UpdateFromManifest(manifest); |
108 } | 110 } |
109 | 111 |
110 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 112 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
111 manifest.icons, | 113 manifest.icons, |
112 kPreferredIconSizeInDp, | 114 kPreferredIconSizeInDp, |
113 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 115 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
114 if (icon_src.is_valid()) { | 116 |
115 // Grab the best icon from the manifest. | 117 // If fetching the Manifest icon fails, fallback to the best favicon |
116 web_contents()->DownloadImage( | 118 // for the page. |
| 119 if (!icon_downloader_->Download( |
| 120 web_contents(), |
117 icon_src, | 121 icon_src, |
118 false, | 122 kPreferredIconSizeInDp, |
119 preferred_icon_size_in_px_, | 123 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
120 false, | 124 this))) { |
121 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | |
122 this)); | |
123 } else { | |
124 // Grab the best favicon for the page. | |
125 FetchFavicon(); | 125 FetchFavicon(); |
126 } | 126 } |
127 | 127 |
128 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 128 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
129 | 129 |
130 // Kick off a timeout for downloading the icon. If an icon isn't set within | 130 // Kick off a timeout for downloading the icon. If an icon isn't set within |
131 // the timeout, fall back to using a dynamically-generated launcher icon. | 131 // the timeout, fall back to using a dynamically-generated launcher icon. |
132 icon_timeout_timer_.Start(FROM_HERE, | 132 icon_timeout_timer_.Start(FROM_HERE, |
133 base::TimeDelta::FromMilliseconds(3000), | 133 base::TimeDelta::FromMilliseconds(3000), |
134 base::Bind( | 134 base::Bind( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 favicon_service->GetLargestRawFaviconForPageURL( | 179 favicon_service->GetLargestRawFaviconForPageURL( |
180 shortcut_info_.url, | 180 shortcut_info_.url, |
181 icon_types, | 181 icon_types, |
182 threshold_to_get_any_largest_icon, | 182 threshold_to_get_any_largest_icon, |
183 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 183 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
184 &favicon_task_tracker_); | 184 &favicon_task_tracker_); |
185 } | 185 } |
186 | 186 |
187 void AddToHomescreenDataFetcher::OnFaviconFetched( | 187 void AddToHomescreenDataFetcher::OnFaviconFetched( |
188 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 188 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
189 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | 189 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
190 return; | 190 return; |
191 } | |
192 | 191 |
193 content::BrowserThread::PostTask( | 192 content::BrowserThread::PostTask( |
194 content::BrowserThread::IO, | 193 content::BrowserThread::IO, |
195 FROM_HERE, | 194 FROM_HERE, |
196 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, | 195 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, |
197 this, | 196 this, |
198 bitmap_result)); | 197 bitmap_result)); |
199 } | 198 } |
200 | 199 |
201 void AddToHomescreenDataFetcher::CreateLauncherIcon( | 200 void AddToHomescreenDataFetcher::CreateLauncherIcon( |
(...skipping 14 matching lines...) Expand all Loading... |
216 } | 215 } |
217 | 216 |
218 content::BrowserThread::PostTask( | 217 content::BrowserThread::PostTask( |
219 content::BrowserThread::UI, | 218 content::BrowserThread::UI, |
220 FROM_HERE, | 219 FROM_HERE, |
221 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, | 220 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
222 this, | 221 this, |
223 icon_bitmap)); | 222 icon_bitmap)); |
224 } | 223 } |
225 | 224 |
226 void AddToHomescreenDataFetcher::OnManifestIconFetched( | 225 void AddToHomescreenDataFetcher::OnManifestIconFetched(const SkBitmap& icon) { |
227 int id, | 226 if (bitmap.drawsNothing()) { |
228 int http_status_code, | |
229 const GURL& url, | |
230 const std::vector<SkBitmap>& bitmaps, | |
231 const std::vector<gfx::Size>& sizes) { | |
232 if (!web_contents() || !weak_observer_) return; | |
233 | |
234 // If getting the candidate manifest icon failed, the ShortcutHelper should | |
235 // fallback to the favicon. | |
236 // Otherwise, it sets the state as if there was no manifest icon pending. | |
237 if (bitmaps.empty()) { | |
238 FetchFavicon(); | 227 FetchFavicon(); |
239 return; | 228 return; |
240 } | 229 } |
241 | 230 NotifyObserver(icon); |
242 // There might be multiple bitmaps returned. The one to pick is bigger or | |
243 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | |
244 int preferred_bitmap_index = 0; | |
245 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
246 if (bitmaps[i].height() < preferred_icon_size_in_px_) | |
247 break; | |
248 preferred_bitmap_index = i; | |
249 } | |
250 | |
251 NotifyObserver(bitmaps[preferred_bitmap_index]); | |
252 } | 231 } |
253 | 232 |
254 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 233 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
256 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 235 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
257 return; | 236 return; |
258 | 237 |
259 is_icon_saved_ = true; | 238 is_icon_saved_ = true; |
260 shortcut_icon_ = bitmap; | 239 shortcut_icon_ = bitmap; |
261 is_ready_ = true; | 240 is_ready_ = true; |
262 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 241 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
263 } | 242 } |
OLD | NEW |