Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / enhanced_bookmarks / bookmark_image_service_ios.h
blob8882e308cd88a06d1e75dd213f834ab8d0f132ea
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 IOS_CHROME_BROWSER_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_IOS_H_
5 #define IOS_CHROME_BROWSER_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_IOS_H_
7 #include "components/enhanced_bookmarks/bookmark_image_service.h"
9 #import <UIKit/UIKit.h>
11 #include "base/containers/mru_cache.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/enhanced_bookmarks/image_record.h"
15 #include "ios/chrome/browser/net/image_fetcher.h"
17 @protocol CRWJSInjectionEvaluator;
18 class GURL;
20 namespace web {
21 class NavigationItem;
24 class BookmarkImageServiceIOS
25 : public enhanced_bookmarks::BookmarkImageService {
26 public:
27 explicit BookmarkImageServiceIOS(
28 const base::FilePath& path,
29 enhanced_bookmarks::EnhancedBookmarkModel* enhanced_bookmark_model,
30 net::URLRequestContextGetter* context,
31 scoped_refptr<base::SequencedWorkerPool> pool);
32 BookmarkImageServiceIOS(
33 scoped_ptr<ImageStore> store,
34 enhanced_bookmarks::EnhancedBookmarkModel* enhanced_bookmark_model,
35 net::URLRequestContextGetter* context,
36 scoped_refptr<base::SequencedWorkerPool> pool);
37 ~BookmarkImageServiceIOS() override;
39 // Searches the pageContext for a salient image, if a url is found the image
40 // is fetched and stored.
41 void RetrieveSalientImageFromContext(id<CRWJSInjectionEvaluator> page_context,
42 const GURL& page_url,
43 bool update_bookmark);
45 // Invokes the superclass SalientImageForUrl, then resizes and optionally
46 // darkens the image.
47 void SalientImageResizedForUrl(const GURL& page_url,
48 const CGSize size,
49 bool darkened,
50 const ImageCallback& callback);
52 // Investigates if the newly visited page corresponding to |navigation_item|
53 // and |original_url| points to a bookmarked url in needs of an updated image.
54 // If it is, invokes RetrieveSalientImageFromContext() for the relevant urls
55 // with |page_context|.
56 void FinishSuccessfulPageLoadForNativationItem(
57 id<CRWJSInjectionEvaluator> page_context,
58 web::NavigationItem* navigation_item,
59 const GURL& original_url);
61 private:
62 // Resizes large images to proper size that fits device display. This method
63 // should _not_ run on the UI thread.
64 scoped_ptr<gfx::Image> ResizeImage(const gfx::Image& image) override;
66 // Retrieves a salient image for a given pageUrl by downloading the image in
67 // one of the bookmarks.
68 void RetrieveSalientImage(const GURL& page_url,
69 const GURL& image_url,
70 const std::string& referrer,
71 net::URLRequest::ReferrerPolicy referrer_policy,
72 bool update_bookmark) override;
74 // Caches the returned image before returning it.
75 void ReturnAndCache(
76 GURL page_url,
77 CGSize size,
78 bool darkened,
79 ImageCallback callback,
80 scoped_refptr<enhanced_bookmarks::ImageRecord> image_record);
82 // A cache to store the most recently found images, this allows serving those
83 // images synchronously.
84 class MRUKey;
85 typedef scoped_refptr<enhanced_bookmarks::ImageRecord> MRUValue;
86 scoped_ptr<base::MRUCache<MRUKey, MRUValue>> cache_;
87 // The helper that actually fetches all those images.
88 scoped_ptr<image_fetcher::ImageFetcher> imageFetcher_;
89 // The script injected in a page to extract the salient image.
90 base::scoped_nsobject<NSString> script_;
91 // The pool used by the bookmark image service to run resize operations.
92 scoped_refptr<base::SequencedWorkerPool> pool_;
93 // Must be last data member.
94 base::WeakPtrFactory<BookmarkImageServiceIOS> weak_ptr_factory_;
95 DISALLOW_COPY_AND_ASSIGN(BookmarkImageServiceIOS);
98 #endif // IOS_CHROME_BROWSER_ENHANCED_BOOKMARKS_BOOKMARK_IMAGE_SERVICE_IOS_H_