- add sources.
[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_tracker.h"
16 #include "chrome/common/extensions/api/webstore_private.h"
17 #include "content/public/browser/gpu_data_manager_observer.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "google_apis/gaia/google_service_auth_error.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22
23 class ProfileSyncService;
24
25 namespace content {
26 class GpuDataManager;
27 }
28
29 class GPUFeatureChecker;
30
31 namespace extensions {
32
33 class WebstorePrivateApi {
34  public:
35   // Allows you to override the WebstoreInstaller delegate for testing.
36   static void SetWebstoreInstallerDelegateForTesting(
37       WebstoreInstaller::Delegate* delegate);
38
39   // Gets the pending approval for the |extension_id| in |profile|. Pending
40   // approvals are held between the calls to beginInstallWithManifest and
41   // completeInstall. This should only be used for testing.
42   static scoped_ptr<WebstoreInstaller::Approval> PopApprovalForTesting(
43       Profile* profile, const std::string& extension_id);
44 };
45
46 class WebstorePrivateInstallBundleFunction
47     : public ChromeAsyncExtensionFunction,
48       public extensions::BundleInstaller::Delegate {
49  public:
50   DECLARE_EXTENSION_FUNCTION("webstorePrivate.installBundle",
51                              WEBSTOREPRIVATE_INSTALLBUNDLE)
52
53   WebstorePrivateInstallBundleFunction();
54
55   // BundleInstaller::Delegate:
56   virtual void OnBundleInstallApproved() OVERRIDE;
57   virtual void OnBundleInstallCanceled(bool user_initiated) OVERRIDE;
58   virtual void OnBundleInstallCompleted() OVERRIDE;
59
60  protected:
61   virtual ~WebstorePrivateInstallBundleFunction();
62
63   // ExtensionFunction:
64   virtual bool RunImpl() OVERRIDE;
65
66   // Reads the extension |details| into |items|.
67   bool ReadBundleInfo(
68       const api::webstore_private::InstallBundle::Params& details,
69           extensions::BundleInstaller::ItemList* items);
70
71  private:
72   scoped_refptr<extensions::BundleInstaller> bundle_;
73 };
74
75 class WebstorePrivateBeginInstallWithManifest3Function
76     : public ChromeAsyncExtensionFunction,
77       public ExtensionInstallPrompt::Delegate,
78       public WebstoreInstallHelper::Delegate,
79       public SigninTracker::Observer {
80  public:
81   DECLARE_EXTENSION_FUNCTION("webstorePrivate.beginInstallWithManifest3",
82                              WEBSTOREPRIVATE_BEGININSTALLWITHMANIFEST3)
83
84   // Result codes for the return value. If you change this, make sure to
85   // update the description for the beginInstallWithManifest3 callback in
86   // the extension API JSON.
87   enum ResultCode {
88     ERROR_NONE = 0,
89
90     // An unspecified error occurred.
91     UNKNOWN_ERROR,
92
93     // The user cancelled the confirmation dialog instead of accepting it.
94     USER_CANCELLED,
95
96     // The manifest failed to parse correctly.
97     MANIFEST_ERROR,
98
99     // There was a problem parsing the base64 encoded icon data.
100     ICON_ERROR,
101
102     // The extension id was invalid.
103     INVALID_ID,
104
105     // The page does not have permission to call this function.
106     PERMISSION_DENIED,
107
108     // Invalid icon url.
109     INVALID_ICON_URL,
110
111     // Signin has failed.
112     SIGNIN_FAILED,
113
114     // An extension with the same extension id has already been installed.
115     ALREADY_INSTALLED,
116   };
117
118   WebstorePrivateBeginInstallWithManifest3Function();
119
120   // WebstoreInstallHelper::Delegate:
121   virtual void OnWebstoreParseSuccess(
122       const std::string& id,
123       const SkBitmap& icon,
124       base::DictionaryValue* parsed_manifest) OVERRIDE;
125   virtual void OnWebstoreParseFailure(
126       const std::string& id,
127       InstallHelperResultCode result_code,
128       const std::string& error_message) OVERRIDE;
129
130   // ExtensionInstallPrompt::Delegate:
131   virtual void InstallUIProceed() OVERRIDE;
132   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
133
134  protected:
135   virtual ~WebstorePrivateBeginInstallWithManifest3Function();
136
137   // ExtensionFunction:
138   virtual bool RunImpl() OVERRIDE;
139
140   // Sets the result_ as a string based on |code|.
141   void SetResultCode(ResultCode code);
142
143  private:
144   // SigninTracker::Observer override.
145   virtual void SigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
146   virtual void SigninSuccess() OVERRIDE;
147
148   // Called when signin is complete or not needed.
149   void SigninCompletedOrNotNeeded();
150
151   const char* ResultCodeToString(ResultCode code);
152
153   // These store the input parameters to the function.
154   scoped_ptr<api::webstore_private::BeginInstallWithManifest3::Params> params_;
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 ChromeAsyncExtensionFunction,
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 ChromeSyncExtensionFunction {
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
212     : public ChromeSyncExtensionFunction {
213  public:
214   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getBrowserLogin",
215                              WEBSTOREPRIVATE_GETBROWSERLOGIN)
216
217  protected:
218   virtual ~WebstorePrivateGetBrowserLoginFunction() {}
219
220   // ExtensionFunction:
221   virtual bool RunImpl() OVERRIDE;
222 };
223
224 class WebstorePrivateGetStoreLoginFunction
225     : public ChromeSyncExtensionFunction {
226  public:
227   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getStoreLogin",
228                              WEBSTOREPRIVATE_GETSTORELOGIN)
229
230  protected:
231   virtual ~WebstorePrivateGetStoreLoginFunction() {}
232
233   // ExtensionFunction:
234   virtual bool RunImpl() OVERRIDE;
235 };
236
237 class WebstorePrivateSetStoreLoginFunction
238     : public ChromeSyncExtensionFunction {
239  public:
240   DECLARE_EXTENSION_FUNCTION("webstorePrivate.setStoreLogin",
241                              WEBSTOREPRIVATE_SETSTORELOGIN)
242
243  protected:
244   virtual ~WebstorePrivateSetStoreLoginFunction() {}
245
246   // ExtensionFunction:
247   virtual bool RunImpl() OVERRIDE;
248 };
249
250 class WebstorePrivateGetWebGLStatusFunction
251     : public ChromeAsyncExtensionFunction {
252  public:
253   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getWebGLStatus",
254                              WEBSTOREPRIVATE_GETWEBGLSTATUS)
255
256   WebstorePrivateGetWebGLStatusFunction();
257
258  protected:
259   virtual ~WebstorePrivateGetWebGLStatusFunction();
260
261   void OnFeatureCheck(bool feature_allowed);
262
263   // ExtensionFunction:
264   virtual bool RunImpl() OVERRIDE;
265
266  private:
267   void CreateResult(bool webgl_allowed);
268
269   scoped_refptr<GPUFeatureChecker> feature_checker_;
270 };
271
272 class WebstorePrivateGetIsLauncherEnabledFunction
273     : public ChromeSyncExtensionFunction {
274  public:
275   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getIsLauncherEnabled",
276                              WEBSTOREPRIVATE_GETISLAUNCHERENABLED)
277
278   WebstorePrivateGetIsLauncherEnabledFunction() {}
279
280  protected:
281   virtual ~WebstorePrivateGetIsLauncherEnabledFunction() {}
282
283   // ExtensionFunction:
284   virtual bool RunImpl() OVERRIDE;
285
286  private:
287   void OnIsLauncherCheckCompleted(bool is_enabled);
288 };
289
290 class WebstorePrivateIsInIncognitoModeFunction
291     : public ChromeSyncExtensionFunction {
292  public:
293   DECLARE_EXTENSION_FUNCTION("webstorePrivate.isInIncognitoMode",
294                              WEBSTOREPRIVATE_ISININCOGNITOMODEFUNCTION)
295
296   WebstorePrivateIsInIncognitoModeFunction() {}
297
298  protected:
299   virtual ~WebstorePrivateIsInIncognitoModeFunction() {}
300
301   // ExtensionFunction:
302   virtual bool RunImpl() OVERRIDE;
303 };
304
305 }  // namespace extensions
306
307 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_