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