Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Unified Diff: components/favicon/core/large_icon_service.h

Issue 1122103003: [Large Icon Service] Move icon resizing into worker thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Inject caller's task runner to Session; fix tests by mocking GetBackgroundTaskRunner(). Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/favicon/core/large_icon_service.h
diff --git a/components/favicon/core/large_icon_service.h b/components/favicon/core/large_icon_service.h
index a214e2037a8438b3fbc0a6f9444412523a775458..830e5f88438be2099d9b4618fa0973032f47073f 100644
--- a/components/favicon/core/large_icon_service.h
+++ b/components/favicon/core/large_icon_service.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,14 +7,17 @@
#include <vector>
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon_base/favicon_callback.h"
+#include "components/favicon_base/favicon_types.h"
#include "components/keyed_service/core/keyed_service.h"
class GURL;
-namespace favicon_base {
-struct FaviconRawBitmapResult;
+namespace base {
+class TaskRunner;
}
namespace favicon {
@@ -25,6 +28,61 @@ class FaviconService;
// the favicon service.
class LargeIconService : public KeyedService {
public:
+ // Tracks the states of a request session for LargeIconService, and manages
+ // the callback chain on different threads.
+ class Session : public base::RefCountedThreadSafe<Session> {
pkotwicz 2015/05/11 14:35:08 I wonder whether: - This class can be called Large
huangs 2015/05/11 15:20:39 - Renamed to LargeIconWorker. - Kept Session::OnIc
+ public:
+ Session(int min_source_size_in_pixel,
+ int desired_size_in_pixel,
+ favicon_base::LargeIconCallback callback,
+ scoped_refptr<base::TaskRunner> background_task_runner,
+ base::CancelableTaskTracker* tracker);
+
+ // Must run on the owner (UI) thread in production.
+ // Intermediate callback for GetLargeIconOrFallbackStyle(). Invokes
+ // ProcessIconOnBackgroundThread() so we do not perform complex image
+ // operations on the UI thread.
+ void OnIconLookupComplete(
+ const favicon_base::FaviconRawBitmapResult& bitmap_result);
+
+ private:
+ friend class base::RefCountedThreadSafe<Session>;
+ ~Session();
+
+ // Must run on a background thread in production.
+ // Tries to resize |bitmap_result| and pass the output to |callback|. If
+ // that does not work, computes the icon fallback style and uses it to
+ // invoke |callback|. This must be run on a background thread because image
+ // resizing and dominant color extraction can be expensive.
+ void ProcessIconOnBackgroundThread();
+
+ // Must run on a background thread in production.
+ // If |bitmap_result| is square and large enough (>= |min_source_in_pixel|),
+ // resizes it to |desired_size_in_pixel| (but if |desired_size_in_pixel| is
+ // 0 then don't resize). If successful, stores the resulting bitmap data
+ // into |resized_bitmap_result| and returns true.
+ static bool ResizeLargeIconOnBackgroundThreadIfValid(
+ int min_source_size_in_pixel,
+ int desired_size_in_pixel,
+ const favicon_base::FaviconRawBitmapResult& bitmap_result,
+ favicon_base::FaviconRawBitmapResult* resized_bitmap_result);
+
+ // Must run on the owner (UI) thread in production.
+ // Invoked when ProcessIconOnBackgroundThread() is done.
+ void OnIconProcessingComplete();
+
+ int min_source_size_in_pixel_;
+ int desired_size_in_pixel_;
+ favicon_base::LargeIconCallback callback_;
+ scoped_refptr<base::TaskRunner> background_task_runner_;
+ scoped_refptr<base::TaskRunner> caller_task_runner_;
+ base::CancelableTaskTracker* tracker_;
+ favicon_base::FaviconRawBitmapResult bitmap_result_;
+ scoped_ptr<favicon_base::LargeIconResult> result_;
+
+ DISALLOW_COPY_AND_ASSIGN(Session);
+ };
+
explicit LargeIconService(FaviconService* favicon_service);
~LargeIconService() override;
@@ -47,33 +105,18 @@ class LargeIconService : public KeyedService {
const favicon_base::LargeIconCallback& callback,
base::CancelableTaskTracker* tracker);
+ // Returns TaskRunner used to execute background task.
+ virtual scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner();
+
private:
// For testing.
friend class TestLargeIconService;
- // Resizes |bitmap_result| to |desired_size_in_pixel|x|desired_size_in_pixel|.
- // Stores the resized bitmap data in |resized_bitmap_result| and returns true
- // if successful.
- bool ResizeLargeIconIfValid(
- int min_source_size_in_pixel,
- int desired_size_in_pixel,
- const favicon_base::FaviconRawBitmapResult& bitmap_result,
- favicon_base::FaviconRawBitmapResult* resized_bitmap_result);
-
- // Intermediate callback for GetLargeIconOrFallbackStyle(). Tries to resize
- // |bitmap_result| and pass the output to |callback|. If that does not work,
- // computes the icon fallback style and uses it to invoke |callback|.
- void RunLargeIconCallback(
- const favicon_base::LargeIconCallback& callback,
- int min_source_size_in_pixel,
- int desired_size_in_pixel,
- const favicon_base::FaviconRawBitmapResult& bitmap_result);
-
FaviconService* favicon_service_;
- // A pre-populated list of the types of icon files to consider when looking
- // for large icons. This is an optimization over populating an icon type
- // vector on each request.
+ // A pre-populated list of icon types to consider when looking for large
+ // icons. This is an optimization over populating an icon type vector on each
+ // request.
std::vector<int> large_icon_types_;
DISALLOW_COPY_AND_ASSIGN(LargeIconService);

Powered by Google App Engine
This is Rietveld 408576698