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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (!manifest.IsEmpty()) { | 105 if (!manifest.IsEmpty()) { |
105 content::RecordAction( | 106 content::RecordAction( |
106 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 107 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
107 shortcut_info_.UpdateFromManifest(manifest); | 108 shortcut_info_.UpdateFromManifest(manifest); |
108 } | 109 } |
109 | 110 |
110 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 111 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
111 manifest.icons, | 112 manifest.icons, |
112 kPreferredIconSizeInDp, | 113 kPreferredIconSizeInDp, |
113 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 114 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
114 if (icon_src.is_valid()) { | 115 |
115 // Grab the best icon from the manifest. | 116 // If fetching the Manifest icon fails, fallback to the best favicon |
116 web_contents()->DownloadImage( | 117 // for the page. |
| 118 if (!ManifestIconDownloader::Download( |
| 119 web_contents(), |
117 icon_src, | 120 icon_src, |
118 false, | 121 kPreferredIconSizeInDp, |
119 preferred_icon_size_in_px_, | |
120 false, | |
121 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | 122 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
122 this)); | 123 this))) { |
123 } else { | |
124 // Grab the best favicon for the page. | |
125 FetchFavicon(); | 124 FetchFavicon(); |
126 } | 125 } |
127 | 126 |
128 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
129 | 128 |
130 // Kick off a timeout for downloading the icon. If an icon isn't set within | 129 // 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. | 130 // the timeout, fall back to using a dynamically-generated launcher icon. |
132 icon_timeout_timer_.Start(FROM_HERE, | 131 icon_timeout_timer_.Start(FROM_HERE, |
133 base::TimeDelta::FromMilliseconds(3000), | 132 base::TimeDelta::FromMilliseconds(3000), |
134 base::Bind( | 133 base::Bind( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 favicon_service->GetLargestRawFaviconForPageURL( | 178 favicon_service->GetLargestRawFaviconForPageURL( |
180 shortcut_info_.url, | 179 shortcut_info_.url, |
181 icon_types, | 180 icon_types, |
182 threshold_to_get_any_largest_icon, | 181 threshold_to_get_any_largest_icon, |
183 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 182 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
184 &favicon_task_tracker_); | 183 &favicon_task_tracker_); |
185 } | 184 } |
186 | 185 |
187 void AddToHomescreenDataFetcher::OnFaviconFetched( | 186 void AddToHomescreenDataFetcher::OnFaviconFetched( |
188 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 187 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
189 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | 188 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
190 return; | 189 return; |
191 } | |
192 | 190 |
193 content::BrowserThread::PostTask( | 191 content::BrowserThread::PostTask( |
194 content::BrowserThread::IO, | 192 content::BrowserThread::IO, |
195 FROM_HERE, | 193 FROM_HERE, |
196 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, | 194 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, |
197 this, | 195 this, |
198 bitmap_result)); | 196 bitmap_result)); |
199 } | 197 } |
200 | 198 |
201 void AddToHomescreenDataFetcher::CreateLauncherIcon( | 199 void AddToHomescreenDataFetcher::CreateLauncherIcon( |
(...skipping 14 matching lines...) Expand all Loading... |
216 } | 214 } |
217 | 215 |
218 content::BrowserThread::PostTask( | 216 content::BrowserThread::PostTask( |
219 content::BrowserThread::UI, | 217 content::BrowserThread::UI, |
220 FROM_HERE, | 218 FROM_HERE, |
221 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, | 219 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
222 this, | 220 this, |
223 icon_bitmap)); | 221 icon_bitmap)); |
224 } | 222 } |
225 | 223 |
226 void AddToHomescreenDataFetcher::OnManifestIconFetched( | 224 void AddToHomescreenDataFetcher::OnManifestIconFetched(const SkBitmap& icon) { |
227 int id, | 225 if (icon.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(); | 226 FetchFavicon(); |
239 return; | 227 return; |
240 } | 228 } |
241 | 229 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 } | 230 } |
253 | 231 |
254 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 232 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
256 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 234 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
257 return; | 235 return; |
258 | 236 |
259 is_icon_saved_ = true; | 237 is_icon_saved_ = true; |
260 shortcut_icon_ = bitmap; | 238 shortcut_icon_ = bitmap; |
261 is_ready_ = true; | 239 is_ready_ = true; |
262 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 240 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
263 } | 241 } |
OLD | NEW |