| 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> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 const int kDataTimeoutInMilliseconds = 4000; | 41 const int kDataTimeoutInMilliseconds = 4000; |
| 42 | 42 |
| 43 // Looks up the original, online URL of the site requested. The URL from the | 43 // Looks up the original, online URL of the site requested. The URL from the |
| 44 // WebContents may be a distilled article which is not appropriate for a home | 44 // WebContents may be a distilled article which is not appropriate for a home |
| 45 // screen shortcut. | 45 // screen shortcut. |
| 46 GURL GetShortcutUrl(content::BrowserContext* browser_context, | 46 GURL GetShortcutUrl(content::BrowserContext* browser_context, |
| 47 const GURL& actual_url) { | 47 const GURL& actual_url) { |
| 48 return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); | 48 return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(actual_url); |
| 49 } | 49 } |
| 50 | 50 |
| 51 InstallableParams ParamsToPerformManifestAndIconFetch( |
| 52 int ideal_icon_size_in_px, |
| 53 int minimum_icon_size_in_px) { |
| 54 InstallableParams params; |
| 55 params.ideal_primary_icon_size_in_px = ideal_icon_size_in_px; |
| 56 params.minimum_primary_icon_size_in_px = minimum_icon_size_in_px; |
| 57 params.fetch_valid_primary_icon = true; |
| 58 return params; |
| 59 } |
| 60 |
| 51 InstallableParams ParamsToPerformInstallableCheck( | 61 InstallableParams ParamsToPerformInstallableCheck( |
| 52 int ideal_icon_size_in_px, | 62 int ideal_icon_size_in_px, |
| 53 int minimum_icon_size_in_px, | 63 int minimum_icon_size_in_px, |
| 54 int badge_size_in_px, | 64 int badge_size_in_px, |
| 55 bool check_webapk_compatibility) { | 65 bool check_webapk_compatibility) { |
| 56 InstallableParams params; | 66 InstallableParams params = ParamsToPerformManifestAndIconFetch( |
| 57 params.ideal_primary_icon_size_in_px = ideal_icon_size_in_px; | 67 ideal_icon_size_in_px, minimum_icon_size_in_px); |
| 58 params.minimum_primary_icon_size_in_px = minimum_icon_size_in_px; | |
| 59 params.check_installable = check_webapk_compatibility; | |
| 60 params.fetch_valid_primary_icon = true; | |
| 61 if (check_webapk_compatibility) { | 68 if (check_webapk_compatibility) { |
| 69 params.check_installable = check_webapk_compatibility; |
| 62 params.ideal_badge_icon_size_in_px = badge_size_in_px; | 70 params.ideal_badge_icon_size_in_px = badge_size_in_px; |
| 63 params.minimum_badge_icon_size_in_px = badge_size_in_px; | 71 params.minimum_badge_icon_size_in_px = badge_size_in_px; |
| 64 params.fetch_valid_badge_icon = true; | 72 params.fetch_valid_badge_icon = true; |
| 65 } | 73 } |
| 66 return params; | 74 return params; |
| 67 } | 75 } |
| 68 | 76 |
| 69 } // namespace | 77 } // namespace |
| 70 | 78 |
| 71 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 79 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 72 content::WebContents* web_contents, | 80 content::WebContents* web_contents, |
| 73 int ideal_icon_size_in_px, | 81 int ideal_icon_size_in_px, |
| 74 int minimum_icon_size_in_px, | 82 int minimum_icon_size_in_px, |
| 75 int ideal_splash_image_size_in_px, | 83 int ideal_splash_image_size_in_px, |
| 76 int minimum_splash_image_size_in_px, | 84 int minimum_splash_image_size_in_px, |
| 77 int badge_size_in_px, | 85 int badge_size_in_px, |
| 78 bool check_webapk_compatibility, | 86 bool check_webapk_compatibility, |
| 79 Observer* observer) | 87 Observer* observer) |
| 80 : WebContentsObserver(web_contents), | 88 : WebContentsObserver(web_contents), |
| 81 background_task_runner_( | 89 background_task_runner_( |
| 82 content::BrowserThread::GetBlockingPool() | 90 content::BrowserThread::GetBlockingPool() |
| 83 ->GetTaskRunnerWithShutdownBehavior( | 91 ->GetTaskRunnerWithShutdownBehavior( |
| 84 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 92 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 93 installable_manager_(InstallableManager::FromWebContents(web_contents)), |
| 85 weak_observer_(observer), | 94 weak_observer_(observer), |
| 86 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), | 95 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), |
| 87 web_contents->GetLastCommittedURL())), | 96 web_contents->GetLastCommittedURL())), |
| 88 ideal_icon_size_in_px_(ideal_icon_size_in_px), | 97 ideal_icon_size_in_px_(ideal_icon_size_in_px), |
| 89 minimum_icon_size_in_px_(minimum_icon_size_in_px), | 98 minimum_icon_size_in_px_(minimum_icon_size_in_px), |
| 90 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), | 99 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), |
| 91 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), | 100 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), |
| 92 badge_size_in_px_(badge_size_in_px), | 101 badge_size_in_px_(badge_size_in_px), |
| 93 check_webapk_compatibility_(check_webapk_compatibility), | 102 check_webapk_compatibility_(check_webapk_compatibility), |
| 94 is_waiting_for_web_application_info_(true), | 103 is_waiting_for_web_application_info_(true), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | 146 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 138 base::RecordAction( | 147 base::RecordAction( |
| 139 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 148 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 140 break; | 149 break; |
| 141 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | 150 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 142 base::RecordAction( | 151 base::RecordAction( |
| 143 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 152 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 144 break; | 153 break; |
| 145 } | 154 } |
| 146 | 155 |
| 147 InstallableManager::CreateForWebContents(web_contents()); | |
| 148 InstallableManager* manager = | |
| 149 InstallableManager::FromWebContents(web_contents()); | |
| 150 DCHECK(manager); | |
| 151 | |
| 152 // Kick off a timeout for downloading data. If we haven't finished within the | 156 // Kick off a timeout for downloading data. If we haven't finished within the |
| 153 // timeout, fall back to using a dynamically-generated launcher icon. | 157 // timeout, fall back to using a dynamically-generated launcher icon. |
| 154 data_timeout_timer_.Start( | 158 data_timeout_timer_.Start( |
| 155 FROM_HERE, base::TimeDelta::FromMilliseconds(kDataTimeoutInMilliseconds), | 159 FROM_HERE, base::TimeDelta::FromMilliseconds(kDataTimeoutInMilliseconds), |
| 156 base::Bind(&AddToHomescreenDataFetcher::OnDataTimedout, this)); | 160 base::Bind(&AddToHomescreenDataFetcher::OnDataTimedout, this)); |
| 157 | 161 |
| 158 manager->GetData( | 162 installable_manager_->GetData( |
| 159 ParamsToPerformInstallableCheck(ideal_icon_size_in_px_, | 163 ParamsToPerformManifestAndIconFetch(ideal_icon_size_in_px_, |
| 160 minimum_icon_size_in_px_, | 164 minimum_icon_size_in_px_), |
| 161 badge_size_in_px_, | 165 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifestAndIcon, this)); |
| 162 check_webapk_compatibility_), | |
| 163 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, | |
| 164 this)); | |
| 165 } | 166 } |
| 166 | 167 |
| 167 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { | 168 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 168 DCHECK(!weak_observer_); | 169 DCHECK(!weak_observer_); |
| 169 } | 170 } |
| 170 | 171 |
| 171 bool AddToHomescreenDataFetcher::OnMessageReceived( | 172 bool AddToHomescreenDataFetcher::OnMessageReceived( |
| 172 const IPC::Message& message, | 173 const IPC::Message& message, |
| 173 content::RenderFrameHost* sender) { | 174 content::RenderFrameHost* sender) { |
| 174 if (!is_waiting_for_web_application_info_) | 175 if (!is_waiting_for_web_application_info_) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 193 is_installable_check_complete_ = true; | 194 is_installable_check_complete_ = true; |
| 194 if (check_webapk_compatibility_) | 195 if (check_webapk_compatibility_) |
| 195 weak_observer_->OnDidDetermineWebApkCompatibility(false); | 196 weak_observer_->OnDidDetermineWebApkCompatibility(false); |
| 196 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 197 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 197 } | 198 } |
| 198 | 199 |
| 199 badge_icon_.reset(); | 200 badge_icon_.reset(); |
| 200 CreateLauncherIcon(SkBitmap()); | 201 CreateLauncherIcon(SkBitmap()); |
| 201 } | 202 } |
| 202 | 203 |
| 204 void AddToHomescreenDataFetcher::OnDidGetManifestAndIcon( |
| 205 const InstallableData& data) { |
| 206 if (!web_contents() || !weak_observer_ || is_installable_check_complete_) |
| 207 return; |
| 208 |
| 209 if (!data.manifest.IsEmpty()) { |
| 210 base::RecordAction(base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 211 shortcut_info_.UpdateFromManifest(data.manifest); |
| 212 shortcut_info_.manifest_url = data.manifest_url; |
| 213 } |
| 214 |
| 215 installable_manager_->GetData( |
| 216 ParamsToPerformInstallableCheck( |
| 217 ideal_icon_size_in_px_, minimum_icon_size_in_px_, badge_size_in_px_, |
| 218 check_webapk_compatibility_), |
| 219 base::Bind(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck, |
| 220 this)); |
| 221 } |
| 222 |
| 203 void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( | 223 void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck( |
| 204 const InstallableData& data) { | 224 const InstallableData& data) { |
| 205 data_timeout_timer_.Stop(); | 225 data_timeout_timer_.Stop(); |
| 206 badge_icon_.reset(); | 226 badge_icon_.reset(); |
| 207 | 227 |
| 208 if (!web_contents() || !weak_observer_ || is_installable_check_complete_) | 228 if (!web_contents() || !weak_observer_ || is_installable_check_complete_) |
| 209 return; | 229 return; |
| 210 | 230 |
| 211 is_installable_check_complete_ = true; | 231 is_installable_check_complete_ = true; |
| 212 | 232 |
| 213 bool webapk_compatible = false; | 233 bool webapk_compatible = false; |
| 214 if (check_webapk_compatibility_) { | 234 if (check_webapk_compatibility_) { |
| 215 webapk_compatible = (data.error_code == NO_ERROR_DETECTED && | 235 webapk_compatible = (data.error_code == NO_ERROR_DETECTED && |
| 216 AreWebManifestUrlsWebApkCompatible(data.manifest)); | 236 AreWebManifestUrlsWebApkCompatible(data.manifest)); |
| 217 weak_observer_->OnDidDetermineWebApkCompatibility(webapk_compatible); | 237 weak_observer_->OnDidDetermineWebApkCompatibility(webapk_compatible); |
| 238 } |
| 218 | 239 |
| 219 if (webapk_compatible) { | 240 if (webapk_compatible) { |
| 220 // WebAPKs are wholly defined by the Web Manifest. Ignore the <meta> tag | 241 shortcut_info_.UpdateSource(ShortcutInfo::SOURCE_ADD_TO_HOMESCREEN_PWA); |
| 221 // data received in OnDidGetWebApplicationInfo(). | 242 |
| 222 shortcut_info_ = ShortcutInfo(GURL()); | 243 if (data.badge_icon && !data.badge_icon->drawsNothing()) { |
| 244 shortcut_info_.best_badge_icon_url = data.badge_icon_url; |
| 245 badge_icon_ = *data.badge_icon; |
| 223 } | 246 } |
| 224 } | 247 } |
| 225 | 248 |
| 226 if (!data.manifest.IsEmpty()) { | |
| 227 base::RecordAction(base::UserMetricsAction("webapps.AddShortcut.Manifest")); | |
| 228 shortcut_info_.UpdateFromManifest(data.manifest); | |
| 229 shortcut_info_.manifest_url = data.manifest_url; | |
| 230 | |
| 231 if (webapk_compatible) { | |
| 232 shortcut_info_.UpdateSource(ShortcutInfo::SOURCE_ADD_TO_HOMESCREEN_PWA); | |
| 233 | |
| 234 if (data.badge_icon && !data.badge_icon->drawsNothing()) { | |
| 235 shortcut_info_.best_badge_icon_url = data.badge_icon_url; | |
| 236 badge_icon_ = *data.badge_icon; | |
| 237 } | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 // Save the splash screen URL for the later download. | 249 // Save the splash screen URL for the later download. |
| 242 shortcut_info_.splash_image_url = | 250 shortcut_info_.splash_image_url = |
| 243 content::ManifestIconSelector::FindBestMatchingIcon( | 251 content::ManifestIconSelector::FindBestMatchingIcon( |
| 244 data.manifest.icons, ideal_splash_image_size_in_px_, | 252 data.manifest.icons, ideal_splash_image_size_in_px_, |
| 245 minimum_splash_image_size_in_px_, | 253 minimum_splash_image_size_in_px_, |
| 246 content::Manifest::Icon::IconPurpose::ANY); | 254 content::Manifest::Icon::IconPurpose::ANY); |
| 247 shortcut_info_.ideal_splash_image_size_in_px = ideal_splash_image_size_in_px_; | 255 shortcut_info_.ideal_splash_image_size_in_px = ideal_splash_image_size_in_px_; |
| 248 shortcut_info_.minimum_splash_image_size_in_px = | 256 shortcut_info_.minimum_splash_image_size_in_px = |
| 249 minimum_splash_image_size_in_px_; | 257 minimum_splash_image_size_in_px_; |
| 250 | 258 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 353 |
| 346 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { | 354 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { |
| 347 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 355 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 348 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 356 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 349 return; | 357 return; |
| 350 | 358 |
| 351 is_icon_saved_ = true; | 359 is_icon_saved_ = true; |
| 352 primary_icon_ = primary_icon; | 360 primary_icon_ = primary_icon; |
| 353 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); | 361 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); |
| 354 } | 362 } |
| OLD | NEW |