Home | History | Annotate | Download | only in webstore_private
      1 // Copyright (c) 2012 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_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_
      7 
      8 #include <string>
      9 
     10 #include "chrome/browser/extensions/bundle_installer.h"
     11 #include "chrome/browser/extensions/extension_function.h"
     12 #include "chrome/browser/extensions/extension_install_prompt.h"
     13 #include "chrome/browser/extensions/webstore_install_helper.h"
     14 #include "chrome/browser/extensions/webstore_installer.h"
     15 #include "chrome/browser/signin/signin_tracker.h"
     16 #include "content/public/browser/gpu_data_manager_observer.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_registrar.h"
     19 #include "google_apis/gaia/google_service_auth_error.h"
     20 #include "third_party/skia/include/core/SkBitmap.h"
     21 
     22 class ProfileSyncService;
     23 
     24 namespace content {
     25 class GpuDataManager;
     26 }
     27 
     28 class GPUFeatureChecker;
     29 
     30 namespace extensions {
     31 
     32 class WebstorePrivateApi {
     33  public:
     34   // Allows you to override the WebstoreInstaller delegate for testing.
     35   static void SetWebstoreInstallerDelegateForTesting(
     36       WebstoreInstaller::Delegate* delegate);
     37 
     38   // Gets the pending approval for the |extension_id| in |profile|. Pending
     39   // approvals are held between the calls to beginInstallWithManifest and
     40   // completeInstall. This should only be used for testing.
     41   static scoped_ptr<WebstoreInstaller::Approval> PopApprovalForTesting(
     42       Profile* profile, const std::string& extension_id);
     43 };
     44 
     45 class WebstorePrivateInstallBundleFunction : public AsyncExtensionFunction,
     46                               public extensions::BundleInstaller::Delegate {
     47  public:
     48   DECLARE_EXTENSION_FUNCTION("webstorePrivate.installBundle",
     49                              WEBSTOREPRIVATE_INSTALLBUNDLE)
     50 
     51   WebstorePrivateInstallBundleFunction();
     52 
     53   // BundleInstaller::Delegate:
     54   virtual void OnBundleInstallApproved() OVERRIDE;
     55   virtual void OnBundleInstallCanceled(bool user_initiated) OVERRIDE;
     56   virtual void OnBundleInstallCompleted() OVERRIDE;
     57 
     58  protected:
     59   virtual ~WebstorePrivateInstallBundleFunction();
     60 
     61   // ExtensionFunction:
     62   virtual bool RunImpl() OVERRIDE;
     63 
     64   // Reads the extension |details| into |items|.
     65   bool ReadBundleInfo(base::ListValue* details,
     66                       extensions::BundleInstaller::ItemList* items);
     67 
     68  private:
     69   scoped_refptr<extensions::BundleInstaller> bundle_;
     70 };
     71 
     72 class WebstorePrivateBeginInstallWithManifest3Function
     73     : public AsyncExtensionFunction,
     74       public ExtensionInstallPrompt::Delegate,
     75       public WebstoreInstallHelper::Delegate,
     76       public SigninTracker::Observer {
     77  public:
     78   DECLARE_EXTENSION_FUNCTION("webstorePrivate.beginInstallWithManifest3",
     79                              WEBSTOREPRIVATE_BEGININSTALLWITHMANIFEST3)
     80 
     81   // Result codes for the return value. If you change this, make sure to
     82   // update the description for the beginInstallWithManifest3 callback in
     83   // the extension API JSON.
     84   enum ResultCode {
     85     ERROR_NONE = 0,
     86 
     87     // An unspecified error occurred.
     88     UNKNOWN_ERROR,
     89 
     90     // The user cancelled the confirmation dialog instead of accepting it.
     91     USER_CANCELLED,
     92 
     93     // The manifest failed to parse correctly.
     94     MANIFEST_ERROR,
     95 
     96     // There was a problem parsing the base64 encoded icon data.
     97     ICON_ERROR,
     98 
     99     // The extension id was invalid.
    100     INVALID_ID,
    101 
    102     // The page does not have permission to call this function.
    103     PERMISSION_DENIED,
    104 
    105     // Invalid icon url.
    106     INVALID_ICON_URL,
    107 
    108     // Signin has failed.
    109     SIGNIN_FAILED,
    110 
    111     // An extension with the same extension id has already been installed.
    112     ALREADY_INSTALLED,
    113   };
    114 
    115   WebstorePrivateBeginInstallWithManifest3Function();
    116 
    117   // WebstoreInstallHelper::Delegate:
    118   virtual void OnWebstoreParseSuccess(
    119       const std::string& id,
    120       const SkBitmap& icon,
    121       base::DictionaryValue* parsed_manifest) OVERRIDE;
    122   virtual void OnWebstoreParseFailure(
    123       const std::string& id,
    124       InstallHelperResultCode result_code,
    125       const std::string& error_message) OVERRIDE;
    126 
    127   // ExtensionInstallPrompt::Delegate:
    128   virtual void InstallUIProceed() OVERRIDE;
    129   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
    130 
    131  protected:
    132   virtual ~WebstorePrivateBeginInstallWithManifest3Function();
    133 
    134   // ExtensionFunction:
    135   virtual bool RunImpl() OVERRIDE;
    136 
    137   // Sets the result_ as a string based on |code|.
    138   void SetResultCode(ResultCode code);
    139 
    140  private:
    141   // SigninTracker::Observer override.
    142   virtual void SigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
    143   virtual void SigninSuccess() OVERRIDE;
    144 
    145   // Called when signin is complete or not needed.
    146   void SigninCompletedOrNotNeeded();
    147 
    148   // These store the input parameters to the function.
    149   std::string id_;
    150   std::string manifest_;
    151   std::string icon_data_;
    152   std::string localized_name_;
    153   bool use_app_installed_bubble_;
    154   bool enable_launcher_;
    155 
    156   // The results of parsing manifest_ and icon_data_ go into these two.
    157   scoped_ptr<base::DictionaryValue> parsed_manifest_;
    158   SkBitmap icon_;
    159 
    160   // A dummy Extension object we create for the purposes of using
    161   // ExtensionInstallPrompt to prompt for confirmation of the install.
    162   scoped_refptr<extensions::Extension> dummy_extension_;
    163 
    164   // The class that displays the install prompt.
    165   scoped_ptr<ExtensionInstallPrompt> install_prompt_;
    166 
    167   scoped_ptr<SigninTracker> signin_tracker_;
    168 };
    169 
    170 class WebstorePrivateCompleteInstallFunction
    171     : public AsyncExtensionFunction,
    172       public WebstoreInstaller::Delegate {
    173  public:
    174   DECLARE_EXTENSION_FUNCTION("webstorePrivate.completeInstall",
    175                              WEBSTOREPRIVATE_COMPLETEINSTALL)
    176 
    177   WebstorePrivateCompleteInstallFunction();
    178 
    179   // WebstoreInstaller::Delegate:
    180   virtual void OnExtensionInstallSuccess(const std::string& id) OVERRIDE;
    181   virtual void OnExtensionInstallFailure(
    182       const std::string& id,
    183       const std::string& error,
    184       WebstoreInstaller::FailureReason reason) OVERRIDE;
    185 
    186  protected:
    187   virtual ~WebstorePrivateCompleteInstallFunction();
    188 
    189   // ExtensionFunction:
    190   virtual bool RunImpl() OVERRIDE;
    191 
    192  private:
    193   scoped_ptr<WebstoreInstaller::Approval> approval_;
    194 };
    195 
    196 class WebstorePrivateEnableAppLauncherFunction
    197     : public AsyncExtensionFunction {
    198  public:
    199   DECLARE_EXTENSION_FUNCTION("webstorePrivate.enableAppLauncher",
    200                              WEBSTOREPRIVATE_ENABLEAPPLAUNCHER)
    201 
    202   WebstorePrivateEnableAppLauncherFunction();
    203 
    204  protected:
    205   virtual ~WebstorePrivateEnableAppLauncherFunction();
    206 
    207   // ExtensionFunction:
    208   virtual bool RunImpl() OVERRIDE;
    209 };
    210 
    211 class WebstorePrivateGetBrowserLoginFunction : public SyncExtensionFunction {
    212  public:
    213   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getBrowserLogin",
    214                              WEBSTOREPRIVATE_GETBROWSERLOGIN)
    215 
    216  protected:
    217   virtual ~WebstorePrivateGetBrowserLoginFunction() {}
    218 
    219   // ExtensionFunction:
    220   virtual bool RunImpl() OVERRIDE;
    221 };
    222 
    223 class WebstorePrivateGetStoreLoginFunction : public SyncExtensionFunction {
    224  public:
    225   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getStoreLogin",
    226                              WEBSTOREPRIVATE_GETSTORELOGIN)
    227 
    228  protected:
    229   virtual ~WebstorePrivateGetStoreLoginFunction() {}
    230 
    231   // ExtensionFunction:
    232   virtual bool RunImpl() OVERRIDE;
    233 };
    234 
    235 class WebstorePrivateSetStoreLoginFunction : public SyncExtensionFunction {
    236  public:
    237   DECLARE_EXTENSION_FUNCTION("webstorePrivate.setStoreLogin",
    238                              WEBSTOREPRIVATE_SETSTORELOGIN)
    239 
    240  protected:
    241   virtual ~WebstorePrivateSetStoreLoginFunction() {}
    242 
    243   // ExtensionFunction:
    244   virtual bool RunImpl() OVERRIDE;
    245 };
    246 
    247 class WebstorePrivateGetWebGLStatusFunction : public AsyncExtensionFunction {
    248  public:
    249   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getWebGLStatus",
    250                              WEBSTOREPRIVATE_GETWEBGLSTATUS)
    251 
    252   WebstorePrivateGetWebGLStatusFunction();
    253 
    254  protected:
    255   virtual ~WebstorePrivateGetWebGLStatusFunction();
    256 
    257   void OnFeatureCheck(bool feature_allowed);
    258 
    259   // ExtensionFunction:
    260   virtual bool RunImpl() OVERRIDE;
    261 
    262  private:
    263   void CreateResult(bool webgl_allowed);
    264 
    265   scoped_refptr<GPUFeatureChecker> feature_checker_;
    266 };
    267 
    268 class WebstorePrivateGetIsLauncherEnabledFunction
    269     : public AsyncExtensionFunction {
    270  public:
    271   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getIsLauncherEnabled",
    272                              WEBSTOREPRIVATE_GETISLAUNCHERENABLED)
    273 
    274   WebstorePrivateGetIsLauncherEnabledFunction() {}
    275 
    276  protected:
    277   virtual ~WebstorePrivateGetIsLauncherEnabledFunction() {}
    278 
    279   // ExtensionFunction:
    280   virtual bool RunImpl() OVERRIDE;
    281 
    282  private:
    283   void OnIsLauncherCheckCompleted(bool is_enabled);
    284 };
    285 
    286 class WebstorePrivateIsInIncognitoModeFunction : public AsyncExtensionFunction {
    287  public:
    288   DECLARE_EXTENSION_FUNCTION("webstorePrivate.isInIncognitoMode",
    289                              WEBSTOREPRIVATE_ISININCOGNITOMODEFUNCTION)
    290 
    291   WebstorePrivateIsInIncognitoModeFunction() {}
    292 
    293  protected:
    294   virtual ~WebstorePrivateIsInIncognitoModeFunction() {}
    295 
    296   // ExtensionFunction:
    297   virtual bool RunImpl() OVERRIDE;
    298 };
    299 
    300 }  // namespace extensions
    301 
    302 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_
    303