OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
| 5 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/singleton.h" |
| 10 #include "components/bookmarks/browser/bookmark_model_observer.h" |
| 11 #include "components/enhanced_bookmarks/image_store.h" |
| 12 #include "components/keyed_service/core/keyed_service.h" |
| 13 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace base { |
| 17 class SingleThreadTaskRunner; |
| 18 } |
| 19 class BookmarkNode; |
| 20 |
| 21 namespace enhanced_bookmarks { |
| 22 |
| 23 // The BookmarkImageService stores salient images for bookmarks. |
| 24 class BookmarkImageService : public KeyedService, |
| 25 public BookmarkModelObserver, |
| 26 public base::NonThreadSafe { |
| 27 public: |
| 28 explicit BookmarkImageService(const base::FilePath& path, |
| 29 BookmarkModel* bookmark_model, |
| 30 scoped_refptr<base::SequencedWorkerPool> pool); |
| 31 BookmarkImageService(scoped_ptr<ImageStore> store, |
| 32 BookmarkModel* bookmark_model, |
| 33 scoped_refptr<base::SequencedWorkerPool> pool); |
| 34 |
| 35 virtual ~BookmarkImageService(); |
| 36 |
| 37 typedef base::Callback<void(const gfx::Image&, const GURL& url)> Callback; |
| 38 |
| 39 // Returns a salient image for a URL. This may trigger a network request for |
| 40 // the image if the image was not retrieved before and if a bookmark node has |
| 41 // a URL for this salient image available. The image (which may be empty) is |
| 42 // sent via the callback. The callback may be called synchronously if it is |
| 43 // possible. The callback is always triggered on the main thread. |
| 44 void SalientImageForUrl(const GURL& page_url, Callback callback); |
| 45 |
| 46 // BookmarkModelObserver methods. |
| 47 virtual void BookmarkNodeRemoved(BookmarkModel* model, |
| 48 const BookmarkNode* parent, |
| 49 int old_index, |
| 50 const BookmarkNode* node, |
| 51 const std::set<GURL>& removed_urls) OVERRIDE; |
| 52 virtual void BookmarkModelLoaded(BookmarkModel* model, |
| 53 bool ids_reassigned) OVERRIDE; |
| 54 virtual void BookmarkNodeMoved(BookmarkModel* model, |
| 55 const BookmarkNode* old_parent, |
| 56 int old_index, |
| 57 const BookmarkNode* new_parent, |
| 58 int new_index) OVERRIDE; |
| 59 virtual void BookmarkNodeAdded(BookmarkModel* model, |
| 60 const BookmarkNode* parent, |
| 61 int index) OVERRIDE; |
| 62 virtual void OnWillChangeBookmarkNode(BookmarkModel* model, |
| 63 const BookmarkNode* node) OVERRIDE; |
| 64 virtual void BookmarkNodeChanged(BookmarkModel* model, |
| 65 const BookmarkNode* node) OVERRIDE; |
| 66 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, |
| 67 const BookmarkNode* node) OVERRIDE; |
| 68 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, |
| 69 const BookmarkNode* node) OVERRIDE; |
| 70 virtual void BookmarkAllUserNodesRemoved( |
| 71 BookmarkModel* model, |
| 72 const std::set<GURL>& removed_urls) OVERRIDE; |
| 73 |
| 74 protected: |
| 75 // Returns true if the image for the page_url is currently being fetched. |
| 76 bool IsPageUrlInProgress(const GURL& page_url); |
| 77 |
| 78 // Once an image has been retrieved, store the image and notify all the |
| 79 // consumers that were waiting on it. |
| 80 void ProcessNewImage(const GURL& page_url, |
| 81 bool update_bookmarks, |
| 82 const gfx::Image& image, |
| 83 const GURL& image_url); |
| 84 |
| 85 // Sets a new image for a bookmark. If the given page_url is bookmarked and |
| 86 // the image is retrieved from the image_url, then the image is locally |
| 87 // stored. If update_bookmark is true the URL is also added to the bookmark. |
| 88 // This is the only method subclass needs to implement. |
| 89 virtual void RetrieveSalientImage( |
| 90 const GURL& page_url, |
| 91 const GURL& image_url, |
| 92 const std::string& referrer, |
| 93 net::URLRequest::ReferrerPolicy referrer_policy, |
| 94 bool update_bookmark) = 0; |
| 95 |
| 96 // Retrieves a salient image for a given page_url by downloading the image in |
| 97 // one of the bookmark. |
| 98 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url); |
| 99 |
| 100 // PageUrls currently in the progress of being retrieved. |
| 101 std::set<GURL> in_progress_page_urls_; |
| 102 |
| 103 // Cached pointer to the bookmarks model. |
| 104 BookmarkModel* bookmark_model_; // weak |
| 105 |
| 106 private: |
| 107 // Same as SalientImageForUrl(const GURL&, Callback) but can prevent the |
| 108 // network request if fetch_from_bookmark is false. |
| 109 void SalientImageForUrl(const GURL& page_url, |
| 110 bool fetch_from_bookmark, |
| 111 Callback stack_callback); |
| 112 |
| 113 // Processes the requests that have been waiting on an image. |
| 114 void ProcessRequests(const GURL& page_url, |
| 115 const gfx::Image& image, |
| 116 const GURL& image_url); |
| 117 |
| 118 // Once an image is retrieved this method updates the store with it. |
| 119 void StoreImage(const gfx::Image& image, |
| 120 const GURL& image_url, |
| 121 const GURL& page_url); |
| 122 |
| 123 // Called when retrieving an image from the image store fails, to trigger |
| 124 // retrieving the image from the url stored in the bookmark (if any). |
| 125 void FetchCallback(const GURL& page_url, |
| 126 Callback original_callback, |
| 127 const gfx::Image& image, |
| 128 const GURL& image_url); |
| 129 |
| 130 // Remove the image stored for this bookmark (if it exists). Called when a |
| 131 // bookmark is deleted. |
| 132 void RemoveImageForUrl(const GURL& url); |
| 133 |
| 134 // Moves an image from one url to another. |
| 135 void ChangeImageURL(const GURL& from, const GURL& to); |
| 136 |
| 137 // Removes all the entries in the image service. |
| 138 void ClearAll(); |
| 139 |
| 140 // The image store can only be accessed from the blocking pool. |
| 141 // RetrieveImageFromStore starts a request to retrieve the image and returns |
| 142 // the result via a callback. RetrieveImageFromStore must be called on the |
| 143 // main thread and the callback will be called on the main thread as well. The |
| 144 // callback will always be called. The returned image is nil if the image is |
| 145 // not present in the store. |
| 146 void RetrieveImageFromStore(const GURL& page_url, |
| 147 BookmarkImageService::Callback callback); |
| 148 |
| 149 // Maps a pageUrl to an image. |
| 150 scoped_ptr<ImageStore> store_; |
| 151 |
| 152 // All the callbacks waiting for a particular image. |
| 153 std::map<const GURL, std::vector<Callback> > callbacks_; |
| 154 |
| 155 // When a bookmark is changed, two messages are received on the |
| 156 // bookmarkModelObserver, one with the old state, one with the new. The url |
| 157 // before the change is saved in this instance variable. |
| 158 GURL previous_url_; |
| 159 |
| 160 // The worker pool to enqueue the store requests onto. |
| 161 scoped_refptr<base::SequencedWorkerPool> pool_; |
| 162 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService); |
| 163 }; |
| 164 |
| 165 } // namespace enhanced_bookmarks |
| 166 |
| 167 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_ |
OLD | NEW |