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_store.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "net/url_request/url_request.h"
17 class SingleThreadTaskRunner
;
21 namespace enhanced_bookmarks
{
23 class EnhancedBookmarkModel
;
25 // The BookmarkImageService stores salient images for bookmarks.
26 class BookmarkImageService
: public KeyedService
,
27 public BookmarkModelObserver
,
28 public base::NonThreadSafe
{
30 BookmarkImageService(const base::FilePath
& path
,
31 EnhancedBookmarkModel
* enhanced_bookmark_model
,
32 scoped_refptr
<base::SequencedWorkerPool
> pool
);
33 BookmarkImageService(scoped_ptr
<ImageStore
> store
,
34 EnhancedBookmarkModel
* enhanced_bookmark_model
,
35 scoped_refptr
<base::SequencedWorkerPool
> pool
);
37 ~BookmarkImageService() override
;
39 typedef base::Callback
<void(const gfx::Image
&, const GURL
& url
)> Callback
;
42 void Shutdown() override
;
44 // 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 // 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 // possible. The callback is always triggered on the main thread.
49 void SalientImageForUrl(const GURL
& page_url
, Callback callback
);
51 // BookmarkModelObserver methods.
52 void BookmarkNodeRemoved(BookmarkModel
* model
,
53 const BookmarkNode
* parent
,
55 const BookmarkNode
* node
,
56 const std::set
<GURL
>& removed_urls
) override
;
57 void BookmarkModelLoaded(BookmarkModel
* model
, bool ids_reassigned
) override
;
58 void BookmarkNodeMoved(BookmarkModel
* model
,
59 const BookmarkNode
* old_parent
,
61 const BookmarkNode
* new_parent
,
62 int new_index
) override
;
63 void BookmarkNodeAdded(BookmarkModel
* model
,
64 const BookmarkNode
* parent
,
66 void OnWillChangeBookmarkNode(BookmarkModel
* model
,
67 const BookmarkNode
* node
) override
;
68 void BookmarkNodeChanged(BookmarkModel
* model
,
69 const BookmarkNode
* node
) override
;
70 void BookmarkNodeFaviconChanged(BookmarkModel
* model
,
71 const BookmarkNode
* node
) override
;
72 void BookmarkNodeChildrenReordered(BookmarkModel
* model
,
73 const BookmarkNode
* node
) override
;
74 void BookmarkAllUserNodesRemoved(BookmarkModel
* model
,
75 const std::set
<GURL
>& removed_urls
) override
;
78 // Returns true if the image for the page_url is currently being fetched.
79 bool IsPageUrlInProgress(const GURL
& page_url
);
81 // Once an image has been retrieved, store the image and notify all the
82 // consumers that were waiting on it.
83 void ProcessNewImage(const GURL
& page_url
,
84 bool update_bookmarks
,
85 const gfx::Image
& image
,
86 const GURL
& image_url
);
88 // Sets a new image for a bookmark. If the given page_url is bookmarked and
89 // the image is retrieved from the image_url, then the image is locally
90 // stored. If update_bookmark is true the URL is also added to the bookmark.
91 // This is the only method subclass needs to implement.
92 virtual void RetrieveSalientImage(
94 const GURL
& image_url
,
95 const std::string
& referrer
,
96 net::URLRequest::ReferrerPolicy referrer_policy
,
97 bool update_bookmark
) = 0;
99 // Retrieves a salient image for a given page_url by downloading the image in
100 // one of the bookmark.
101 virtual void RetrieveSalientImageForPageUrl(const GURL
& page_url
);
103 // PageUrls currently in the progress of being retrieved.
104 std::set
<GURL
> in_progress_page_urls_
;
106 // Cached pointer to the bookmark model.
107 EnhancedBookmarkModel
* enhanced_bookmark_model_
;
110 // Same as SalientImageForUrl(const GURL&, Callback) but can prevent the
111 // network request if fetch_from_bookmark is false.
112 void SalientImageForUrl(const GURL
& page_url
,
113 bool fetch_from_bookmark
,
114 Callback stack_callback
);
116 // Processes the requests that have been waiting on an image.
117 void ProcessRequests(const GURL
& page_url
,
118 const gfx::Image
& image
,
119 const GURL
& image_url
);
121 // Once an image is retrieved this method updates the store with it.
122 void StoreImage(const gfx::Image
& image
,
123 const GURL
& image_url
,
124 const GURL
& page_url
);
126 // Called when retrieving an image from the image store fails, to trigger
127 // retrieving the image from the url stored in the bookmark (if any).
128 void FetchCallback(const GURL
& page_url
,
129 Callback original_callback
,
130 const gfx::Image
& image
,
131 const GURL
& image_url
);
133 // Remove the image stored for this bookmark (if it exists). Called when a
134 // bookmark is deleted.
135 void RemoveImageForUrl(const GURL
& url
);
137 // Moves an image from one url to another.
138 void ChangeImageURL(const GURL
& from
, const GURL
& to
);
140 // Removes all the entries in the image service.
143 // The image store can only be accessed from the blocking pool.
144 // RetrieveImageFromStore starts a request to retrieve the image and returns
145 // the result via a callback. RetrieveImageFromStore must be called on the
146 // main thread and the callback will be called on the main thread as well. The
147 // callback will always be called. The returned image is nil if the image is
148 // not present in the store.
149 void RetrieveImageFromStore(const GURL
& page_url
,
150 BookmarkImageService::Callback callback
);
152 // Maps a pageUrl to an image.
153 scoped_ptr
<ImageStore
> store_
;
155 // All the callbacks waiting for a particular image.
156 std::map
<const GURL
, std::vector
<Callback
> > callbacks_
;
158 // When a bookmark is changed, two messages are received on the
159 // bookmarkModelObserver, one with the old state, one with the new. The url
160 // before the change is saved in this instance variable.
163 // The worker pool to enqueue the store requests onto.
164 scoped_refptr
<base::SequencedWorkerPool
> pool_
;
165 DISALLOW_COPY_AND_ASSIGN(BookmarkImageService
);
168 } // namespace enhanced_bookmarks
170 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_H_