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 namespace base {
     18 class RefCountedString;
     19 }
     20 
     21 namespace extensions {
     22 class WebstoreDataFetcher;
     23 }
     24 
     25 namespace net {
     26 class URLRequestContextGetter;
     27 }
     28 
     29 namespace chromeos {
     30 
     31 class KioskAppDataDelegate;
     32 
     33 // Fetches an app's web store data and manages the cached info such as name
     34 // and icon.
     35 class 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   KioskAppDataDelegate* delegate_;  // not owned.
    104   Status status_;
    105 
    106   std::string app_id_;
    107   std::string user_id_;
    108   std::string name_;
    109   gfx::ImageSkia icon_;
    110   scoped_refptr<base::RefCountedString> raw_icon_;
    111 
    112   scoped_ptr<extensions::WebstoreDataFetcher> webstore_fetcher_;
    113   base::FilePath icon_path_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(KioskAppData);
    116 };
    117 
    118 }  // namespace chromeos
    119 
    120 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_DATA_H_
    121