OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ | 4 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
5 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ | 5 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
10 #include "components/bookmarks/browser/bookmark_model_observer.h" | 10 #include "components/bookmarks/browser/bookmark_model_observer.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 public: | 33 public: |
34 BookmarkImageService(const base::FilePath& path, | 34 BookmarkImageService(const base::FilePath& path, |
35 EnhancedBookmarkModel* enhanced_bookmark_model, | 35 EnhancedBookmarkModel* enhanced_bookmark_model, |
36 scoped_refptr<base::SequencedWorkerPool> pool); | 36 scoped_refptr<base::SequencedWorkerPool> pool); |
37 BookmarkImageService(scoped_ptr<ImageStore> store, | 37 BookmarkImageService(scoped_ptr<ImageStore> store, |
38 EnhancedBookmarkModel* enhanced_bookmark_model, | 38 EnhancedBookmarkModel* enhanced_bookmark_model, |
39 scoped_refptr<base::SequencedWorkerPool> pool); | 39 scoped_refptr<base::SequencedWorkerPool> pool); |
40 | 40 |
41 ~BookmarkImageService() override; | 41 ~BookmarkImageService() override; |
42 | 42 |
43 typedef base::Callback<void(const ImageRecord&)> ImageCallback; | 43 typedef base::Callback<void(scoped_refptr<ImageRecord>)> ImageCallback; |
44 | 44 |
45 // KeyedService: | 45 // KeyedService: |
46 void Shutdown() override; | 46 void Shutdown() override; |
47 | 47 |
48 // Returns a salient image for a URL. This may trigger a network request for | 48 // Returns a salient image for a URL. This may trigger a network request for |
49 // the image if the image was not retrieved before and if a bookmark node has | 49 // the image if the image was not retrieved before and if a bookmark node has |
50 // a URL for this salient image available. The image (which may be empty) is | 50 // a URL for this salient image available. The image (which may be empty) is |
51 // sent via the callback. The callback may be called synchronously if it is | 51 // sent via the callback. The callback may be called synchronously if it is |
52 // possible. The callback is always triggered on the main thread. | 52 // possible. The callback is always triggered on the main thread. |
53 void SalientImageForUrl(const GURL& page_url, ImageCallback callback); | 53 void SalientImageForUrl(const GURL& page_url, ImageCallback callback); |
(...skipping 28 matching lines...) Expand all Loading... |
82 | 82 |
83 protected: | 83 protected: |
84 // Returns true if the image for the page_url is currently being fetched. | 84 // Returns true if the image for the page_url is currently being fetched. |
85 bool IsPageUrlInProgress(const GURL& page_url); | 85 bool IsPageUrlInProgress(const GURL& page_url); |
86 | 86 |
87 // Stores the new image to local storage. If update_bookmarks is true, relates | 87 // Stores the new image to local storage. If update_bookmarks is true, relates |
88 // the corresponding bookmark to image_url. | 88 // the corresponding bookmark to image_url. |
89 void ProcessNewImage(const GURL& page_url, | 89 void ProcessNewImage(const GURL& page_url, |
90 bool update_bookmarks, | 90 bool update_bookmarks, |
91 const GURL& image_url, | 91 const GURL& image_url, |
92 const gfx::Image& image); | 92 scoped_ptr<gfx::Image> image); |
93 | 93 |
94 // Resizes large images to proper size that fits device display. This method | 94 // Resizes large images to proper size that fits device display. This method |
95 // should _not_ run on the UI thread. | 95 // should _not_ run on the UI thread. |
96 virtual gfx::Image ResizeImage(gfx::Image image) = 0; | 96 virtual scoped_ptr<gfx::Image> ResizeImage(const gfx::Image& image) = 0; |
97 | 97 |
98 // Sets a new image for a bookmark. If the given page_url is bookmarked and | 98 // Sets a new image for a bookmark. If the given page_url is bookmarked and |
99 // the image is retrieved from the image_url, then the image is locally | 99 // the image is retrieved from the image_url, then the image is locally |
100 // stored. If update_bookmark is true the URL is also added to the bookmark. | 100 // stored. If update_bookmark is true the URL is also added to the bookmark. |
101 virtual void RetrieveSalientImage( | 101 virtual void RetrieveSalientImage( |
102 const GURL& page_url, | 102 const GURL& page_url, |
103 const GURL& image_url, | 103 const GURL& image_url, |
104 const std::string& referrer, | 104 const std::string& referrer, |
105 net::URLRequest::ReferrerPolicy referrer_policy, | 105 net::URLRequest::ReferrerPolicy referrer_policy, |
106 bool update_bookmark) = 0; | 106 bool update_bookmark) = 0; |
107 | 107 |
108 // Retrieves a salient image for a given page_url by downloading the image in | 108 // Retrieves a salient image for a given page_url by downloading the image in |
109 // one of the bookmark. | 109 // one of the bookmark. |
110 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url); | 110 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url); |
111 | 111 |
112 // PageUrls currently in the progress of being retrieved. | 112 // PageUrls currently in the progress of being retrieved. |
113 std::set<GURL> in_progress_page_urls_; | 113 std::set<GURL> in_progress_page_urls_; |
114 | 114 |
115 // Cached pointer to the bookmark model. | 115 // Cached pointer to the bookmark model. |
116 EnhancedBookmarkModel* enhanced_bookmark_model_; | 116 EnhancedBookmarkModel* enhanced_bookmark_model_; |
117 | 117 |
118 private: | 118 private: |
119 // If fetch_from_web is true, retrieves the salient image via a network | 119 // If fetch_from_web is true, retrieves the salient image via a network |
120 // request; else only gets the image from local storage. | 120 // request; else only gets the image from local storage. |
121 void SalientImageForUrl(const GURL& page_url, | 121 void SalientImageForUrl(const GURL& page_url, |
122 bool fetch_from_web, | 122 bool fetch_from_web, |
123 ImageCallback stack_callback); | 123 ImageCallback stack_callback); |
124 | 124 |
125 // Processes the requests that have been waiting on an image. | 125 // Processes the requests that have been waiting on an image. |
126 void ProcessRequests(const GURL& page_url, const ImageRecord& image); | 126 void ProcessRequests(const GURL& page_url, scoped_refptr<ImageRecord> image); |
127 | 127 |
128 // Once an image is retrieved this method calls ResizeImage() and updates the | 128 // Once an image is retrieved this method calls ResizeImage() and updates the |
129 // store with the smaller image, then returns the newly formed ImageRecord. | 129 // store with the smaller image, then returns the newly formed ImageRecord. |
130 // This is typically called on |pool_|, the background sequenced worker pool | 130 // This is typically called on |pool_|, the background sequenced worker pool |
131 // for this object. | 131 // for this object. |
132 ImageRecord ResizeAndStoreImage(const gfx::Image& image, | 132 scoped_refptr<ImageRecord> ResizeAndStoreImage( |
133 const GURL& image_url, | 133 scoped_refptr<ImageRecord> image_info, |
134 const GURL& page_url); | 134 const GURL& page_url); |
135 | 135 |
136 // Calls |StoreImage| in the background. This should only be called from the | 136 // Calls |StoreImage| in the background. This should only be called from the |
137 // main thread. | 137 // main thread. |
138 void PostTaskToStoreImage(const gfx::Image& image, | 138 void PostTaskToStoreImage(scoped_ptr<gfx::Image> image, |
139 const GURL& image_url, | 139 const GURL& image_url, |
140 const GURL& page_url); | 140 const GURL& page_url); |
141 | 141 |
142 // Called when |StoreImage| as been posted. This should only be called from | 142 // Called when |StoreImage| as been posted. This should only be called from |
143 // the main thread. | 143 // the main thread. |
144 void OnStoreImagePosted(const GURL& page_url, const ImageRecord& image); | 144 void OnStoreImagePosted(const GURL& page_url, |
| 145 scoped_refptr<ImageRecord> image); |
145 | 146 |
146 // Called when retrieving an image from the image store fails, to trigger | 147 // Called when retrieving an image from the image store fails, to trigger |
147 // retrieving the image from the url stored in the bookmark (if any). | 148 // retrieving the image from the url stored in the bookmark (if any). |
148 void FetchCallback(const GURL& page_url, | 149 void FetchCallback(const GURL& page_url, |
149 ImageCallback original_callback, | 150 ImageCallback original_callback, |
150 const ImageRecord& record); | 151 scoped_refptr<ImageRecord> record); |
151 | 152 |
152 // Remove the image stored for this bookmark (if it exists). Called when a | 153 // Remove the image stored for this bookmark (if it exists). Called when a |
153 // bookmark is deleted. | 154 // bookmark is deleted. |
154 void RemoveImageForUrl(const GURL& url); | 155 void RemoveImageForUrl(const GURL& url); |
155 | 156 |
156 // Moves an image from one url to another. | 157 // Moves an image from one url to another. |
157 void ChangeImageURL(const GURL& from, const GURL& to); | 158 void ChangeImageURL(const GURL& from, const GURL& to); |
158 | 159 |
159 // Removes all the entries in the image service. | 160 // Removes all the entries in the image service. |
160 void ClearAll(); | 161 void ClearAll(); |
(...skipping 19 matching lines...) Expand all Loading... |
180 | 181 |
181 // The worker pool to enqueue the requests onto. | 182 // The worker pool to enqueue the requests onto. |
182 scoped_refptr<base::SequencedWorkerPool> pool_; | 183 scoped_refptr<base::SequencedWorkerPool> pool_; |
183 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService); | 184 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService); |
184 }; | 185 }; |
185 | 186 |
186 } // namespace enhanced_bookmarks | 187 } // namespace enhanced_bookmarks |
187 | 188 |
188 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ | 189 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
189 | 190 |
OLD | NEW |