kiosk_app_data.h revision 58537e28ecd584eab876aee8be7156509866d23a
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_CHROMEOS_APP_MODE_KIOSK_APP_DATA_H_
6#define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_DATA_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/files/file_path.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/weak_ptr.h"
14#include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
15#include "ui/gfx/image/image_skia.h"
16
17namespace base {
18class RefCountedString;
19}
20
21namespace extensions {
22class WebstoreDataFetcher;
23}
24
25namespace net {
26class URLRequestContextGetter;
27}
28
29namespace chromeos {
30
31class KioskAppDataDelegate;
32
33// Fetches an app's web store data and manages the cached info such as name
34// and icon.
35class KioskAppData : public base::SupportsWeakPtr<KioskAppData>,
36                     public extensions::WebstoreDataFetcherDelegate {
37 public:
38  enum Status {
39    STATUS_INIT,     // Data initialized with app id.
40    STATUS_LOADING,  // Loading data from cache or web store.
41    STATUS_LOADED,   // Data loaded.
42    STATUS_ERROR,    // Failed to load data.
43  };
44
45  KioskAppData(KioskAppDataDelegate* delegate,
46               const std::string& app_id,
47               const std::string& user_id);
48  virtual ~KioskAppData();
49
50  // Loads app data from cache. If there is no cached data, fetches it
51  // from web store.
52  void Load();
53
54  // Clears locally cached data.
55  void ClearCache();
56
57  // Returns true if web store data fetching is in progress.
58  bool IsLoading() const;
59
60  const std::string& app_id() const { return app_id_; }
61  const std::string& user_id() const { return user_id_; }
62  const std::string& name() const { return name_; }
63  const gfx::ImageSkia& icon() const { return icon_; }
64  const base::RefCountedString* raw_icon() const {
65    return raw_icon_.get();
66  }
67  Status status() const { return status_; }
68
69 private:
70  class IconLoader;
71  class WebstoreDataParser;
72
73  void SetStatus(Status status);
74
75  // Returns URLRequestContextGetter to use for fetching web store data.
76  net::URLRequestContextGetter* GetRequestContextGetter();
77
78  // Loads the locally cached data. Return false if there is none.
79  bool LoadFromCache();
80
81  // Sets the cached data.
82  void SetCache(const std::string& name, const base::FilePath& icon_path);
83
84  // Callbacks for IconLoader.
85  void OnIconLoadSuccess(const scoped_refptr<base::RefCountedString>& raw_icon,
86                         const gfx::ImageSkia& icon);
87  void OnIconLoadFailure();
88
89  // Callbacks for WebstoreDataParser
90  void OnWebstoreParseSuccess(const SkBitmap& icon);
91  void OnWebstoreParseFailure();
92
93  // Starts to fetch data from web store.
94  void StartFetch();
95
96  // extensions::WebstoreDataFetcherDelegate overrides:
97  virtual void OnWebstoreRequestFailure() OVERRIDE;
98  virtual void OnWebstoreResponseParseSuccess(
99      base::DictionaryValue* webstore_data) OVERRIDE;
100  virtual void OnWebstoreResponseParseFailure(
101      const std::string& error) OVERRIDE;
102
103  // Helper function for testing for the existence of |key| in
104  // |response|. Passes |key|'s content via |value| and returns
105  // true when |key| is present.
106  bool CheckResponseKeyValue(const base::DictionaryValue* response,
107                             const char* key,
108                             std::string* value);
109
110  KioskAppDataDelegate* delegate_;  // not owned.
111  Status status_;
112
113  std::string app_id_;
114  std::string user_id_;
115  std::string name_;
116  gfx::ImageSkia icon_;
117  scoped_refptr<base::RefCountedString> raw_icon_;
118
119  scoped_ptr<extensions::WebstoreDataFetcher> webstore_fetcher_;
120  base::FilePath icon_path_;
121
122  DISALLOW_COPY_AND_ASSIGN(KioskAppData);
123};
124
125}  // namespace chromeos
126
127#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_DATA_H_
128