url_icon_source.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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_UI_APP_LIST_SEARCH_COMMON_URL_ICON_SOURCE_H_
6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_URL_ICON_SOURCE_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/image_decoder.h"
13#include "net/url_request/url_fetcher_delegate.h"
14#include "ui/gfx/image/image_skia.h"
15#include "ui/gfx/image/image_skia_source.h"
16#include "url/gurl.h"
17
18namespace net {
19class URLFetcher;
20class URLRequestContextGetter;
21}
22
23namespace app_list {
24
25// An ImageSkiaSource for icons fetched from a URL. Till the URL icon is
26// fetched, the default icon (specified by it's resource id) is shown.
27class UrlIconSource : public gfx::ImageSkiaSource,
28                      public net::URLFetcherDelegate,
29                      public ImageDecoder::Delegate {
30 public:
31  typedef base::Closure IconLoadedCallback;
32
33  // Create a URL Icon source with the given URL. The post_process parameter
34  // specifies a function to post-process the result icon before displaying it.
35  UrlIconSource(const IconLoadedCallback& icon_loaded_callback,
36                net::URLRequestContextGetter* context_getter,
37                const GURL& icon_url,
38                int icon_size,
39                int default_icon_resource_id);
40  virtual ~UrlIconSource();
41
42 private:
43  // Invoked from GetImageForScale to download the app icon when the hosting
44  // ImageSkia gets painted on screen.
45  void StartIconFetch();
46
47  // gfx::ImageSkiaSource overrides:
48  virtual gfx::ImageSkiaRep GetImageForScale(
49      ui::ScaleFactor scale_factor) OVERRIDE;
50
51  // net::URLFetcherDelegate overrides:
52  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
53
54  // ImageDecoder::Delegate overrides:
55  virtual void OnImageDecoded(const ImageDecoder* decoder,
56                              const SkBitmap& decoded_image) OVERRIDE;
57  virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
58
59  IconLoadedCallback icon_loaded_callback_;
60  net::URLRequestContextGetter* context_getter_;
61  const GURL icon_url_;
62  const int icon_size_;
63  const int default_icon_resource_id_;
64
65  bool icon_fetch_attempted_;
66  scoped_ptr<net::URLFetcher> icon_fetcher_;
67
68  scoped_refptr<ImageDecoder> image_decoder_;
69
70  gfx::ImageSkia icon_;
71
72  DISALLOW_COPY_AND_ASSIGN(UrlIconSource);
73};
74
75}  // namespace app_list
76
77#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_URL_ICON_SOURCE_H_
78