| 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" |
| 11 #include "base/metrics/user_metrics.h" | 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/task_scheduler/post_task.h" |
| 13 #include "base/task_runner_util.h" | |
| 14 #include "base/threading/sequenced_worker_pool.h" | |
| 15 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | |
| 16 #include "chrome/browser/android/shortcut_helper.h" | |
| 17 #include "chrome/browser/android/webapk/webapk_web_manifest_checker.h" | 13 #include "chrome/browser/android/webapk/webapk_web_manifest_checker.h" |
| 18 #include "chrome/browser/favicon/favicon_service_factory.h" | 14 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 19 #include "chrome/browser/installable/installable_manager.h" | 15 #include "chrome/browser/installable/installable_manager.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 22 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
| 23 #include "chrome/common/web_application_info.h" | 19 #include "chrome/common/web_application_info.h" |
| 24 #include "components/dom_distiller/core/url_utils.h" | 20 #include "components/dom_distiller/core/url_utils.h" |
| 25 #include "components/favicon/core/favicon_service.h" | 21 #include "components/favicon/core/favicon_service.h" |
| 26 #include "components/favicon_base/favicon_types.h" | 22 #include "components/favicon_base/favicon_types.h" |
| 27 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/manifest_icon_selector.h" | 24 #include "content/public/browser/manifest_icon_selector.h" |
| 29 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 30 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/web_contents_observer.h" | |
| 32 #include "content/public/common/manifest.h" | 27 #include "content/public/common/manifest.h" |
| 33 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" | 28 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" |
| 34 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
| 35 #include "ui/gfx/favicon_size.h" | 30 #include "ui/gfx/favicon_size.h" |
| 36 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 37 | 32 |
| 38 namespace { | 33 namespace { |
| 39 | 34 |
| 40 // The default number of milliseconds to wait for the data download to complete. | 35 // The default number of milliseconds to wait for the data download to complete. |
| 41 const int kDataTimeoutInMilliseconds = 4000; | 36 const int kDataTimeoutInMilliseconds = 4000; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 70 | 65 |
| 71 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 66 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 72 content::WebContents* web_contents, | 67 content::WebContents* web_contents, |
| 73 int ideal_icon_size_in_px, | 68 int ideal_icon_size_in_px, |
| 74 int minimum_icon_size_in_px, | 69 int minimum_icon_size_in_px, |
| 75 int ideal_splash_image_size_in_px, | 70 int ideal_splash_image_size_in_px, |
| 76 int minimum_splash_image_size_in_px, | 71 int minimum_splash_image_size_in_px, |
| 77 int badge_size_in_px, | 72 int badge_size_in_px, |
| 78 bool check_webapk_compatibility, | 73 bool check_webapk_compatibility, |
| 79 Observer* observer) | 74 Observer* observer) |
| 80 : WebContentsObserver(web_contents), | 75 : content::WebContentsObserver(web_contents), |
| 81 background_task_runner_( | |
| 82 content::BrowserThread::GetBlockingPool() | |
| 83 ->GetTaskRunnerWithShutdownBehavior( | |
| 84 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | |
| 85 weak_observer_(observer), | 76 weak_observer_(observer), |
| 86 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), | 77 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), |
| 87 web_contents->GetLastCommittedURL())), | 78 web_contents->GetLastCommittedURL())), |
| 88 ideal_icon_size_in_px_(ideal_icon_size_in_px), | 79 ideal_icon_size_in_px_(ideal_icon_size_in_px), |
| 89 minimum_icon_size_in_px_(minimum_icon_size_in_px), | 80 minimum_icon_size_in_px_(minimum_icon_size_in_px), |
| 90 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), | 81 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), | 82 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), |
| 92 badge_size_in_px_(badge_size_in_px), | 83 badge_size_in_px_(badge_size_in_px), |
| 93 check_webapk_compatibility_(check_webapk_compatibility), | 84 check_webapk_compatibility_(check_webapk_compatibility), |
| 94 is_waiting_for_web_application_info_(true), | 85 is_waiting_for_web_application_info_(true), |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 // otherwise using the largest icon among all avaliable icons. | 274 // otherwise using the largest icon among all avaliable icons. |
| 284 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px_ - 1; | 275 int threshold_to_get_any_largest_icon = ideal_icon_size_in_px_ - 1; |
| 285 favicon_service->GetLargestRawFaviconForPageURL( | 276 favicon_service->GetLargestRawFaviconForPageURL( |
| 286 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon, | 277 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon, |
| 287 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 278 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 288 &favicon_task_tracker_); | 279 &favicon_task_tracker_); |
| 289 } | 280 } |
| 290 | 281 |
| 291 void AddToHomescreenDataFetcher::OnFaviconFetched( | 282 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 292 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 283 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 284 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 285 |
| 293 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 286 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 294 return; | 287 return; |
| 295 | 288 |
| 296 base::PostTaskAndReplyWithResult( | 289 // The user is waiting for the icon to be processed before they can proceed |
| 297 background_task_runner_.get(), FROM_HERE, | 290 // with add to homescreen. But if we shut down, there's no point starting the |
| 298 base::Bind(&AddToHomescreenDataFetcher:: | 291 // image processing. Use USER_VISIBLE with MayBlock and SKIP_ON_SHUTDOWN. |
| 299 CreateLauncherIconFromFaviconInBackground, | 292 base::PostTaskWithTraitsAndReplyWithResult( |
| 300 base::Unretained(this), bitmap_result), | 293 FROM_HERE, |
| 301 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, | 294 {base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 302 base::RetainedRef(this))); | 295 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
| 296 base::BindOnce(&AddToHomescreenDataFetcher:: |
| 297 CreateLauncherIconFromFaviconInBackground, |
| 298 base::Unretained(this), bitmap_result), |
| 299 base::BindOnce(&AddToHomescreenDataFetcher::NotifyObserver, |
| 300 base::RetainedRef(this))); |
| 303 } | 301 } |
| 304 | 302 |
| 305 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground( | 303 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground( |
| 306 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 304 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 307 base::ThreadRestrictions::AssertIOAllowed(); | 305 base::ThreadRestrictions::AssertIOAllowed(); |
| 308 | 306 |
| 309 SkBitmap raw_icon; | 307 SkBitmap raw_icon; |
| 310 if (bitmap_result.is_valid()) { | 308 if (bitmap_result.is_valid()) { |
| 311 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 309 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 312 bitmap_result.bitmap_data->size(), &raw_icon); | 310 bitmap_result.bitmap_data->size(), &raw_icon); |
| 313 } | 311 } |
| 314 | 312 |
| 315 shortcut_info_.best_primary_icon_url = bitmap_result.icon_url; | 313 shortcut_info_.best_primary_icon_url = bitmap_result.icon_url; |
| 316 return CreateLauncherIconInBackground(raw_icon); | 314 return CreateLauncherIconInBackground(raw_icon); |
| 317 } | 315 } |
| 318 | 316 |
| 319 void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { | 317 void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { |
| 320 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 318 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 321 base::PostTaskAndReplyWithResult( | 319 |
| 322 background_task_runner_.get(), FROM_HERE, | 320 // The user is waiting for the icon to be processed before they can proceed |
| 323 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, | 321 // with add to homescreen. But if we shut down, there's no point starting the |
| 324 base::Unretained(this), raw_icon), | 322 // image processing. Use USER_VISIBLE with MayBlock and SKIP_ON_SHUTDOWN. |
| 325 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, | 323 base::PostTaskWithTraitsAndReplyWithResult( |
| 326 base::RetainedRef(this))); | 324 FROM_HERE, |
| 325 {base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 326 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
| 327 base::BindOnce( |
| 328 &AddToHomescreenDataFetcher::CreateLauncherIconInBackground, |
| 329 base::Unretained(this), raw_icon), |
| 330 base::BindOnce(&AddToHomescreenDataFetcher::NotifyObserver, |
| 331 base::RetainedRef(this))); |
| 327 } | 332 } |
| 328 | 333 |
| 329 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground( | 334 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground( |
| 330 const SkBitmap& raw_icon) { | 335 const SkBitmap& raw_icon) { |
| 331 base::ThreadRestrictions::AssertIOAllowed(); | 336 base::ThreadRestrictions::AssertIOAllowed(); |
| 332 | 337 |
| 333 SkBitmap primary_icon; | 338 SkBitmap primary_icon; |
| 334 bool is_generated = false; | 339 bool is_generated = false; |
| 335 if (weak_observer_) { | 340 if (weak_observer_) { |
| 336 primary_icon = weak_observer_->FinalizeLauncherIconInBackground( | 341 primary_icon = weak_observer_->FinalizeLauncherIconInBackground( |
| 337 raw_icon, shortcut_info_.url, &is_generated); | 342 raw_icon, shortcut_info_.url, &is_generated); |
| 338 } | 343 } |
| 339 | 344 |
| 340 if (is_generated) | 345 if (is_generated) |
| 341 shortcut_info_.best_primary_icon_url = GURL(); | 346 shortcut_info_.best_primary_icon_url = GURL(); |
| 342 | 347 |
| 343 return primary_icon; | 348 return primary_icon; |
| 344 } | 349 } |
| 345 | 350 |
| 346 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { | 351 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { |
| 347 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 352 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 348 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 353 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 349 return; | 354 return; |
| 350 | 355 |
| 351 is_icon_saved_ = true; | 356 is_icon_saved_ = true; |
| 352 primary_icon_ = primary_icon; | 357 primary_icon_ = primary_icon; |
| 353 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); | 358 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); |
| 354 } | 359 } |
| OLD | NEW |