Home | History | Annotate | Download | only in sync_notifier
      1 // Copyright 2013 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_NOTIFICATIONS_SYNC_NOTIFIER_NOTIFICATION_BITMAP_FETCHER_H_
      6 #define CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_NOTIFICATION_BITMAP_FETCHER_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "chrome/browser/image_decoder.h"
     10 #include "net/url_request/url_fetcher_delegate.h"
     11 #include "third_party/skia/include/core/SkBitmap.h"
     12 #include "url/gurl.h"
     13 
     14 namespace net {
     15 class URLFetcher;
     16 }  // namespace net
     17 
     18 class Profile;
     19 
     20 namespace notifier {
     21 
     22 // A delegate interface for users of NotificationBitmapFetcher.
     23 class NotificationBitmapFetcherDelegate {
     24  public:
     25   // This will be called when the bitmap has been requested, whether or not the
     26   // request succeeds.  |url| is the URL that was originally fetched so we can
     27   // match up the bitmap with a specific request.
     28   virtual void OnFetchComplete(const GURL url, const SkBitmap* bitmap) = 0;
     29 
     30  protected:
     31   virtual ~NotificationBitmapFetcherDelegate() {}
     32 };
     33 
     34 class NotificationBitmapFetcher
     35     : public net::URLFetcherDelegate,
     36       public ImageDecoder::Delegate {
     37  public:
     38   NotificationBitmapFetcher(
     39       const GURL& url,
     40       NotificationBitmapFetcherDelegate* delegate);
     41   virtual ~NotificationBitmapFetcher();
     42 
     43   GURL url() const { return url_; }
     44 
     45   // Start fetching the URL with the fetcher.  The operation will be continued
     46   // in the OnURLFetchComplete callback.
     47   void Start(Profile* profile);
     48 
     49   // Methods inherited from URLFetcherDelegate
     50 
     51   // This will be called when the URL has been fetched, successfully or not.
     52   // Use accessor methods on |source| to get the results.
     53   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     54 
     55   // This will be called when some part of the response is read. |current|
     56   // denotes the number of bytes received up to the call, and |total| is the
     57   // expected total size of the response (or -1 if not determined).
     58   virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source,
     59                                           int64 current, int64 total) OVERRIDE;
     60 
     61   // Methods inherited from ImageDecoder::Delegate
     62 
     63   // Called when image is decoded. |decoder| is used to identify the image in
     64   // case of decoding several images simultaneously.  This will not be called
     65   // on the UI thread.
     66   virtual void OnImageDecoded(const ImageDecoder* decoder,
     67                               const SkBitmap& decoded_image) OVERRIDE;
     68 
     69   // Called when decoding image failed.
     70   virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
     71 
     72  private:
     73   scoped_ptr<net::URLFetcher> url_fetcher_;
     74   scoped_refptr<ImageDecoder> image_decoder_;
     75   const GURL url_;
     76   scoped_ptr<SkBitmap> bitmap_;
     77   NotificationBitmapFetcherDelegate* const delegate_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(NotificationBitmapFetcher);
     80 };
     81 
     82 }  // namespace notifier
     83 
     84 #endif  // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_NOTIFICATION_BITMAP_FETCHER_H_
     85