Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / bitmap_fetcher / bitmap_fetcher_service.h
blobcc3ecba422699bfe1c510fbbf105a5a30ca4086b
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 CHROME_BROWSER_BITMAP_FETCHER_BITMAP_FETCHER_SERVICE_H_
5 #define CHROME_BROWSER_BITMAP_FETCHER_BITMAP_FETCHER_SERVICE_H_
7 #include "base/compiler_specific.h"
8 #include "base/containers/mru_cache.h"
9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
13 #include "components/keyed_service/core/keyed_service.h"
15 namespace content {
16 class BrowserContext;
17 } // namespace content
19 namespace chrome {
20 class BitmapFetcher;
21 } // namespace chrome
23 class BitmapFetcherRequest;
24 class GURL;
25 class SkBitmap;
27 // Service to retrieve images for Answers in Suggest.
28 class BitmapFetcherService : public KeyedService,
29 public chrome::BitmapFetcherDelegate {
30 public:
31 typedef int RequestId;
32 static const RequestId REQUEST_ID_INVALID = 0;
34 class Observer {
35 public:
36 virtual ~Observer() {}
38 // Called whenever the image changes. Called with an empty image if the
39 // fetch failed or the request ended for any reason.
40 // TODO(dschuyler) The comment differs from what the code does, follow-up.
41 virtual void OnImageChanged(RequestId request_id,
42 const SkBitmap& answers_image) = 0;
45 explicit BitmapFetcherService(content::BrowserContext* context);
46 ~BitmapFetcherService() override;
48 // Cancels a request, if it is still in-flight.
49 void CancelRequest(RequestId requestId);
51 // Requests a new image. Will either trigger download or satisfy from cache.
52 // Takes ownership of |observer|. If there are too many outstanding requests,
53 // the request will fail and |observer| will be called to signal failure.
54 // Otherwise, |observer| will be called with either the cached image or the
55 // downloaded one.
56 // NOTE: The observer might be called back synchronously from RequestImage if
57 // the image is already in the cache.
58 RequestId RequestImage(const GURL& url, Observer* observer);
60 // Start fetching the image at the given |url|.
61 void Prefetch(const GURL& url);
63 protected:
64 // Create a bitmap fetcher for the given |url| and start it. Virtual method
65 // so tests can override this for different behavior.
66 virtual chrome::BitmapFetcher* CreateFetcher(const GURL& url);
68 private:
69 friend class BitmapFetcherServiceTest;
71 typedef ScopedVector<chrome::BitmapFetcher> BitmapFetchers;
73 // Gets the existing fetcher for |url| or constructs a new one if it doesn't
74 // exist.
75 const chrome::BitmapFetcher* EnsureFetcherForUrl(const GURL& url);
77 // Find a fetcher with a given |url|. Return NULL if none is found.
78 const chrome::BitmapFetcher* FindFetcherForUrl(const GURL& url);
80 // Remove |fetcher| from list of active fetchers. |fetcher| MUST be part of
81 // the list.
82 void RemoveFetcher(const chrome::BitmapFetcher* fetcher);
84 // BitmapFetcherDelegate implementation.
85 void OnFetchComplete(const GURL& url, const SkBitmap* bitmap) override;
87 // Currently active image fetchers.
88 BitmapFetchers active_fetchers_;
90 // Currently active requests.
91 ScopedVector<BitmapFetcherRequest> requests_;
93 // Cache of retrieved images.
94 struct CacheEntry {
95 CacheEntry();
96 ~CacheEntry();
98 scoped_ptr<const SkBitmap> bitmap;
100 base::OwningMRUCache<GURL, CacheEntry*> cache_;
102 // Current request ID to be used.
103 int current_request_id_;
105 // Browser context this service is active for.
106 content::BrowserContext* context_;
108 DISALLOW_COPY_AND_ASSIGN(BitmapFetcherService);
111 #endif // CHROME_BROWSER_BITMAP_FETCHER_BITMAP_FETCHER_SERVICE_H_