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(float scale) OVERRIDE;
49
50  // net::URLFetcherDelegate overrides:
51  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
52
53  // ImageDecoder::Delegate overrides:
54  virtual void OnImageDecoded(const ImageDecoder* decoder,
55                              const SkBitmap& decoded_image) OVERRIDE;
56  virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
57
58  IconLoadedCallback icon_loaded_callback_;
59  net::URLRequestContextGetter* context_getter_;
60  const GURL icon_url_;
61  const int icon_size_;
62  const int default_icon_resource_id_;
63
64  bool icon_fetch_attempted_;
65  scoped_ptr<net::URLFetcher> icon_fetcher_;
66
67  scoped_refptr<ImageDecoder> image_decoder_;
68
69  gfx::ImageSkia icon_;
70
71  DISALLOW_COPY_AND_ASSIGN(UrlIconSource);
72};
73
74}  // namespace app_list
75
76#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_URL_ICON_SOURCE_H_
77