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"
17 } // namespace content
23 class BitmapFetcherRequest
;
27 // Service to retrieve images for Answers in Suggest.
28 class BitmapFetcherService
: public KeyedService
,
29 public chrome::BitmapFetcherDelegate
{
31 typedef int RequestId
;
32 static const RequestId REQUEST_ID_INVALID
= 0;
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
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
);
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
);
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
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
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.
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_