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