| 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/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/task_runner_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 15 #include "chrome/browser/android/shortcut_helper.h" | 16 #include "chrome/browser/android/shortcut_helper.h" |
| 16 #include "chrome/browser/android/webapk/webapk_web_manifest_checker.h" | 17 #include "chrome/browser/android/webapk/webapk_web_manifest_checker.h" |
| 17 #include "chrome/browser/favicon/favicon_service_factory.h" | 18 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 18 #include "chrome/browser/installable/installable_manager.h" | 19 #include "chrome/browser/installable/installable_manager.h" |
| 19 #include "chrome/browser/manifest/manifest_icon_selector.h" | 20 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/common/chrome_constants.h" | 22 #include "chrome/common/chrome_constants.h" |
| 22 #include "chrome/common/render_messages.h" | 23 #include "chrome/common/render_messages.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( | 68 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 68 content::WebContents* web_contents, | 69 content::WebContents* web_contents, |
| 69 int ideal_icon_size_in_px, | 70 int ideal_icon_size_in_px, |
| 70 int minimum_icon_size_in_px, | 71 int minimum_icon_size_in_px, |
| 71 int ideal_splash_image_size_in_px, | 72 int ideal_splash_image_size_in_px, |
| 72 int minimum_splash_image_size_in_px, | 73 int minimum_splash_image_size_in_px, |
| 73 int badge_size_in_px, | 74 int badge_size_in_px, |
| 74 bool check_webapk_compatibility, | 75 bool check_webapk_compatibility, |
| 75 Observer* observer) | 76 Observer* observer) |
| 76 : WebContentsObserver(web_contents), | 77 : WebContentsObserver(web_contents), |
| 78 background_task_runner_( |
| 79 content::BrowserThread::GetBlockingPool() |
| 80 ->GetTaskRunnerWithShutdownBehavior( |
| 81 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 77 weak_observer_(observer), | 82 weak_observer_(observer), |
| 78 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), | 83 shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(), |
| 79 web_contents->GetLastCommittedURL())), | 84 web_contents->GetLastCommittedURL())), |
| 80 data_timeout_timer_(false, false), | 85 data_timeout_timer_(false, false), |
| 81 ideal_icon_size_in_px_(ideal_icon_size_in_px), | 86 ideal_icon_size_in_px_(ideal_icon_size_in_px), |
| 82 minimum_icon_size_in_px_(minimum_icon_size_in_px), | 87 minimum_icon_size_in_px_(minimum_icon_size_in_px), |
| 83 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), | 88 ideal_splash_image_size_in_px_(ideal_splash_image_size_in_px), |
| 84 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), | 89 minimum_splash_image_size_in_px_(minimum_splash_image_size_in_px), |
| 85 badge_size_in_px_(badge_size_in_px), | 90 badge_size_in_px_(badge_size_in_px), |
| 86 check_webapk_compatibility_(check_webapk_compatibility), | 91 check_webapk_compatibility_(check_webapk_compatibility), |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon, | 284 shortcut_info_.url, icon_types, threshold_to_get_any_largest_icon, |
| 280 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), | 285 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 281 &favicon_task_tracker_); | 286 &favicon_task_tracker_); |
| 282 } | 287 } |
| 283 | 288 |
| 284 void AddToHomescreenDataFetcher::OnFaviconFetched( | 289 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 285 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 290 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 286 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 291 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 287 return; | 292 return; |
| 288 | 293 |
| 289 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( | 294 base::PostTaskAndReplyWithResult( |
| 290 FROM_HERE, base::Bind(&AddToHomescreenDataFetcher:: | 295 background_task_runner_.get(), FROM_HERE, |
| 291 CreateLauncherIconFromFaviconInBackground, | 296 base::Bind(&AddToHomescreenDataFetcher:: |
| 292 this, bitmap_result), | 297 CreateLauncherIconFromFaviconInBackground, |
| 293 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 298 base::Unretained(this), bitmap_result), |
| 299 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
| 300 base::RetainedRef(this))); |
| 294 } | 301 } |
| 295 | 302 |
| 296 void AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground( | 303 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground( |
| 297 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 304 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 298 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 305 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 299 | 306 |
| 300 SkBitmap raw_icon; | 307 SkBitmap raw_icon; |
| 301 if (bitmap_result.is_valid()) { | 308 if (bitmap_result.is_valid()) { |
| 302 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 309 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 303 bitmap_result.bitmap_data->size(), &raw_icon); | 310 bitmap_result.bitmap_data->size(), &raw_icon); |
| 304 } | 311 } |
| 305 | 312 |
| 306 shortcut_info_.best_primary_icon_url = bitmap_result.icon_url; | 313 shortcut_info_.best_primary_icon_url = bitmap_result.icon_url; |
| 307 CreateLauncherIconInBackground(raw_icon); | 314 return CreateLauncherIconInBackground(raw_icon); |
| 308 } | 315 } |
| 309 | 316 |
| 310 void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { | 317 void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) { |
| 311 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 318 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 312 content::BrowserThread::GetBlockingPool()->PostWorkerTaskWithShutdownBehavior( | 319 base::PostTaskAndReplyWithResult( |
| 313 FROM_HERE, | 320 background_task_runner_.get(), FROM_HERE, |
| 314 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, | 321 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground, |
| 315 this, raw_icon), | 322 base::Unretained(this), raw_icon), |
| 316 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 323 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
| 324 base::RetainedRef(this))); |
| 317 } | 325 } |
| 318 | 326 |
| 319 void AddToHomescreenDataFetcher::CreateLauncherIconInBackground( | 327 SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground( |
| 320 const SkBitmap& raw_icon) { | 328 const SkBitmap& raw_icon) { |
| 321 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 329 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 322 | 330 |
| 323 SkBitmap primary_icon; | 331 SkBitmap primary_icon; |
| 324 bool is_generated = false; | 332 bool is_generated = false; |
| 325 if (weak_observer_) { | 333 if (weak_observer_) { |
| 326 primary_icon = weak_observer_->FinalizeLauncherIconInBackground( | 334 primary_icon = weak_observer_->FinalizeLauncherIconInBackground( |
| 327 raw_icon, shortcut_info_.url, &is_generated); | 335 raw_icon, shortcut_info_.url, &is_generated); |
| 328 } | 336 } |
| 329 | 337 |
| 330 if (is_generated) | 338 if (is_generated) |
| 331 shortcut_info_.best_primary_icon_url = GURL(); | 339 shortcut_info_.best_primary_icon_url = GURL(); |
| 332 | 340 |
| 333 content::BrowserThread::PostTask( | 341 return primary_icon; |
| 334 content::BrowserThread::UI, FROM_HERE, | |
| 335 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, this, | |
| 336 primary_icon)); | |
| 337 } | 342 } |
| 338 | 343 |
| 339 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { | 344 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& primary_icon) { |
| 340 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 345 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 341 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 346 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 342 return; | 347 return; |
| 343 | 348 |
| 344 is_icon_saved_ = true; | 349 is_icon_saved_ = true; |
| 345 primary_icon_ = primary_icon; | 350 primary_icon_ = primary_icon; |
| 346 is_ready_ = true; | 351 is_ready_ = true; |
| 347 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); | 352 weak_observer_->OnDataAvailable(shortcut_info_, primary_icon_, badge_icon_); |
| 348 } | 353 } |
| OLD | NEW |