Update mojo sdk to rev d459e688a608f6eda850a23bb5e308a76ea51a47
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_image_service.h
blob9cc0b73a9fc32d9fa3bd3805ffee951ff153f318
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_
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_record.h"
12 #include "components/enhanced_bookmarks/image_store.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "net/url_request/url_request.h"
15 #include "url/gurl.h"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace bookmarks {
22 class BookmarkNode;
25 namespace enhanced_bookmarks {
27 class EnhancedBookmarkModel;
29 // The BookmarkImageService stores salient images for bookmarks.
30 class BookmarkImageService : public KeyedService,
31 public bookmarks::BookmarkModelObserver,
32 public base::NonThreadSafe {
33 public:
34 BookmarkImageService(const base::FilePath& path,
35 EnhancedBookmarkModel* enhanced_bookmark_model,
36 scoped_refptr<base::SequencedWorkerPool> pool);
37 BookmarkImageService(scoped_ptr<ImageStore> store,
38 EnhancedBookmarkModel* enhanced_bookmark_model,
39 scoped_refptr<base::SequencedWorkerPool> pool);
41 ~BookmarkImageService() override;
43 typedef base::Callback<void(scoped_refptr<ImageRecord>)> ImageCallback;
45 // KeyedService:
46 void Shutdown() override;
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
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
52 // possible. The callback is always triggered on the main thread.
53 void SalientImageForUrl(const GURL& page_url, ImageCallback callback);
55 // bookmarks::BookmarkModelObserver:
56 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
57 const bookmarks::BookmarkNode* parent,
58 int old_index,
59 const bookmarks::BookmarkNode* node,
60 const std::set<GURL>& removed_urls) override;
61 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
62 bool ids_reassigned) override;
63 void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
64 const bookmarks::BookmarkNode* old_parent,
65 int old_index,
66 const bookmarks::BookmarkNode* new_parent,
67 int new_index) override;
68 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
69 const bookmarks::BookmarkNode* parent,
70 int index) override;
71 void OnWillChangeBookmarkNode(bookmarks::BookmarkModel* model,
72 const bookmarks::BookmarkNode* node) override;
73 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
74 const bookmarks::BookmarkNode* node) override;
75 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
76 const bookmarks::BookmarkNode* node) override;
77 void BookmarkNodeChildrenReordered(
78 bookmarks::BookmarkModel* model,
79 const bookmarks::BookmarkNode* node) override;
80 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
81 const std::set<GURL>& removed_urls) override;
83 protected:
84 // Returns true if the image for the page_url is currently being fetched.
85 bool IsPageUrlInProgress(const GURL& page_url);
87 // Stores the new image to local storage. If update_bookmarks is true, relates
88 // the corresponding bookmark to image_url.
89 void ProcessNewImage(const GURL& page_url,
90 bool update_bookmarks,
91 const GURL& image_url,
92 scoped_ptr<gfx::Image> image);
94 // Resizes large images to proper size that fits device display. This method
95 // should _not_ run on the UI thread.
96 virtual scoped_ptr<gfx::Image> ResizeImage(const gfx::Image& image) = 0;
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
100 // stored. If update_bookmark is true the URL is also added to the bookmark.
101 virtual void RetrieveSalientImage(
102 const GURL& page_url,
103 const GURL& image_url,
104 const std::string& referrer,
105 net::URLRequest::ReferrerPolicy referrer_policy,
106 bool update_bookmark) = 0;
108 // Retrieves a salient image for a given page_url by downloading the image in
109 // one of the bookmark.
110 virtual void RetrieveSalientImageForPageUrl(const GURL& page_url);
112 // PageUrls currently in the progress of being retrieved.
113 std::set<GURL> in_progress_page_urls_;
115 // Cached pointer to the bookmark model.
116 EnhancedBookmarkModel* enhanced_bookmark_model_;
118 private:
119 // If fetch_from_web is true, retrieves the salient image via a network
120 // request; else only gets the image from local storage.
121 void SalientImageForUrl(const GURL& page_url,
122 bool fetch_from_web,
123 ImageCallback stack_callback);
125 // Processes the requests that have been waiting on an image.
126 void ProcessRequests(const GURL& page_url, scoped_refptr<ImageRecord> image);
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.
130 // This is typically called on |pool_|, the background sequenced worker pool
131 // for this object.
132 scoped_refptr<ImageRecord> ResizeAndStoreImage(
133 scoped_refptr<ImageRecord> image_info,
134 const GURL& page_url);
136 // Calls |StoreImage| in the background. This should only be called from the
137 // main thread.
138 void PostTaskToStoreImage(scoped_ptr<gfx::Image> image,
139 const GURL& image_url,
140 const GURL& page_url);
142 // Called when |StoreImage| as been posted. This should only be called from
143 // the main thread.
144 void OnStoreImagePosted(const GURL& page_url,
145 scoped_refptr<ImageRecord> image);
147 // Called when retrieving an image from the image store fails, to trigger
148 // retrieving the image from the url stored in the bookmark (if any).
149 void FetchCallback(const GURL& page_url,
150 ImageCallback original_callback,
151 scoped_refptr<ImageRecord> record);
153 // Remove the image stored for this bookmark (if it exists). Called when a
154 // bookmark is deleted.
155 void RemoveImageForUrl(const GURL& url);
157 // Moves an image from one url to another.
158 void ChangeImageURL(const GURL& from, const GURL& to);
160 // Removes all the entries in the image service.
161 void ClearAll();
163 // The image store can only be accessed from the blocking pool.
164 // RetrieveImageFromStore starts a request to retrieve the image and returns
165 // the result via a callback. RetrieveImageFromStore must be called on the
166 // main thread and the callback will be called on the main thread as well. The
167 // callback will always be called. The returned image is nil if the image is
168 // not present in the store.
169 void RetrieveImageFromStore(const GURL& page_url, ImageCallback callback);
171 // Maps a pageUrl to an image.
172 scoped_ptr<ImageStore> store_;
174 // All the callbacks waiting for a particular image.
175 std::map<const GURL, std::vector<ImageCallback>> callbacks_;
177 // When a bookmark is changed, two messages are received on the
178 // bookmarkModelObserver, one with the old state, one with the new. The url
179 // before the change is saved in this instance variable.
180 GURL previous_url_;
182 // The worker pool to enqueue the requests onto.
183 scoped_refptr<base::SequencedWorkerPool> pool_;
184 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService);
187 } // namespace enhanced_bookmarks
189 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_