| 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 <vector> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 11 #include "base/task/cancelable_task_tracker.h" | |
| 12 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 13 #include "chrome/browser/android/shortcut_helper.h" | 14 #include "chrome/browser/android/shortcut_helper.h" |
| 14 #include "chrome/browser/favicon/favicon_service_factory.h" | 15 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 15 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 16 #include "chrome/browser/installable/installable_manager.h" |
| 16 #include "chrome/browser/manifest/manifest_icon_selector.h" | 17 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 19 #include "chrome/common/render_messages.h" | 20 #include "chrome/common/render_messages.h" |
| 20 #include "chrome/common/web_application_info.h" | 21 #include "chrome/common/web_application_info.h" |
| 21 #include "components/dom_distiller/core/url_utils.h" | 22 #include "components/dom_distiller/core/url_utils.h" |
| 22 #include "components/favicon/core/favicon_service.h" | 23 #include "components/favicon/core/favicon_service.h" |
| 24 #include "components/favicon_base/favicon_types.h" |
| 23 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/user_metrics.h" | 26 #include "content/public/browser/user_metrics.h" |
| 25 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/browser/web_contents_observer.h" | 28 #include "content/public/browser/web_contents_observer.h" |
| 27 #include "content/public/common/frame_navigate_params.h" | |
| 28 #include "content/public/common/manifest.h" | 29 #include "content/public/common/manifest.h" |
| 29 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" | 30 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" |
| 30 #include "ui/display/display.h" | 31 #include "ui/display/display.h" |
| 31 #include "ui/display/screen.h" | 32 #include "ui/display/screen.h" |
| 32 #include "ui/gfx/codec/png_codec.h" | 33 #include "ui/gfx/codec/png_codec.h" |
| 33 #include "ui/gfx/favicon_size.h" | 34 #include "ui/gfx/favicon_size.h" |
| 34 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 35 | 36 |
| 36 using content::Manifest; | 37 namespace { |
| 38 |
| 39 // Looks up the original, online URL of the site requested. The URL from the |
| 40 // WebContents may be an offline page or a distilled article which is not |
| 41 // appropriate for a home screen shortcut. |
| 42 GURL GetShortcutUrl(content::BrowserContext* browser_context, |
| 43 const GURL& actual_url) { |
| 44 GURL original_url = |
| 45 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); |
| 46 |
| 47 // If URL points to an offline content, get original URL. |
| 48 GURL online_url = |
| 49 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( |
| 50 browser_context, original_url); |
| 51 if (online_url.is_valid()) |
| 52 return online_url; |
| 53 |
| 54 return original_url; |
| 55 } |
| 56 |
| 57 InstallableParams ParamsToPerformInstallableCheck(int ideal_icon_size_in_dp, |
| 58 int minimum_icon_size_in_dp) { |
| 59 // TODO(hanxi): change check_installable to true for WebAPKs. |
| 60 InstallableParams params; |
| 61 params.ideal_icon_size_in_dp = ideal_icon_size_in_dp; |
| 62 params.minimum_icon_size_in_dp = minimum_icon_size_in_dp; |
| 63 params.check_installable = false; |
| 64 params.fetch_valid_icon = true; |
| 65 return params; |
| 66 } |
| 67 |
| 68 } // namespace |
| 37 | 69 |
| 38 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 70 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 39 content::WebContents* web_contents, | 71 content::WebContents* web_contents, |
| 40 int ideal_icon_size_in_dp, | 72 int ideal_icon_size_in_dp, |
| 41 int minimum_icon_size_in_dp, | 73 int minimum_icon_size_in_dp, |
| 42 int ideal_splash_image_size_in_dp, | 74 int ideal_splash_image_size_in_dp, |
| 43 int minimum_splash_image_size_in_dp, | 75 int minimum_splash_image_size_in_dp, |
| 44 Observer* observer) | 76 Observer* observer) |
| 45 : WebContentsObserver(web_contents), | 77 : WebContentsObserver(web_contents), |
| 46 weak_observer_(observer), | 78 weak_observer_(observer), |
| 47 is_waiting_for_web_application_info_(false), | 79 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), |
| 48 is_icon_saved_(false), | 80 web_contents->GetLastCommittedURL())), |
| 49 is_ready_(false), | 81 data_timeout_timer_(false, false), |
| 50 icon_timeout_timer_(false, false), | |
| 51 shortcut_info_(GetShortcutUrl(web_contents->GetURL())), | |
| 52 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), | 82 ideal_icon_size_in_dp_(ideal_icon_size_in_dp), |
| 53 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), | 83 minimum_icon_size_in_dp_(minimum_icon_size_in_dp), |
| 54 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), | 84 ideal_splash_image_size_in_dp_(ideal_splash_image_size_in_dp), |
| 55 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp) { | 85 minimum_splash_image_size_in_dp_(minimum_splash_image_size_in_dp), |
| 86 is_waiting_for_web_application_info_(true), |
| 87 is_icon_saved_(false), |
| 88 is_ready_(false) { |
| 56 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); | 89 DCHECK(minimum_icon_size_in_dp <= ideal_icon_size_in_dp); |
| 57 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); | 90 DCHECK(minimum_splash_image_size_in_dp <= ideal_splash_image_size_in_dp); |
| 58 | 91 |
| 59 // Send a message to the renderer to retrieve information about the page. | 92 // Send a message to the renderer to retrieve information about the page. |
| 60 is_waiting_for_web_application_info_ = true; | |
| 61 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 93 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 62 } | 94 } |
| 63 | 95 |
| 64 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( | 96 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| 65 const WebApplicationInfo& received_web_app_info) { | 97 const WebApplicationInfo& received_web_app_info) { |
| 66 is_waiting_for_web_application_info_ = false; | 98 is_waiting_for_web_application_info_ = false; |
| 67 if (!web_contents() || !weak_observer_) return; | 99 if (!web_contents() || !weak_observer_) |
| 100 return; |
| 68 | 101 |
| 69 // Sanitize received_web_app_info. | 102 // Sanitize received_web_app_info. |
| 70 WebApplicationInfo web_app_info = received_web_app_info; | 103 WebApplicationInfo web_app_info = received_web_app_info; |
| 71 web_app_info.title = | 104 web_app_info.title = |
| 72 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); | 105 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); |
| 73 web_app_info.description = | 106 web_app_info.description = |
| 74 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); | 107 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); |
| 75 | 108 |
| 76 // Simply set the user-editable title to be the page's title | 109 // Simply set the user-editable title to be the page's title |
| 77 shortcut_info_.user_title = web_app_info.title.empty() | 110 shortcut_info_.user_title = web_app_info.title.empty() |
| (...skipping 16 matching lines...) Expand all Loading... |
| 94 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | 127 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 95 content::RecordAction( | 128 content::RecordAction( |
| 96 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 129 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 97 break; | 130 break; |
| 98 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | 131 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 99 content::RecordAction( | 132 content::RecordAction( |
| 100 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 133 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 101 break; | 134 break; |
| 102 } | 135 } |
| 103 | 136 |
| 104 web_contents()->GetManifest( | 137 InstallableManager::CreateForWebContents(web_contents()); |
| 105 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); | 138 InstallableManager* manager = |
| 139 InstallableManager::FromWebContents(web_contents()); |
| 140 DCHECK(manager); |
| 141 |
| 142 // Kick off a timeout for downloading data. If we haven't finished within the |
| 143 // timeout, fall back to using a dynamically-generated launcher icon. |
| 144 data_timeout_timer_.Start( |
| 145 FROM_HERE, base::TimeDelta::FromMilliseconds(4000), |
| 146 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this, |
| 147 favicon_base::FaviconRawBitmapResult())); |
| 148 |
| 149 manager->GetData( |
| 150 ParamsToPerformInstallableCheck(ideal_icon_size_in_dp_, |
| 151 minimum_icon_size_in_dp_), |
| 152 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, |
| 153 this)); |
| 106 } | 154 } |
| 107 | 155 |
| 108 void AddToHomescreenDataFetcher::OnDidGetManifest( | 156 void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
| 109 const GURL& manifest_url, | 157 const InstallableData& data) { |
| 110 const content::Manifest& manifest) { | 158 if (!web_contents() || !weak_observer_) |
| 111 if (!web_contents() || !weak_observer_) return; | 159 return; |
| 112 | 160 |
| 113 if (!manifest.IsEmpty()) { | 161 if (!data.manifest.IsEmpty()) { |
| 114 content::RecordAction( | 162 content::RecordAction( |
| 115 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 163 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 116 shortcut_info_.UpdateFromManifest(manifest); | 164 shortcut_info_.UpdateFromManifest(data.manifest); |
| 117 shortcut_info_.manifest_url = manifest_url; | 165 shortcut_info_.manifest_url = data.manifest_url; |
| 118 } | |
| 119 | |
| 120 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | |
| 121 manifest.icons, ideal_icon_size_in_dp_, minimum_icon_size_in_dp_); | |
| 122 | |
| 123 // If fetching the Manifest icon fails, fallback to the best favicon | |
| 124 // for the page. | |
| 125 if (!ManifestIconDownloader::Download( | |
| 126 web_contents(), | |
| 127 icon_src, | |
| 128 ideal_icon_size_in_dp_, | |
| 129 minimum_icon_size_in_dp_, | |
| 130 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, | |
| 131 this, icon_src))) { | |
| 132 FetchFavicon(); | |
| 133 } | 166 } |
| 134 | 167 |
| 135 // Save the splash screen URL for the later download. | 168 // Save the splash screen URL for the later download. |
| 136 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( | 169 splash_screen_url_ = ManifestIconSelector::FindBestMatchingIcon( |
| 137 manifest.icons, ideal_splash_image_size_in_dp_, | 170 data.manifest.icons, ideal_splash_image_size_in_dp_, |
| 138 minimum_splash_image_size_in_dp_); | 171 minimum_splash_image_size_in_dp_); |
| 139 | 172 |
| 140 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 173 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 141 | 174 |
| 142 // Kick off a timeout for downloading the icon. If an icon isn't set within | 175 if (data.icon && !data.icon->drawsNothing()) { |
| 143 // the timeout, fall back to using a dynamically-generated launcher icon. | 176 // TODO(hanxi): implement WebAPK path if shortcut_info_.url has a secure |
| 144 icon_timeout_timer_.Start(FROM_HERE, | 177 // scheme and data.is_installable is true. |
| 145 base::TimeDelta::FromMilliseconds(3000), | 178 NotifyObserver(data.icon_url, *(data.icon)); |
| 146 base::Bind( | 179 return; |
| 147 &AddToHomescreenDataFetcher::OnFaviconFetched, | 180 } |
| 148 this, | 181 |
| 149 favicon_base::FaviconRawBitmapResult())); | 182 FetchFavicon(); |
| 150 } | 183 } |
| 151 | 184 |
| 152 bool AddToHomescreenDataFetcher::OnMessageReceived( | 185 bool AddToHomescreenDataFetcher::OnMessageReceived( |
| 153 const IPC::Message& message) { | 186 const IPC::Message& message) { |
| 154 if (!is_waiting_for_web_application_info_) return false; | 187 if (!is_waiting_for_web_application_info_) |
| 188 return false; |
| 155 | 189 |
| 156 bool handled = true; | 190 bool handled = true; |
| 157 | 191 |
| 158 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message) | 192 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message) |
| 159 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | 193 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, |
| 160 OnDidGetWebApplicationInfo) | 194 OnDidGetWebApplicationInfo) |
| 161 IPC_MESSAGE_UNHANDLED(handled = false) | 195 IPC_MESSAGE_UNHANDLED(handled = false) |
| 162 IPC_END_MESSAGE_MAP() | 196 IPC_END_MESSAGE_MAP() |
| 163 | 197 |
| 164 return handled; | 198 return handled; |
| 165 } | 199 } |
| 166 | 200 |
| 167 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 201 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 168 DCHECK(!weak_observer_); | 202 DCHECK(!weak_observer_); |
| 169 } | 203 } |
| 170 | 204 |
| 171 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( | 205 base::Closure AddToHomescreenDataFetcher::FetchSplashScreenImageCallback( |
| 172 const std::string& webapp_id) { | 206 const std::string& webapp_id) { |
| 173 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(), | 207 return base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents(), |
| 174 splash_screen_url_, ideal_splash_image_size_in_dp_, | 208 splash_screen_url_, ideal_splash_image_size_in_dp_, |
| 175 minimum_splash_image_size_in_dp_, webapp_id); | 209 minimum_splash_image_size_in_dp_, webapp_id); |
| 176 } | 210 } |
| 177 | 211 |
| 178 void AddToHomescreenDataFetcher::FetchFavicon() { | 212 void AddToHomescreenDataFetcher::FetchFavicon() { |
| 179 if (!web_contents() || !weak_observer_) return; | 213 if (!web_contents() || !weak_observer_) |
| 180 | 214 return; |
| 181 Profile* profile = | |
| 182 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 183 | 215 |
| 184 // Grab the best, largest icon we can find to represent this bookmark. | 216 // Grab the best, largest icon we can find to represent this bookmark. |
| 185 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its | 217 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its |
| 186 // rewrite is further along. | 218 // rewrite is further along. |
| 187 std::vector<int> icon_types; | 219 std::vector<int> icon_types{ |
| 188 icon_types.push_back(favicon_base::FAVICON); | 220 favicon_base::FAVICON, |
| 189 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | | 221 favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON}; |
| 190 favicon_base::TOUCH_ICON); | 222 |
| 191 favicon::FaviconService* favicon_service = | 223 favicon::FaviconService* favicon_service = |
| 192 FaviconServiceFactory::GetForProfile(profile, | 224 FaviconServiceFactory::GetForProfile( |
| 193 ServiceAccessType::EXPLICIT_ACCESS); | 225 Profile::FromBrowserContext(web_contents()->GetBrowserContext()), |
| 226 ServiceAccessType::EXPLICIT_ACCESS); |
| 194 | 227 |
| 195 // Using favicon if its size is not smaller than platform required size, | 228 // Using favicon if its size is not smaller than platform required size, |
| 196 // otherwise using the largest icon among all avaliable icons. | 229 // otherwise using the largest icon among all avaliable icons. |
| 197 int ideal_icon_size_in_px = | 230 int ideal_icon_size_in_px = |
| 198 ideal_icon_size_in_dp_ * | 231 ideal_icon_size_in_dp_ * |
| 199 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(); | 232 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(); |
| 200 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1; | 233 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px - 1; |
| 201 favicon_service->GetLargestRawFaviconForPageURL( | 234 favicon_service->GetLargestRawFaviconForPageURL( |
| 202 shortcut_info_.url, | 235 shortcut_info_.url, |
| 203 icon_types, | 236 icon_types, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 icon_bitmap, page_url, &is_generated); | 269 icon_bitmap, page_url, &is_generated); |
| 237 } | 270 } |
| 238 | 271 |
| 239 GURL icon_url = is_generated ? GURL() : bitmap_result.icon_url; | 272 GURL icon_url = is_generated ? GURL() : bitmap_result.icon_url; |
| 240 content::BrowserThread::PostTask( | 273 content::BrowserThread::PostTask( |
| 241 content::BrowserThread::UI, FROM_HERE, | 274 content::BrowserThread::UI, FROM_HERE, |
| 242 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, icon_url, | 275 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, icon_url, |
| 243 icon_bitmap)); | 276 icon_bitmap)); |
| 244 } | 277 } |
| 245 | 278 |
| 246 void AddToHomescreenDataFetcher::OnManifestIconFetched(const GURL& icon_url, | |
| 247 const SkBitmap& icon) { | |
| 248 if (icon.drawsNothing()) { | |
| 249 FetchFavicon(); | |
| 250 return; | |
| 251 } | |
| 252 NotifyObserver(icon_url, icon); | |
| 253 } | |
| 254 | |
| 255 void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url, | 279 void AddToHomescreenDataFetcher::NotifyObserver(const GURL& icon_url, |
| 256 const SkBitmap& bitmap) { | 280 const SkBitmap& bitmap) { |
| 257 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 258 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 282 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 259 return; | 283 return; |
| 260 | 284 |
| 261 is_icon_saved_ = true; | 285 is_icon_saved_ = true; |
| 262 shortcut_info_.icon_url = icon_url; | 286 shortcut_info_.icon_url = icon_url; |
| 263 shortcut_icon_ = bitmap; | 287 shortcut_icon_ = bitmap; |
| 264 is_ready_ = true; | 288 is_ready_ = true; |
| 265 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 289 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 266 } | 290 } |
| 267 | |
| 268 GURL AddToHomescreenDataFetcher::GetShortcutUrl(const GURL& actual_url) { | |
| 269 GURL original_url = | |
| 270 dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); | |
| 271 | |
| 272 // If URL points to an offline content, get original URL. | |
| 273 GURL online_url = | |
| 274 offline_pages::OfflinePageUtils::MaybeGetOnlineURLForOfflineURL( | |
| 275 web_contents()->GetBrowserContext(), original_url); | |
| 276 if (online_url.is_valid()) | |
| 277 return online_url; | |
| 278 | |
| 279 return original_url; | |
| 280 } | |
| OLD | NEW |