e61cdb0fef8c4bc3f69864e28c3b7670f79fe59c
[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/active_install_data.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/common/extensions/api/webstore_private.h"
16 #include "chrome/common/extensions/webstore_install_result.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 WebstorePrivateBeginInstallWithManifest3Function
47     : public ChromeAsyncExtensionFunction,
48       public ExtensionInstallPrompt::Delegate,
49       public WebstoreInstallHelper::Delegate {
50  public:
51   DECLARE_EXTENSION_FUNCTION("webstorePrivate.beginInstallWithManifest3",
52                              WEBSTOREPRIVATE_BEGININSTALLWITHMANIFEST3)
53
54   // Result codes for the return value. If you change this, make sure to
55   // update the description for the beginInstallWithManifest3 callback in
56   // the extension API JSON.
57   enum ResultCode {
58     ERROR_NONE = 0,
59
60     // An unspecified error occurred.
61     UNKNOWN_ERROR,
62
63     // The user cancelled the confirmation dialog instead of accepting it.
64     USER_CANCELLED,
65
66     // The manifest failed to parse correctly.
67     MANIFEST_ERROR,
68
69     // There was a problem parsing the base64 encoded icon data.
70     ICON_ERROR,
71
72     // The extension id was invalid.
73     INVALID_ID,
74
75     // The page does not have permission to call this function.
76     PERMISSION_DENIED,
77
78     // Invalid icon url.
79     INVALID_ICON_URL,
80
81     // An extension with the same extension id has already been installed.
82     ALREADY_INSTALLED,
83   };
84
85   WebstorePrivateBeginInstallWithManifest3Function();
86
87   // WebstoreInstallHelper::Delegate:
88   void OnWebstoreParseSuccess(const std::string& id,
89                               const SkBitmap& icon,
90                               base::DictionaryValue* parsed_manifest) override;
91   void OnWebstoreParseFailure(const std::string& id,
92                               InstallHelperResultCode result_code,
93                               const std::string& error_message) override;
94
95   // ExtensionInstallPrompt::Delegate:
96   void InstallUIProceed() override;
97   void InstallUIAbort(bool user_initiated) override;
98
99  protected:
100   ~WebstorePrivateBeginInstallWithManifest3Function() override;
101
102   // ExtensionFunction:
103   bool RunAsync() override;
104
105   // Sets the result_ as a string based on |code|.
106   void SetResultCode(ResultCode code);
107
108  private:
109   const char* ResultCodeToString(ResultCode code);
110
111   // These store the input parameters to the function.
112   scoped_ptr<api::webstore_private::BeginInstallWithManifest3::Params> params_;
113
114   // The results of parsing manifest_ and icon_data_ go into these two.
115   scoped_ptr<base::DictionaryValue> parsed_manifest_;
116   SkBitmap icon_;
117
118   // A dummy Extension object we create for the purposes of using
119   // ExtensionInstallPrompt to prompt for confirmation of the install.
120   scoped_refptr<extensions::Extension> dummy_extension_;
121
122   // The class that displays the install prompt.
123   scoped_ptr<ExtensionInstallPrompt> install_prompt_;
124
125   scoped_ptr<ScopedActiveInstall> scoped_active_install_;
126
127   // The authuser query parameter value which should be used with CRX download
128   // requests. This is empty if authuser should not be set on download requests.
129   std::string authuser_;
130 };
131
132 class WebstorePrivateCompleteInstallFunction
133     : public ChromeAsyncExtensionFunction,
134       public WebstoreInstaller::Delegate {
135  public:
136   DECLARE_EXTENSION_FUNCTION("webstorePrivate.completeInstall",
137                              WEBSTOREPRIVATE_COMPLETEINSTALL)
138
139   WebstorePrivateCompleteInstallFunction();
140
141   // WebstoreInstaller::Delegate:
142   void OnExtensionInstallSuccess(const std::string& id) override;
143   void OnExtensionInstallFailure(
144       const std::string& id,
145       const std::string& error,
146       WebstoreInstaller::FailureReason reason) override;
147
148  protected:
149   ~WebstorePrivateCompleteInstallFunction() override;
150
151   // ExtensionFunction:
152   bool RunAsync() override;
153
154  private:
155   scoped_ptr<WebstoreInstaller::Approval> approval_;
156   scoped_ptr<ScopedActiveInstall> scoped_active_install_;
157
158   void OnInstallSuccess(const std::string& id);
159 };
160
161 class WebstorePrivateEnableAppLauncherFunction
162     : public ChromeSyncExtensionFunction {
163  public:
164   DECLARE_EXTENSION_FUNCTION("webstorePrivate.enableAppLauncher",
165                              WEBSTOREPRIVATE_ENABLEAPPLAUNCHER)
166
167   WebstorePrivateEnableAppLauncherFunction();
168
169  protected:
170   ~WebstorePrivateEnableAppLauncherFunction() override;
171
172   // ExtensionFunction:
173   bool RunSync() override;
174 };
175
176 class WebstorePrivateGetBrowserLoginFunction
177     : public ChromeSyncExtensionFunction {
178  public:
179   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getBrowserLogin",
180                              WEBSTOREPRIVATE_GETBROWSERLOGIN)
181
182  protected:
183   ~WebstorePrivateGetBrowserLoginFunction() override {}
184
185   // ExtensionFunction:
186   bool RunSync() override;
187 };
188
189 class WebstorePrivateGetStoreLoginFunction
190     : public ChromeSyncExtensionFunction {
191  public:
192   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getStoreLogin",
193                              WEBSTOREPRIVATE_GETSTORELOGIN)
194
195  protected:
196   ~WebstorePrivateGetStoreLoginFunction() override {}
197
198   // ExtensionFunction:
199   bool RunSync() override;
200 };
201
202 class WebstorePrivateSetStoreLoginFunction
203     : public ChromeSyncExtensionFunction {
204  public:
205   DECLARE_EXTENSION_FUNCTION("webstorePrivate.setStoreLogin",
206                              WEBSTOREPRIVATE_SETSTORELOGIN)
207
208  protected:
209   ~WebstorePrivateSetStoreLoginFunction() override {}
210
211   // ExtensionFunction:
212   bool RunSync() override;
213 };
214
215 class WebstorePrivateGetWebGLStatusFunction
216     : public ChromeAsyncExtensionFunction {
217  public:
218   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getWebGLStatus",
219                              WEBSTOREPRIVATE_GETWEBGLSTATUS)
220
221   WebstorePrivateGetWebGLStatusFunction();
222
223  protected:
224   ~WebstorePrivateGetWebGLStatusFunction() override;
225
226   void OnFeatureCheck(bool feature_allowed);
227
228   // ExtensionFunction:
229   bool RunAsync() override;
230
231  private:
232   void CreateResult(bool webgl_allowed);
233
234   scoped_refptr<GPUFeatureChecker> feature_checker_;
235 };
236
237 class WebstorePrivateGetIsLauncherEnabledFunction
238     : public ChromeSyncExtensionFunction {
239  public:
240   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getIsLauncherEnabled",
241                              WEBSTOREPRIVATE_GETISLAUNCHERENABLED)
242
243   WebstorePrivateGetIsLauncherEnabledFunction() {}
244
245  protected:
246   ~WebstorePrivateGetIsLauncherEnabledFunction() override {}
247
248   // ExtensionFunction:
249   bool RunSync() override;
250
251  private:
252   void OnIsLauncherCheckCompleted(bool is_enabled);
253 };
254
255 class WebstorePrivateIsInIncognitoModeFunction
256     : public ChromeSyncExtensionFunction {
257  public:
258   DECLARE_EXTENSION_FUNCTION("webstorePrivate.isInIncognitoMode",
259                              WEBSTOREPRIVATE_ISININCOGNITOMODEFUNCTION)
260
261   WebstorePrivateIsInIncognitoModeFunction() {}
262
263  protected:
264   ~WebstorePrivateIsInIncognitoModeFunction() override {}
265
266   // ExtensionFunction:
267   bool RunSync() override;
268 };
269
270 class WebstorePrivateLaunchEphemeralAppFunction
271     : public ChromeAsyncExtensionFunction {
272  public:
273   DECLARE_EXTENSION_FUNCTION("webstorePrivate.launchEphemeralApp",
274                              WEBSTOREPRIVATE_LAUNCHEPHEMERALAPP)
275
276   WebstorePrivateLaunchEphemeralAppFunction();
277
278  protected:
279   ~WebstorePrivateLaunchEphemeralAppFunction() override;
280
281   // ExtensionFunction:
282   bool RunAsync() override;
283
284  private:
285   void OnLaunchComplete(webstore_install::Result result,
286                         const std::string& error);
287   void SetResult(
288       api::webstore_private::LaunchEphemeralApp::Results::Result result,
289       const std::string& error);
290 };
291
292 class WebstorePrivateGetEphemeralAppsEnabledFunction
293     : public ChromeSyncExtensionFunction {
294  public:
295   DECLARE_EXTENSION_FUNCTION("webstorePrivate.getEphemeralAppsEnabled",
296                              WEBSTOREPRIVATE_GETEPHEMERALAPPSENABLED)
297
298   WebstorePrivateGetEphemeralAppsEnabledFunction();
299
300  protected:
301   ~WebstorePrivateGetEphemeralAppsEnabledFunction() override;
302
303   // ExtensionFunction:
304   bool RunSync() override;
305 };
306
307 }  // namespace extensions
308
309 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_