Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bitmap_fetcher.h
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
5 #ifndef CHROME_BROWSER_BITMAP_FETCHER_H_
6 #define CHROME_BROWSER_BITMAP_FETCHER_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/bitmap_fetcher_delegate.h"
10 #include "chrome/browser/image_decoder.h"
11 #include "net/url_request/url_fetcher_delegate.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "url/gurl.h"
14
15 namespace net {
16 class URLFetcher;
17 }  // namespace net
18
19 class Profile;
20
21 namespace chrome {
22
23 // Asynchrounously fetches an image from the given URL and returns the
24 // decoded Bitmap to the provided BitmapFetcherDelegate.
25 class BitmapFetcher : public net::URLFetcherDelegate,
26                       public ImageDecoder::Delegate {
27  public:
28   BitmapFetcher(const GURL& url, BitmapFetcherDelegate* delegate);
29   virtual ~BitmapFetcher();
30
31   const GURL& url() const { return url_; }
32
33   // Start fetching the URL with the fetcher. The delegate is notified
34   // asynchronously when done.
35   void Start(Profile* profile);
36
37   // Methods inherited from URLFetcherDelegate
38
39   // This will be called when the URL has been fetched, successfully or not.
40   // Use accessor methods on |source| to get the results.
41   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
42
43   // This will be called when some part of the response is read. |current|
44   // denotes the number of bytes received up to the call, and |total| is the
45   // expected total size of the response (or -1 if not determined).
46   virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source,
47                                           int64 current,
48                                           int64 total) OVERRIDE;
49
50   // Methods inherited from ImageDecoder::Delegate
51
52   // Called when image is decoded. |decoder| is used to identify the image in
53   // case of decoding several images simultaneously.  This will not be called
54   // on the UI thread.
55   virtual void OnImageDecoded(const ImageDecoder* decoder,
56                               const SkBitmap& decoded_image) OVERRIDE;
57
58   // Called when decoding image failed.
59   virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
60
61  private:
62   // Alerts the delegate that a failure occurred.
63   void ReportFailure();
64
65   scoped_ptr<net::URLFetcher> url_fetcher_;
66   scoped_refptr<ImageDecoder> image_decoder_;
67   const GURL url_;
68   BitmapFetcherDelegate* const delegate_;
69
70   DISALLOW_COPY_AND_ASSIGN(BitmapFetcher);
71 };
72
73 }  // namespace chrome
74
75 #endif  // CHROME_BROWSER_BITMAP_FETCHER_H_