• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_EXTENSION_WEBSTORE_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBSTORE_PRIVATE_API_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "chrome/browser/browser_signin.h"
12 #include "chrome/browser/extensions/extension_function.h"
13 #include "chrome/browser/extensions/extension_install_ui.h"
14 #include "chrome/common/net/gaia/google_service_auth_error.h"
15 #include "content/common/notification_observer.h"
16 #include "content/common/notification_registrar.h"
17 
18 class ProfileSyncService;
19 
20 class WebstorePrivateApi {
21  public:
22   // Allows you to set the ProfileSyncService the function will use for
23   // testing purposes.
24   static void SetTestingProfileSyncService(ProfileSyncService* service);
25 
26   // Allows you to set the BrowserSignin the function will use for
27   // testing purposes.
28   static void SetTestingBrowserSignin(BrowserSignin* signin);
29 };
30 
31 // TODO(asargent): this is being deprecated in favor of
32 // BeginInstallWithManifestFunction. See crbug.com/75821 for details.
33 class BeginInstallFunction : public SyncExtensionFunction {
34  public:
35   // For use only in tests - sets a flag that can cause this function to ignore
36   // the normal requirement that it is called during a user gesture.
37   static void SetIgnoreUserGestureForTests(bool ignore);
38  protected:
39   virtual bool RunImpl();
40   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.beginInstall");
41 };
42 
43 class BeginInstallWithManifestFunction : public AsyncExtensionFunction,
44                                          public ExtensionInstallUI::Delegate {
45  public:
46   BeginInstallWithManifestFunction();
47 
48   // Result codes for the return value. If you change this, make sure to
49   // update the description for the beginInstallWithManifest callback in
50   // extension_api.json.
51   enum ResultCode {
52     ERROR_NONE = 0,
53 
54     // An unspecified error occurred.
55     UNKNOWN_ERROR,
56 
57     // The user cancelled the confirmation dialog instead of accepting it.
58     USER_CANCELLED,
59 
60     // The manifest failed to parse correctly.
61     MANIFEST_ERROR,
62 
63     // There was a problem parsing the base64 encoded icon data.
64     ICON_ERROR,
65 
66     // The extension id was invalid.
67     INVALID_ID,
68 
69     // The page does not have permission to call this function.
70     PERMISSION_DENIED,
71 
72     // The function was not called during a user gesture.
73     NO_GESTURE,
74   };
75 
76   // For use only in tests - sets a flag that can cause this function to ignore
77   // the normal requirement that it is called during a user gesture.
78   static void SetIgnoreUserGestureForTests(bool ignore);
79 
80   // Called when we've successfully parsed the manifest and icon data in the
81   // utility process. Ownership of parsed_manifest is transferred.
82   void OnParseSuccess(const SkBitmap& icon, DictionaryValue* parsed_manifest);
83 
84   // Called to indicate a parse failure. The |result_code| parameter should
85   // indicate whether the problem was with the manifest or icon data.
86   void OnParseFailure(ResultCode result_code, const std::string& error_message);
87 
88   // Implementing ExtensionInstallUI::Delegate interface.
89   virtual void InstallUIProceed() OVERRIDE;
90   virtual void InstallUIAbort() OVERRIDE;
91 
92  protected:
93   virtual ~BeginInstallWithManifestFunction();
94   virtual bool RunImpl();
95 
96   // Sets the result_ as a string based on |code|.
97   void SetResult(ResultCode code);
98 
99  private:
100   // These store the input parameters to the function.
101   std::string id_;
102   std::string manifest_;
103   std::string icon_data_;
104 
105   // The results of parsing manifest_ and icon_data_ go into these two.
106   scoped_ptr<DictionaryValue> parsed_manifest_;
107   SkBitmap icon_;
108 
109   // A dummy Extension object we create for the purposes of using
110   // ExtensionInstallUI to prompt for confirmation of the install.
111   scoped_refptr<Extension> dummy_extension_;
112   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.beginInstallWithManifest");
113 };
114 
115 class CompleteInstallFunction : public SyncExtensionFunction {
116   virtual bool RunImpl();
117   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.completeInstall");
118 };
119 
120 class GetBrowserLoginFunction : public SyncExtensionFunction {
121   virtual bool RunImpl();
122   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.getBrowserLogin");
123 };
124 
125 class GetStoreLoginFunction : public SyncExtensionFunction {
126   virtual bool RunImpl();
127   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.getStoreLogin");
128 };
129 
130 class SetStoreLoginFunction : public SyncExtensionFunction {
131   virtual bool RunImpl();
132   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.setStoreLogin");
133 };
134 
135 class PromptBrowserLoginFunction : public AsyncExtensionFunction,
136                                    public NotificationObserver,
137                                    public BrowserSignin::SigninDelegate {
138  public:
139   PromptBrowserLoginFunction();
140   // Implements BrowserSignin::SigninDelegate interface.
141   virtual void OnLoginSuccess();
142   virtual void OnLoginFailure(const GoogleServiceAuthError& error);
143 
144   // Implements the NotificationObserver interface.
145   virtual void Observe(NotificationType type,
146                        const NotificationSource& source,
147                        const NotificationDetails& details);
148 
149  protected:
150   virtual ~PromptBrowserLoginFunction();
151   virtual bool RunImpl();
152 
153  private:
154   // Creates the message for signing in.
155   virtual string16 GetLoginMessage();
156 
157   // Are we waiting for a token available notification?
158   bool waiting_for_token_;
159 
160   // Used for listening for TokenService notifications.
161   NotificationRegistrar registrar_;
162 
163   DECLARE_EXTENSION_FUNCTION_NAME("webstorePrivate.promptBrowserLogin");
164 };
165 
166 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBSTORE_PRIVATE_API_H_
167