Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_image_service.h
blob87b14e49e8550777e66d0d8af37357a1da516320
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(const 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 const 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 gfx::Image ResizeImage(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, const 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 ImageRecord ResizeAndStoreImage(const gfx::Image& image,
133 const GURL& image_url,
134 const GURL& page_url);
136 // Calls |StoreImage| in the background. This should only be called from the
137 // main thread.
138 void PostTaskToStoreImage(const 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, const ImageRecord& image);
146 // 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 void FetchCallback(const GURL& page_url,
149 ImageCallback original_callback,
150 const ImageRecord& record);
152 // Remove the image stored for this bookmark (if it exists). Called when a
153 // bookmark is deleted.
154 void RemoveImageForUrl(const GURL& url);
156 // Moves an image from one url to another.
157 void ChangeImageURL(const GURL& from, const GURL& to);
159 // Removes all the entries in the image service.
160 void ClearAll();
162 // The image store can only be accessed from the blocking pool.
163 // RetrieveImageFromStore starts a request to retrieve the image and returns
164 // the result via a callback. RetrieveImageFromStore must be called on the
165 // main thread and the callback will be called on the main thread as well. The
166 // callback will always be called. The returned image is nil if the image is
167 // not present in the store.
168 void RetrieveImageFromStore(const GURL& page_url, ImageCallback callback);
170 // Maps a pageUrl to an image.
171 scoped_ptr<ImageStore> store_;
173 // All the callbacks waiting for a particular image.
174 std::map<const GURL, std::vector<ImageCallback>> callbacks_;
176 // When a bookmark is changed, two messages are received on the
177 // bookmarkModelObserver, one with the old state, one with the new. The url
178 // before the change is saved in this instance variable.
179 GURL previous_url_;
181 // The worker pool to enqueue the requests onto.
182 scoped_refptr<base::SequencedWorkerPool> pool_;
183 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService);
186 } // namespace enhanced_bookmarks
188 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_