Skip to content

Commit

Permalink
Migrate AddToHomescreenDataFetcher to use TaskScheduler.
Browse files Browse the repository at this point in the history
BUG=667892

Review-Url: https://codereview.chromium.org/2949863003
Cr-Commit-Position: refs/heads/master@{#481359}
  • Loading branch information
dominickn authored and Commit Bot committed Jun 21, 2017
1 parent b80ffde commit 2311330
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
53 changes: 29 additions & 24 deletions chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
#include "base/bind.h"
#include "base/location.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string16.h"
#include "base/task_runner_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/android/offline_pages/offline_page_utils.h"
#include "chrome/browser/android/shortcut_helper.h"
#include "base/task_scheduler/post_task.h"
#include "chrome/browser/android/webapk/webapk_web_manifest_checker.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/installable/installable_manager.h"
Expand All @@ -28,7 +24,6 @@
#include "content/public/browser/manifest_icon_selector.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/manifest.h"
#include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationLockType.h"
#include "ui/gfx/codec/png_codec.h"
Expand Down Expand Up @@ -77,11 +72,7 @@ AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
int badge_size_in_px,
bool check_webapk_compatibility,
Observer* observer)
: WebContentsObserver(web_contents),
background_task_runner_(
content::BrowserThread::GetBlockingPool()
->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
: content::WebContentsObserver(web_contents),
weak_observer_(observer),
shortcut_info_(GetShortcutUrl(web_contents->GetBrowserContext(),
web_contents->GetLastCommittedURL())),
Expand Down Expand Up @@ -290,16 +281,23 @@ void AddToHomescreenDataFetcher::FetchFavicon() {

void AddToHomescreenDataFetcher::OnFaviconFetched(
const favicon_base::FaviconRawBitmapResult& bitmap_result) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

if (!web_contents() || !weak_observer_ || is_icon_saved_)
return;

base::PostTaskAndReplyWithResult(
background_task_runner_.get(), FROM_HERE,
base::Bind(&AddToHomescreenDataFetcher::
CreateLauncherIconFromFaviconInBackground,
base::Unretained(this), bitmap_result),
base::Bind(&AddToHomescreenDataFetcher::NotifyObserver,
base::RetainedRef(this)));
// The user is waiting for the icon to be processed before they can proceed
// with add to homescreen. But if we shut down, there's no point starting the
// image processing. Use USER_VISIBLE with MayBlock and SKIP_ON_SHUTDOWN.
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
base::BindOnce(&AddToHomescreenDataFetcher::
CreateLauncherIconFromFaviconInBackground,
base::Unretained(this), bitmap_result),
base::BindOnce(&AddToHomescreenDataFetcher::NotifyObserver,
base::RetainedRef(this)));
}

SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground(
Expand All @@ -318,12 +316,19 @@ SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconFromFaviconInBackground(

void AddToHomescreenDataFetcher::CreateLauncherIcon(const SkBitmap& raw_icon) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskAndReplyWithResult(
background_task_runner_.get(), FROM_HERE,
base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIconInBackground,
base::Unretained(this), raw_icon),
base::Bind(&AddToHomescreenDataFetcher::NotifyObserver,
base::RetainedRef(this)));

// The user is waiting for the icon to be processed before they can proceed
// with add to homescreen. But if we shut down, there's no point starting the
// image processing. Use USER_VISIBLE with MayBlock and SKIP_ON_SHUTDOWN.
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
base::BindOnce(
&AddToHomescreenDataFetcher::CreateLauncherIconInBackground,
base::Unretained(this), raw_icon),
base::BindOnce(&AddToHomescreenDataFetcher::NotifyObserver,
base::RetainedRef(this)));
}

SkBitmap AddToHomescreenDataFetcher::CreateLauncherIconInBackground(
Expand Down
15 changes: 1 addition & 14 deletions chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,17 @@

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/timer/timer.h"
#include "chrome/browser/android/shortcut_info.h"
#include "content/public/browser/web_contents_observer.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace base {
class TaskRunner;
}

namespace content {
class WebContents;
}

namespace favicon_base {
struct FaviconRawBitmapResult;
}

namespace IPC {
class Message;
}

class GURL;
struct InstallableData;
struct WebApplicationInfo;
Expand Down Expand Up @@ -123,8 +112,6 @@ class AddToHomescreenDataFetcher
// Notifies the observer that the shortcut data is all available.
void NotifyObserver(const SkBitmap& icon);

scoped_refptr<base::TaskRunner> background_task_runner_;

Observer* weak_observer_;

// The icons must only be set on the UI thread for thread safety.
Expand Down

0 comments on commit 2311330

Please sign in to comment.