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