| 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_splash_image_size_in_dp, |
| 38 int ideal_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_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), |
| 50 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> | 49 ideal_icon_size_in_dp_(ideal_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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (!web_contents() || !weak_observer_) return; | 101 if (!web_contents() || !weak_observer_) return; |
| 104 | 102 |
| 105 if (!manifest.IsEmpty()) { | 103 if (!manifest.IsEmpty()) { |
| 106 content::RecordAction( | 104 content::RecordAction( |
| 107 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 105 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 108 shortcut_info_.UpdateFromManifest(manifest); | 106 shortcut_info_.UpdateFromManifest(manifest); |
| 109 } | 107 } |
| 110 | 108 |
| 111 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 109 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| 112 manifest.icons, | 110 manifest.icons, |
| 113 kPreferredIconSizeInDp, | 111 ideal_icon_size_in_dp_, |
| 114 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 115 | 113 |
| 116 // If fetching the Manifest icon fails, fallback to the best favicon | 114 // If fetching the Manifest icon fails, fallback to the best favicon |
| 117 // for the page. | 115 // for the page. |
| 118 if (!ManifestIconDownloader::Download( | 116 if (!ManifestIconDownloader::Download( |
| 119 web_contents(), | 117 web_contents(), |
| 120 icon_src, | 118 icon_src, |
| 121 kPreferredIconSizeInDp, | 119 ideal_icon_size_in_dp_, |
| 122 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | 120 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
| 123 this))) { | 121 this))) { |
| 124 FetchFavicon(); | 122 FetchFavicon(); |
| 125 } | 123 } |
| 126 | 124 |
| 125 // Save the splash screen URL for the later download. |
| 126 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( |
| 127 manifest.icons, |
| 128 ideal_splash_image_size_in_dp_, |
| 129 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 130 |
| 127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 131 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 128 | 132 |
| 129 // Kick off a timeout for downloading the icon. If an icon isn't set within | 133 // 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. | 134 // the timeout, fall back to using a dynamically-generated launcher icon. |
| 131 icon_timeout_timer_.Start(FROM_HERE, | 135 icon_timeout_timer_.Start(FROM_HERE, |
| 132 base::TimeDelta::FromMilliseconds(3000), | 136 base::TimeDelta::FromMilliseconds(3000), |
| 133 base::Bind( | 137 base::Bind( |
| 134 &AddToHomescreenDataFetcher::OnFaviconFetched, | 138 &AddToHomescreenDataFetcher::OnFaviconFetched, |
| 135 this, | 139 this, |
| 136 favicon_base::FaviconRawBitmapResult())); | 140 favicon_base::FaviconRawBitmapResult())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 148 IPC_MESSAGE_UNHANDLED(handled = false) | 152 IPC_MESSAGE_UNHANDLED(handled = false) |
| 149 IPC_END_MESSAGE_MAP() | 153 IPC_END_MESSAGE_MAP() |
| 150 | 154 |
| 151 return handled; | 155 return handled; |
| 152 } | 156 } |
| 153 | 157 |
| 154 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 158 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 155 DCHECK(!weak_observer_); | 159 DCHECK(!weak_observer_); |
| 156 } | 160 } |
| 157 | 161 |
| 162 void AddToHomescreenDataFetcher::FetchSplashScreenImage( |
| 163 const std::string& webapp_id) { |
| 164 ShortcutHelper::FetchSplashScreenImage( |
| 165 web_contents(), |
| 166 splash_screen_url_, |
| 167 ideal_splash_image_size_in_dp_, |
| 168 webapp_id); |
| 169 } |
| 170 |
| 158 void AddToHomescreenDataFetcher::FetchFavicon() { | 171 void AddToHomescreenDataFetcher::FetchFavicon() { |
| 159 if (!web_contents() || !weak_observer_) return; | 172 if (!web_contents() || !weak_observer_) return; |
| 160 | 173 |
| 161 Profile* profile = | 174 Profile* profile = |
| 162 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 175 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 163 | 176 |
| 164 // Grab the best, largest icon we can find to represent this bookmark. | 177 // Grab the best, largest icon we can find to represent this bookmark. |
| 165 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its | 178 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its |
| 166 // rewrite is further along. | 179 // rewrite is further along. |
| 167 std::vector<int> icon_types; | 180 std::vector<int> icon_types; |
| 168 icon_types.push_back(favicon_base::FAVICON); | 181 icon_types.push_back(favicon_base::FAVICON); |
| 169 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | | 182 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | |
| 170 favicon_base::TOUCH_ICON); | 183 favicon_base::TOUCH_ICON); |
| 171 favicon::FaviconService* favicon_service = | 184 favicon::FaviconService* favicon_service = |
| 172 FaviconServiceFactory::GetForProfile(profile, | 185 FaviconServiceFactory::GetForProfile(profile, |
| 173 ServiceAccessType::EXPLICIT_ACCESS); | 186 ServiceAccessType::EXPLICIT_ACCESS); |
| 174 | 187 |
| 175 // Using favicon if its size is not smaller than platform required size, | 188 // Using favicon if its size is not smaller than platform required size, |
| 176 // otherwise using the largest icon among all avaliable icons. | 189 // otherwise using the largest icon among all avaliable icons. |
| 177 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; | 190 int ideal_icon_size_in_px = ideal_icon_size_in_dp_ * |
| 191 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())-> |
| 192 GetPrimaryDisplay().device_scale_factor(); |
| 193 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1; |
| 178 favicon_service->GetLargestRawFaviconForPageURL( | 194 favicon_service->GetLargestRawFaviconForPageURL( |
| 179 shortcut_info_.url, | 195 shortcut_info_.url, |
| 180 icon_types, | 196 icon_types, |
| 181 threshold_to_get_any_largest_icon, | 197 threshold_to_get_any_largest_icon, |
| 182 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 198 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 183 &favicon_task_tracker_); | 199 &favicon_task_tracker_); |
| 184 } | 200 } |
| 185 | 201 |
| 186 void AddToHomescreenDataFetcher::OnFaviconFetched( | 202 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 187 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 203 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) { | 248 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 249 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 234 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 250 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 235 return; | 251 return; |
| 236 | 252 |
| 237 is_icon_saved_ = true; | 253 is_icon_saved_ = true; |
| 238 shortcut_icon_ = bitmap; | 254 shortcut_icon_ = bitmap; |
| 239 is_ready_ = true; | 255 is_ready_ = true; |
| 240 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 256 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 241 } | 257 } |
| OLD | NEW |