| 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" |
| 11 #include "components/enhanced_bookmarks/image_record.h" |
| 11 #include "components/enhanced_bookmarks/image_store.h" | 12 #include "components/enhanced_bookmarks/image_store.h" |
| 12 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
| 13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class SingleThreadTaskRunner; | 18 class SingleThreadTaskRunner; |
| 18 } | 19 } |
| 19 class BookmarkNode; | 20 class BookmarkNode; |
| 20 | 21 |
| 21 namespace enhanced_bookmarks { | 22 namespace enhanced_bookmarks { |
| 22 | 23 |
| 23 class EnhancedBookmarkModel; | 24 class EnhancedBookmarkModel; |
| 24 | 25 |
| 25 // The BookmarkImageService stores salient images for bookmarks. | 26 // The BookmarkImageService stores salient images for bookmarks. |
| 26 class BookmarkImageService : public KeyedService, | 27 class BookmarkImageService : public KeyedService, |
| 27 public bookmarks::BookmarkModelObserver, | 28 public bookmarks::BookmarkModelObserver, |
| 28 public base::NonThreadSafe { | 29 public base::NonThreadSafe { |
| 29 public: | 30 public: |
| 30 BookmarkImageService(const base::FilePath& path, | 31 BookmarkImageService(const base::FilePath& path, |
| 31 EnhancedBookmarkModel* enhanced_bookmark_model, | 32 EnhancedBookmarkModel* enhanced_bookmark_model, |
| 32 scoped_refptr<base::SequencedWorkerPool> pool); | 33 scoped_refptr<base::SequencedWorkerPool> pool); |
| 33 BookmarkImageService(scoped_ptr<ImageStore> store, | 34 BookmarkImageService(scoped_ptr<ImageStore> store, |
| 34 EnhancedBookmarkModel* enhanced_bookmark_model, | 35 EnhancedBookmarkModel* enhanced_bookmark_model, |
| 35 scoped_refptr<base::SequencedWorkerPool> pool); | 36 scoped_refptr<base::SequencedWorkerPool> pool); |
| 36 | 37 |
| 37 ~BookmarkImageService() override; | 38 ~BookmarkImageService() override; |
| 38 | 39 |
| 39 typedef base::Callback<void(const gfx::Image&, const GURL& url)> Callback; | 40 typedef base::Callback<void(const ImageRecord&)> ImageCallback; |
| 40 | 41 |
| 41 // KeyedService: | 42 // KeyedService: |
| 42 void Shutdown() override; | 43 void Shutdown() override; |
| 43 | 44 |
| 44 // Returns a salient image for a URL. This may trigger a network request for | 45 // Returns a salient image for a URL. This may trigger a network request for |
| 45 // the image if the image was not retrieved before and if a bookmark node has | 46 // the image if the image was not retrieved before and if a bookmark node has |
| 46 // a URL for this salient image available. The image (which may be empty) is | 47 // a URL for this salient image available. The image (which may be empty) is |
| 47 // sent via the callback. The callback may be called synchronously if it is | 48 // sent via the callback. The callback may be called synchronously if it is |
| 48 // possible. The callback is always triggered on the main thread. | 49 // possible. The callback is always triggered on the main thread. |
| 49 void SalientImageForUrl(const GURL& page_url, Callback callback); | 50 void SalientImageForUrl(const GURL& page_url, ImageCallback callback); |
| 50 | 51 |
| 51 // bookmarks::BookmarkModelObserver: | 52 // bookmarks::BookmarkModelObserver: |
| 52 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | 53 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
| 53 const BookmarkNode* parent, | 54 const BookmarkNode* parent, |
| 54 int old_index, | 55 int old_index, |
| 55 const BookmarkNode* node, | 56 const BookmarkNode* node, |
| 56 const std::set<GURL>& removed_urls) override; | 57 const std::set<GURL>& removed_urls) override; |
| 57 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, | 58 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, |
| 58 bool ids_reassigned) override; | 59 bool ids_reassigned) override; |
| 59 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, | 60 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 // one of the bookmark. | 102 // one of the bookmark. |
| 102 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url); | 103 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url); |
| 103 | 104 |
| 104 // PageUrls currently in the progress of being retrieved. | 105 // PageUrls currently in the progress of being retrieved. |
| 105 std::set<GURL> in_progress_page_urls_; | 106 std::set<GURL> in_progress_page_urls_; |
| 106 | 107 |
| 107 // Cached pointer to the bookmark model. | 108 // Cached pointer to the bookmark model. |
| 108 EnhancedBookmarkModel* enhanced_bookmark_model_; | 109 EnhancedBookmarkModel* enhanced_bookmark_model_; |
| 109 | 110 |
| 110 private: | 111 private: |
| 111 // Same as SalientImageForUrl(const GURL&, Callback) but can prevent the | 112 // Same as SalientImageForUrl(const GURL&, ImageCallback) but can |
| 112 // network request if fetch_from_bookmark is false. | 113 // prevent the network request if fetch_from_bookmark is false. |
| 113 void SalientImageForUrl(const GURL& page_url, | 114 void SalientImageForUrl(const GURL& page_url, |
| 114 bool fetch_from_bookmark, | 115 bool fetch_from_bookmark, |
| 115 Callback stack_callback); | 116 ImageCallback stack_callback); |
| 116 | 117 |
| 117 // Processes the requests that have been waiting on an image. | 118 // Processes the requests that have been waiting on an image. |
| 118 void ProcessRequests(const GURL& page_url, | 119 void ProcessRequests(const GURL& page_url, const ImageRecord& image); |
| 119 const gfx::Image& image, | |
| 120 const GURL& image_url); | |
| 121 | 120 |
| 122 // Once an image is retrieved this method updates the store with it. | 121 // Once an image is retrieved this method updates the store with it. Returns |
| 123 void StoreImage(const gfx::Image& image, | 122 // the newly formed ImageRecord. This is typically called on |pool_|, the |
| 124 const GURL& image_url, | 123 // background sequenced worker pool for this object. |
| 125 const GURL& page_url); | 124 ImageRecord StoreImage(const gfx::Image& image, |
| 125 const GURL& image_url, |
| 126 const GURL& page_url); |
| 127 |
| 128 // Calls |StoreImage| in the background. This should only be called from the |
| 129 // main thread. |
| 130 void PostTaskToStoreImage(const gfx::Image& image, |
| 131 const GURL& image_url, |
| 132 const GURL& page_url); |
| 133 |
| 134 // Called when |StoreImage| as been posted. This should only be called from |
| 135 // the main thread. |
| 136 void OnStoreImagePosted(const GURL& page_url, const ImageRecord& image); |
| 126 | 137 |
| 127 // Called when retrieving an image from the image store fails, to trigger | 138 // Called when retrieving an image from the image store fails, to trigger |
| 128 // retrieving the image from the url stored in the bookmark (if any). | 139 // retrieving the image from the url stored in the bookmark (if any). |
| 129 void FetchCallback(const GURL& page_url, | 140 void FetchCallback(const GURL& page_url, |
| 130 Callback original_callback, | 141 ImageCallback original_callback, |
| 131 const gfx::Image& image, | 142 const ImageRecord& record); |
| 132 const GURL& image_url); | |
| 133 | 143 |
| 134 // Remove the image stored for this bookmark (if it exists). Called when a | 144 // Remove the image stored for this bookmark (if it exists). Called when a |
| 135 // bookmark is deleted. | 145 // bookmark is deleted. |
| 136 void RemoveImageForUrl(const GURL& url); | 146 void RemoveImageForUrl(const GURL& url); |
| 137 | 147 |
| 138 // Moves an image from one url to another. | 148 // Moves an image from one url to another. |
| 139 void ChangeImageURL(const GURL& from, const GURL& to); | 149 void ChangeImageURL(const GURL& from, const GURL& to); |
| 140 | 150 |
| 141 // Removes all the entries in the image service. | 151 // Removes all the entries in the image service. |
| 142 void ClearAll(); | 152 void ClearAll(); |
| 143 | 153 |
| 144 // The image store can only be accessed from the blocking pool. | 154 // The image store can only be accessed from the blocking pool. |
| 145 // RetrieveImageFromStore starts a request to retrieve the image and returns | 155 // RetrieveImageFromStore starts a request to retrieve the image and returns |
| 146 // the result via a callback. RetrieveImageFromStore must be called on the | 156 // the result via a callback. RetrieveImageFromStore must be called on the |
| 147 // main thread and the callback will be called on the main thread as well. The | 157 // main thread and the callback will be called on the main thread as well. The |
| 148 // callback will always be called. The returned image is nil if the image is | 158 // callback will always be called. The returned image is nil if the image is |
| 149 // not present in the store. | 159 // not present in the store. |
| 150 void RetrieveImageFromStore(const GURL& page_url, | 160 void RetrieveImageFromStore(const GURL& page_url, ImageCallback callback); |
| 151 BookmarkImageService::Callback callback); | |
| 152 | 161 |
| 153 // Maps a pageUrl to an image. | 162 // Maps a pageUrl to an image. |
| 154 scoped_ptr<ImageStore> store_; | 163 scoped_ptr<ImageStore> store_; |
| 155 | 164 |
| 156 // All the callbacks waiting for a particular image. | 165 // All the callbacks waiting for a particular image. |
| 157 std::map<const GURL, std::vector<Callback> > callbacks_; | 166 std::map<const GURL, std::vector<ImageCallback>> callbacks_; |
| 158 | 167 |
| 159 // When a bookmark is changed, two messages are received on the | 168 // When a bookmark is changed, two messages are received on the |
| 160 // bookmarkModelObserver, one with the old state, one with the new. The url | 169 // bookmarkModelObserver, one with the old state, one with the new. The url |
| 161 // before the change is saved in this instance variable. | 170 // before the change is saved in this instance variable. |
| 162 GURL previous_url_; | 171 GURL previous_url_; |
| 163 | 172 |
| 164 // The worker pool to enqueue the store requests onto. | 173 // The worker pool to enqueue the requests onto. |
| 165 scoped_refptr<base::SequencedWorkerPool> pool_; | 174 scoped_refptr<base::SequencedWorkerPool> pool_; |
| 166 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService); | 175 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService); |
| 167 }; | 176 }; |
| 168 | 177 |
| 169 } // namespace enhanced_bookmarks | 178 } // namespace enhanced_bookmarks |
| 170 | 179 |
| 171 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ | 180 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
| OLD | NEW |