Chromium Code Reviews| 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/android/shortcut_helper.h" | |
| 11 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 12 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 13 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| 13 #include "chrome/browser/manifest/manifest_icon_selector.h" | 14 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
| 17 #include "chrome/common/web_application_info.h" | 18 #include "chrome/common/web_application_info.h" |
| 18 #include "components/dom_distiller/core/url_utils.h" | 19 #include "components/dom_distiller/core/url_utils.h" |
| 19 #include "components/favicon/core/favicon_service.h" | 20 #include "components/favicon/core/favicon_service.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/user_metrics.h" | 22 #include "content/public/browser/user_metrics.h" |
| 22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_contents_observer.h" | 24 #include "content/public/browser/web_contents_observer.h" |
| 24 #include "content/public/common/frame_navigate_params.h" | 25 #include "content/public/common/frame_navigate_params.h" |
| 25 #include "content/public/common/manifest.h" | 26 #include "content/public/common/manifest.h" |
| 26 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h" | 27 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h" |
| 27 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
| 28 #include "ui/gfx/favicon_size.h" | 29 #include "ui/gfx/favicon_size.h" |
| 29 #include "ui/gfx/screen.h" | 30 #include "ui/gfx/screen.h" |
| 30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 31 | 32 |
| 32 using content::Manifest; | 33 using content::Manifest; |
| 33 | 34 |
| 34 // Android's preferred icon size in DP is 48, as defined in | |
| 35 // http://developer.android.com/design/style/iconography.html | |
| 36 const int AddToHomescreenDataFetcher::kPreferredIconSizeInDp = 48; | |
| 37 | |
| 38 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 35 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 39 content::WebContents* web_contents, | 36 content::WebContents* web_contents, |
| 37 int ideal_icon_size_in_dp, | |
| 38 int ideal_splash_icon_size_in_dp, | |
| 40 Observer* observer) | 39 Observer* observer) |
| 41 : WebContentsObserver(web_contents), | 40 : WebContentsObserver(web_contents), |
| 42 weak_observer_(observer), | 41 weak_observer_(observer), |
| 43 is_waiting_for_web_application_info_(false), | 42 is_waiting_for_web_application_info_(false), |
| 44 is_icon_saved_(false), | 43 is_icon_saved_(false), |
| 45 is_ready_(false), | 44 is_ready_(false), |
| 46 icon_timeout_timer_(false, false), | 45 icon_timeout_timer_(false, false), |
| 47 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 48 web_contents->GetURL())), | 47 web_contents->GetURL())), |
| 49 preferred_icon_size_in_px_(kPreferredIconSizeInDp * | 48 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), |
| 50 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> | 49 ideal_splash_icon_size_in_dp_(ideal_splash_icon_size_in_dp) { |
| 51 GetPrimaryDisplay().device_scale_factor()) { | |
| 52 // Send a message to the renderer to retrieve information about the page. | 50 // Send a message to the renderer to retrieve information about the page. |
| 53 is_waiting_for_web_application_info_ = true; | 51 is_waiting_for_web_application_info_ = true; |
| 54 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 52 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 55 } | 53 } |
| 56 | 54 |
| 57 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( | 55 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| 58 const WebApplicationInfo& received_web_app_info) { | 56 const WebApplicationInfo& received_web_app_info) { |
| 59 is_waiting_for_web_application_info_ = false; | 57 is_waiting_for_web_application_info_ = false; |
| 60 if (!web_contents() || !weak_observer_) return; | 58 if (!web_contents() || !weak_observer_) return; |
| 61 | 59 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 } | 93 } |
| 96 | 94 |
| 97 web_contents()->GetManifest( | 95 web_contents()->GetManifest( |
| 98 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); | 96 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); |
| 99 } | 97 } |
| 100 | 98 |
| 101 void AddToHomescreenDataFetcher::OnDidGetManifest( | 99 void AddToHomescreenDataFetcher::OnDidGetManifest( |
| 102 const content::Manifest& manifest) { | 100 const content::Manifest& manifest) { |
| 103 if (!web_contents() || !weak_observer_) return; | 101 if (!web_contents() || !weak_observer_) return; |
| 104 | 102 |
| 103 manifest_ = manifest; | |
| 105 if (!manifest.IsEmpty()) { | 104 if (!manifest.IsEmpty()) { |
| 106 content::RecordAction( | 105 content::RecordAction( |
| 107 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 106 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 108 shortcut_info_.UpdateFromManifest(manifest); | 107 shortcut_info_.UpdateFromManifest(manifest); |
| 109 } | 108 } |
| 110 | 109 |
| 111 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 110 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| 112 manifest.icons, | 111 manifest.icons, |
| 113 kPreferredIconSizeInDp, | 112 ideal_icon_size_in_dp_, |
| 114 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 113 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 115 | 114 |
| 116 // If fetching the Manifest icon fails, fallback to the best favicon | 115 // If fetching the Manifest icon fails, fallback to the best favicon |
| 117 // for the page. | 116 // for the page. |
| 118 if (!ManifestIconDownloader::Download( | 117 if (!ManifestIconDownloader::Download( |
| 119 web_contents(), | 118 web_contents(), |
| 120 icon_src, | 119 icon_src, |
| 121 kPreferredIconSizeInDp, | 120 ideal_icon_size_in_dp_, |
| 122 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | 121 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
| 123 this))) { | 122 this))) { |
| 124 FetchFavicon(); | 123 FetchFavicon(); |
| 125 } | 124 } |
| 126 | 125 |
| 127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 126 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 128 | 127 |
| 129 // Kick off a timeout for downloading the icon. If an icon isn't set within | 128 // 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. | 129 // the timeout, fall back to using a dynamically-generated launcher icon. |
| 131 icon_timeout_timer_.Start(FROM_HERE, | 130 icon_timeout_timer_.Start(FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 148 IPC_MESSAGE_UNHANDLED(handled = false) | 147 IPC_MESSAGE_UNHANDLED(handled = false) |
| 149 IPC_END_MESSAGE_MAP() | 148 IPC_END_MESSAGE_MAP() |
| 150 | 149 |
| 151 return handled; | 150 return handled; |
| 152 } | 151 } |
| 153 | 152 |
| 154 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 153 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 155 DCHECK(!weak_observer_); | 154 DCHECK(!weak_observer_); |
| 156 } | 155 } |
| 157 | 156 |
| 157 void AddToHomescreenDataFetcher::FetchSplashscreenIcon(const std::string& id) { | |
| 158 if (manifest_.IsEmpty()) | |
| 159 return; | |
| 160 | |
| 161 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | |
| 162 manifest_.icons, | |
| 163 ideal_splash_icon_size_in_dp_, | |
| 164 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | |
| 165 | |
| 166 // This is a fire and forget task. It is not vital for the splashscreen icon | |
| 167 // to be downloaded so the result of the download is not checked and neither | |
| 168 // is there any fallback option. | |
| 169 ManifestIconDownloader::Download( | |
| 170 web_contents(), | |
| 171 icon_src, | |
| 172 ideal_splash_icon_size_in_dp_, | |
| 173 base::Bind(&ShortcutHelper::AddSplashscreenIconToWebappData, | |
| 174 id)); | |
|
mlamouri (slow - plz ping)
2015/08/24 16:43:54
nit: "id));" doesn't fit in the previous line?
Lalit Maganti
2015/08/25 11:38:44
Changed to webapp_id which doesn't fit :/
| |
| 175 } | |
| 176 | |
| 158 void AddToHomescreenDataFetcher::FetchFavicon() { | 177 void AddToHomescreenDataFetcher::FetchFavicon() { |
| 159 if (!web_contents() || !weak_observer_) return; | 178 if (!web_contents() || !weak_observer_) return; |
| 160 | 179 |
| 161 Profile* profile = | 180 Profile* profile = |
| 162 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 181 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 163 | 182 |
| 164 // Grab the best, largest icon we can find to represent this bookmark. | 183 // Grab the best, largest icon we can find to represent this bookmark. |
| 165 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its | 184 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its |
| 166 // rewrite is further along. | 185 // rewrite is further along. |
| 167 std::vector<int> icon_types; | 186 std::vector<int> icon_types; |
| 168 icon_types.push_back(favicon_base::FAVICON); | 187 icon_types.push_back(favicon_base::FAVICON); |
| 169 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | | 188 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | |
| 170 favicon_base::TOUCH_ICON); | 189 favicon_base::TOUCH_ICON); |
| 171 favicon::FaviconService* favicon_service = | 190 favicon::FaviconService* favicon_service = |
| 172 FaviconServiceFactory::GetForProfile(profile, | 191 FaviconServiceFactory::GetForProfile(profile, |
| 173 ServiceAccessType::EXPLICIT_ACCESS); | 192 ServiceAccessType::EXPLICIT_ACCESS); |
| 174 | 193 |
| 175 // Using favicon if its size is not smaller than platform required size, | 194 // Using favicon if its size is not smaller than platform required size, |
| 176 // otherwise using the largest icon among all avaliable icons. | 195 // otherwise using the largest icon among all avaliable icons. |
| 177 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; | 196 int ideal_icon_size_in_px = ideal_icon_size_in_dp_ * |
| 197 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())-> | |
| 198 GetPrimaryDisplay().device_scale_factor(); | |
| 199 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1; | |
| 178 favicon_service->GetLargestRawFaviconForPageURL( | 200 favicon_service->GetLargestRawFaviconForPageURL( |
| 179 shortcut_info_.url, | 201 shortcut_info_.url, |
| 180 icon_types, | 202 icon_types, |
| 181 threshold_to_get_any_largest_icon, | 203 threshold_to_get_any_largest_icon, |
| 182 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 204 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 183 &favicon_task_tracker_); | 205 &favicon_task_tracker_); |
| 184 } | 206 } |
| 185 | 207 |
| 186 void AddToHomescreenDataFetcher::OnFaviconFetched( | 208 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 187 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 209 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 254 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 234 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 256 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 235 return; | 257 return; |
| 236 | 258 |
| 237 is_icon_saved_ = true; | 259 is_icon_saved_ = true; |
| 238 shortcut_icon_ = bitmap; | 260 shortcut_icon_ = bitmap; |
| 239 is_ready_ = true; | 261 is_ready_ = true; |
| 240 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 262 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 241 } | 263 } |
| OLD | NEW |