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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 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 #ifndef COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_ 5 #ifndef COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_
6 #define COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_ 6 #define COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
10 #include "base/task/cancelable_task_tracker.h" 12 #include "base/task/cancelable_task_tracker.h"
11 #include "components/favicon_base/favicon_callback.h" 13 #include "components/favicon_base/favicon_callback.h"
14 #include "components/favicon_base/favicon_types.h"
12 #include "components/keyed_service/core/keyed_service.h" 15 #include "components/keyed_service/core/keyed_service.h"
13 16
14 class GURL; 17 class GURL;
15 18
16 namespace favicon_base { 19 namespace base {
17 struct FaviconRawBitmapResult; 20 class TaskRunner;
18 } 21 }
19 22
20 namespace favicon { 23 namespace favicon {
21 24
22 class FaviconService; 25 class FaviconService;
23 26
24 // The large icon service provides methods to access large icons. It relies on 27 // The large icon service provides methods to access large icons. It relies on
25 // the favicon service. 28 // the favicon service.
26 class LargeIconService : public KeyedService { 29 class LargeIconService : public KeyedService {
27 public: 30 public:
31 // Tracks the states of a request session for LargeIconService, and manages
32 // the callback chain on different threads.
33 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
34 public:
35 Session(int min_source_size_in_pixel,
36 int desired_size_in_pixel,
37 favicon_base::LargeIconCallback callback,
38 scoped_refptr<base::TaskRunner> background_task_runner,
39 base::CancelableTaskTracker* tracker);
40
41 // Must run on the owner (UI) thread in production.
42 // Intermediate callback for GetLargeIconOrFallbackStyle(). Invokes
43 // ProcessIconOnBackgroundThread() so we do not perform complex image
44 // operations on the UI thread.
45 void OnIconLookupComplete(
46 const favicon_base::FaviconRawBitmapResult& bitmap_result);
47
48 private:
49 friend class base::RefCountedThreadSafe<Session>;
50 ~Session();
51
52 // Must run on a background thread in production.
53 // Tries to resize |bitmap_result| and pass the output to |callback|. If
54 // that does not work, computes the icon fallback style and uses it to
55 // invoke |callback|. This must be run on a background thread because image
56 // resizing and dominant color extraction can be expensive.
57 void ProcessIconOnBackgroundThread();
58
59 // Must run on a background thread in production.
60 // If |bitmap_result| is square and large enough (>= |min_source_in_pixel|),
61 // resizes it to |desired_size_in_pixel| (but if |desired_size_in_pixel| is
62 // 0 then don't resize). If successful, stores the resulting bitmap data
63 // into |resized_bitmap_result| and returns true.
64 static bool ResizeLargeIconOnBackgroundThreadIfValid(
65 int min_source_size_in_pixel,
66 int desired_size_in_pixel,
67 const favicon_base::FaviconRawBitmapResult& bitmap_result,
68 favicon_base::FaviconRawBitmapResult* resized_bitmap_result);
69
70 // Must run on the owner (UI) thread in production.
71 // Invoked when ProcessIconOnBackgroundThread() is done.
72 void OnIconProcessingComplete();
73
74 int min_source_size_in_pixel_;
75 int desired_size_in_pixel_;
76 favicon_base::LargeIconCallback callback_;
77 scoped_refptr<base::TaskRunner> background_task_runner_;
78 scoped_refptr<base::TaskRunner> caller_task_runner_;
79 base::CancelableTaskTracker* tracker_;
80 favicon_base::FaviconRawBitmapResult bitmap_result_;
81 scoped_ptr<favicon_base::LargeIconResult> result_;
82
83 DISALLOW_COPY_AND_ASSIGN(Session);
84 };
85
28 explicit LargeIconService(FaviconService* favicon_service); 86 explicit LargeIconService(FaviconService* favicon_service);
29 87
30 ~LargeIconService() override; 88 ~LargeIconService() override;
31 89
32 // Requests the best large icon for the page at |page_url|. 90 // Requests the best large icon for the page at |page_url|.
33 // Case 1. An icon exists whose size is >= |min_source_size_in_pixel|: 91 // Case 1. An icon exists whose size is >= |min_source_size_in_pixel|:
34 // - If |desired_size_in_pixel| == 0: returns icon as is. 92 // - If |desired_size_in_pixel| == 0: returns icon as is.
35 // - Else: returns the icon resized to |desired_size_in_pixel|. 93 // - Else: returns the icon resized to |desired_size_in_pixel|.
36 // Case 2. An icon exists whose size is < |min_source_size_in_pixel|: 94 // Case 2. An icon exists whose size is < |min_source_size_in_pixel|:
37 // - Extracts dominant color of smaller image, returns a fallback icon style 95 // - Extracts dominant color of smaller image, returns a fallback icon style
38 // that has a matching background. 96 // that has a matching background.
39 // Case 3. No icon exists. 97 // Case 3. No icon exists.
40 // - Returns the default fallback icon style. 98 // - Returns the default fallback icon style.
41 // For cases 2 and 3, this function returns the style of the fallback icon 99 // For cases 2 and 3, this function returns the style of the fallback icon
42 // instead of rendering an icon so clients can render the icon themselves. 100 // instead of rendering an icon so clients can render the icon themselves.
43 base::CancelableTaskTracker::TaskId GetLargeIconOrFallbackStyle( 101 base::CancelableTaskTracker::TaskId GetLargeIconOrFallbackStyle(
44 const GURL& page_url, 102 const GURL& page_url,
45 int min_source_size_in_pixel, 103 int min_source_size_in_pixel,
46 int desired_size_in_pixel, 104 int desired_size_in_pixel,
47 const favicon_base::LargeIconCallback& callback, 105 const favicon_base::LargeIconCallback& callback,
48 base::CancelableTaskTracker* tracker); 106 base::CancelableTaskTracker* tracker);
49 107
108 // Returns TaskRunner used to execute background task.
109 virtual scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner();
110
50 private: 111 private:
51 // For testing. 112 // For testing.
52 friend class TestLargeIconService; 113 friend class TestLargeIconService;
53 114
54 // Resizes |bitmap_result| to |desired_size_in_pixel|x|desired_size_in_pixel|.
55 // Stores the resized bitmap data in |resized_bitmap_result| and returns true
56 // if successful.
57 bool ResizeLargeIconIfValid(
58 int min_source_size_in_pixel,
59 int desired_size_in_pixel,
60 const favicon_base::FaviconRawBitmapResult& bitmap_result,
61 favicon_base::FaviconRawBitmapResult* resized_bitmap_result);
62
63 // Intermediate callback for GetLargeIconOrFallbackStyle(). Tries to resize
64 // |bitmap_result| and pass the output to |callback|. If that does not work,
65 // computes the icon fallback style and uses it to invoke |callback|.
66 void RunLargeIconCallback(
67 const favicon_base::LargeIconCallback& callback,
68 int min_source_size_in_pixel,
69 int desired_size_in_pixel,
70 const favicon_base::FaviconRawBitmapResult& bitmap_result);
71
72 FaviconService* favicon_service_; 115 FaviconService* favicon_service_;
73 116
74 // A pre-populated list of the types of icon files to consider when looking 117 // A pre-populated list of icon types to consider when looking for large
75 // for large icons. This is an optimization over populating an icon type 118 // icons. This is an optimization over populating an icon type vector on each
76 // vector on each request. 119 // request.
77 std::vector<int> large_icon_types_; 120 std::vector<int> large_icon_types_;
78 121
79 DISALLOW_COPY_AND_ASSIGN(LargeIconService); 122 DISALLOW_COPY_AND_ASSIGN(LargeIconService);
80 }; 123 };
81 124
82 } // namespace favicon 125 } // namespace favicon
83 126
84 #endif // COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_ 127 #endif // COMPONENTS_FAVICON_CORE_LARGE_ICON_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698