Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / webstore_private / webstore_private_api.h
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
204 class WebstorePrivateEnableAppLauncherFunction
205     : public ChromeSyncExtensionFunction {
206  public:
207   DECLARE_EXTENSION_FUNCTION("webstorePrivate.enableAppLauncher",
208                              WEBSTOREPRIVATE_ENABLEAPPLAUNCHER)
209
210   WebstorePrivateEnableAppLauncherFunction();
211
212  protected:
213   virtual ~WebstorePrivateEnableAppLauncherFunction();
214
215   // ExtensionFunction:
216   virtual bool RunSync() OVERRIDE;
217 };
218
219 class WebstorePrivateGetBrowserLoginFunction
220     : public ChromeSyncExtensionFunction {
221  public:
222   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getBrowserLogin",
223                              WEBSTOREPRIVATE_GETBROWSERLOGIN)
224
225  protected:
226   virtual ~WebstorePrivateGetBrowserLoginFunction() {}
227
228   // ExtensionFunction:
229   virtual bool RunSync() OVERRIDE;
230 };
231
232 class WebstorePrivateGetStoreLoginFunction
233     : public ChromeSyncExtensionFunction {
234  public:
235   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getStoreLogin",
236                              WEBSTOREPRIVATE_GETSTORELOGIN)
237
238  protected:
239   virtual ~WebstorePrivateGetStoreLoginFunction() {}
240
241   // ExtensionFunction:
242   virtual bool RunSync() OVERRIDE;
243 };
244
245 class WebstorePrivateSetStoreLoginFunction
246     : public ChromeSyncExtensionFunction {
247  public:
248   DECLARE_EXTENSION_FUNCTION("webstorePrivate.setStoreLogin",
249                              WEBSTOREPRIVATE_SETSTORELOGIN)
250
251  protected:
252   virtual ~WebstorePrivateSetStoreLoginFunction() {}
253
254   // ExtensionFunction:
255   virtual bool RunSync() OVERRIDE;
256 };
257
258 class WebstorePrivateGetWebGLStatusFunction
259     : public ChromeAsyncExtensionFunction {
260  public:
261   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getWebGLStatus",
262                              WEBSTOREPRIVATE_GETWEBGLSTATUS)
263
264   WebstorePrivateGetWebGLStatusFunction();
265
266  protected:
267   virtual ~WebstorePrivateGetWebGLStatusFunction();
268
269   void OnFeatureCheck(bool feature_allowed);
270
271   // ExtensionFunction:
272   virtual bool RunAsync() OVERRIDE;
273
274  private:
275   void CreateResult(bool webgl_allowed);
276
277   scoped_refptr<GPUFeatureChecker> feature_checker_;
278 };
279
280 class WebstorePrivateGetIsLauncherEnabledFunction
281     : public ChromeSyncExtensionFunction {
282  public:
283   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getIsLauncherEnabled",
284                              WEBSTOREPRIVATE_GETISLAUNCHERENABLED)
285
286   WebstorePrivateGetIsLauncherEnabledFunction() {}
287
288  protected:
289   virtual ~WebstorePrivateGetIsLauncherEnabledFunction() {}
290
291   // ExtensionFunction:
292   virtual bool RunSync() OVERRIDE;
293
294  private:
295   void OnIsLauncherCheckCompleted(bool is_enabled);
296 };
297
298 class WebstorePrivateIsInIncognitoModeFunction
299     : public ChromeSyncExtensionFunction {
300  public:
301   DECLARE_EXTENSION_FUNCTION("webstorePrivate.isInIncognitoMode",
302                              WEBSTOREPRIVATE_ISININCOGNITOMODEFUNCTION)
303
304   WebstorePrivateIsInIncognitoModeFunction() {}
305
306  protected:
307   virtual ~WebstorePrivateIsInIncognitoModeFunction() {}
308
309   // ExtensionFunction:
310   virtual bool RunSync() OVERRIDE;
311 };
312
313 class WebstorePrivateSignInFunction : public ChromeAsyncExtensionFunction,
314                                       public SigninManagerFactory::Observer,
315                                       public SigninTracker::Observer {
316  public:
317   DECLARE_EXTENSION_FUNCTION("webstorePrivate.signIn",
318                              WEBSTOREPRIVATE_SIGNINFUNCTION)
319
320   WebstorePrivateSignInFunction();
321
322  protected:
323   virtual ~WebstorePrivateSignInFunction();
324
325   // ExtensionFunction:
326   virtual bool RunAsync() OVERRIDE;
327
328   // SigninManagerFactory::Observer:
329   virtual void SigninManagerShutdown(SigninManagerBase* manager) OVERRIDE;
330
331   // SigninTracker::Observer:
332   virtual void SigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
333   virtual void SigninSuccess() OVERRIDE;
334   virtual void MergeSessionComplete(const GoogleServiceAuthError& error)
335       OVERRIDE;
336
337  private:
338   // The sign-in manager for the invoking tab's Chrome Profile. Weak reference.
339   SigninManagerBase* signin_manager_;
340
341   // Tracks changes to sign-in state. Used to notify the page when an existing
342   // in-progress sign-in completes, either with success or failure.
343   scoped_ptr<SigninTracker> signin_tracker_;
344 };
345
346 }  // namespace extensions
347
348 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_