blob: fa586fa124f3904be3a89ca989ac037f45b11f2c [file] [log] [blame]
[email protected]bdb74a22012-01-25 20:33:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8915f342011-08-29 22:14:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]734bcec2012-10-08 20:29:055#ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_STANDALONE_INSTALLER_H_
6#define CHROME_BROWSER_EXTENSIONS_WEBSTORE_STANDALONE_INSTALLER_H_
[email protected]8915f342011-08-29 22:14:377
dchengc963c7142016-04-08 03:55:228#include <memory>
[email protected]8915f342011-08-29 22:14:379#include <string>
10
[email protected]d2a639e2012-09-17 07:41:2111#include "base/callback.h"
avia2f4804a2015-12-24 23:11:1312#include "base/macros.h"
[email protected]8915f342011-08-29 22:14:3713#include "base/memory/ref_counted.h"
[email protected]773272b2014-07-18 05:48:3514#include "chrome/browser/extensions/active_install_data.h"
[email protected]c82da8c42012-06-08 19:49:1115#include "chrome/browser/extensions/extension_install_prompt.h"
[email protected]7b3941052013-02-13 01:21:5916#include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
[email protected]8915f342011-08-29 22:14:3717#include "chrome/browser/extensions/webstore_install_helper.h"
[email protected]10c2d692012-05-11 05:32:2318#include "chrome/browser/extensions/webstore_installer.h"
[email protected]ced522c2014-07-23 20:23:5919#include "chrome/common/extensions/webstore_install_result.h"
[email protected]15fb2aa2012-05-22 22:52:5920#include "net/url_request/url_fetcher_delegate.h"
[email protected]8915f342011-08-29 22:14:3721#include "third_party/skia/include/core/SkBitmap.h"
22
[email protected]d44ec7b2013-03-15 04:34:3423namespace base {
24class DictionaryValue;
25}
[email protected]1c321ee2012-05-21 03:02:3426
[email protected]3d61a7f2012-07-12 19:11:2527namespace extensions {
28class Extension;
[email protected]7b3941052013-02-13 01:21:5929class WebstoreDataFetcher;
[email protected]3d61a7f2012-07-12 19:11:2530
[email protected]d44ec7b2013-03-15 04:34:3431// A a purely abstract base for concrete classes implementing various types of
32// standalone installs:
33// 1) Downloads and parses metadata from the webstore.
34// 2) Optionally shows an install dialog.
35// 3) Starts download once the user confirms (if confirmation was requested).
36// 4) Optionally shows a post-install UI.
37// Follows the Template Method pattern. Implementing subclasses must override
38// the primitive hooks in the corresponding section below.
39
[email protected]734bcec2012-10-08 20:29:0540class WebstoreStandaloneInstaller
41 : public base::RefCountedThreadSafe<WebstoreStandaloneInstaller>,
[email protected]7b3941052013-02-13 01:21:5942 public WebstoreDataFetcherDelegate,
[email protected]cb08ba22011-10-19 21:41:4043 public WebstoreInstaller::Delegate,
[email protected]8915f342011-08-29 22:14:3744 public WebstoreInstallHelper::Delegate {
45 public:
[email protected]d44ec7b2013-03-15 04:34:3446 // A callback for when the install process completes, successfully or not. If
[email protected]d2a639e2012-09-17 07:41:2147 // there was a failure, |success| will be false and |error| may contain a
48 // developer-readable error message about why it failed.
Devlin Cronin1cf10162019-01-04 01:07:5149 using Callback = base::OnceCallback<void(bool success,
50 const std::string& error,
51 webstore_install::Result result)>;
[email protected]d2a639e2012-09-17 07:41:2152
[email protected]d44ec7b2013-03-15 04:34:3453 WebstoreStandaloneInstaller(const std::string& webstore_item_id,
[email protected]c1b2d042013-02-23 00:31:0454 Profile* profile,
Devlin Cronin1cf10162019-01-04 01:07:5155 Callback callback);
[email protected]8915f342011-08-29 22:14:3756 void BeginInstall();
57
[email protected]d44ec7b2013-03-15 04:34:3458 protected:
dchengae36a4a2014-10-21 12:36:3659 ~WebstoreStandaloneInstaller() override;
[email protected]d44ec7b2013-03-15 04:34:3460
rdevlin.cronin5f6b69d2014-09-20 01:23:3561 // Runs the callback; primarily used for running a callback before it is
Devlin Cronin1cf10162019-01-04 01:07:5162 // cleared in AbortInstall(). This should only be called once for the lifetime
63 // of the class.
rdevlin.cronin5f6b69d2014-09-20 01:23:3564 void RunCallback(
65 bool success, const std::string& error, webstore_install::Result result);
66
[email protected]9c7b3092014-06-21 01:19:0367 // Called when the install should be aborted. The callback is cleared.
[email protected]d44ec7b2013-03-15 04:34:3468 void AbortInstall();
[email protected]9c7b3092014-06-21 01:19:0369
[email protected]773272b2014-07-18 05:48:3570 // Checks InstallTracker and returns true if the same extension is not
71 // currently being installed. Registers this install with the InstallTracker.
72 bool EnsureUniqueInstall(webstore_install::Result* reason,
73 std::string* error);
74
[email protected]9c7b3092014-06-21 01:19:0375 // Called when the install is complete.
76 virtual void CompleteInstall(webstore_install::Result result,
77 const std::string& error);
78
79 // Called when the installer should proceed to prompt the user.
80 void ProceedWithInstallPrompt();
81
82 // Lazily creates a dummy extension for display from the parsed manifest. This
83 // is safe to call from OnManifestParsed() onwards. The manifest may be
84 // invalid, thus the caller must check that the return value is not NULL.
85 scoped_refptr<const Extension> GetLocalizedExtensionForDisplay();
[email protected]d44ec7b2013-03-15 04:34:3486
87 // Template Method's hooks to be implemented by subclasses.
88
[email protected]d44ec7b2013-03-15 04:34:3489 // Called at certain check points of the workflow to decide whether it makes
90 // sense to proceed with installation. A requestor can be a website that
91 // initiated an inline installation, or a command line option.
92 virtual bool CheckRequestorAlive() const = 0;
93
[email protected]d44ec7b2013-03-15 04:34:3494 // Should a new tab be opened after installation to show the newly installed
95 // extension's icon?
96 virtual bool ShouldShowPostInstallUI() const = 0;
97
98 // Should pop up an "App installed" bubble after installation?
99 virtual bool ShouldShowAppInstalledBubble() const = 0;
100
101 // In the very least this should return a dummy WebContents (required
102 // by some calls even when no prompt or other UI is shown). A non-dummy
103 // WebContents is required if the prompt returned by CreateInstallPromt()
104 // contains a navigable link(s). Returned WebContents should correspond
105 // to |profile| passed into the constructor.
106 virtual content::WebContents* GetWebContents() const = 0;
107
108 // Should return an installation prompt with desired properties or NULL if
109 // no prompt should be shown.
dchengc963c7142016-04-08 03:55:22110 virtual std::unique_ptr<ExtensionInstallPrompt::Prompt> CreateInstallPrompt()
[email protected]d382baa2014-06-17 18:50:01111 const = 0;
[email protected]d44ec7b2013-03-15 04:34:34112
[email protected]9c7b3092014-06-21 01:19:03113 // Will be called after the extension's manifest has been successfully parsed.
114 // Subclasses can perform asynchronous checks at this point and call
115 // ProceedWithInstallPrompt() to proceed with the install or otherwise call
116 // CompleteInstall() with an error code. The default implementation calls
117 // ProceedWithInstallPrompt().
118 virtual void OnManifestParsed();
[email protected]ce35418b2013-11-25 01:22:33119
[email protected]f8b23b42013-06-27 20:12:14120 // Returns an install UI to be shown. By default, this returns an install UI
121 // that is a transient child of the host window for GetWebContents().
dchengc963c7142016-04-08 03:55:22122 virtual std::unique_ptr<ExtensionInstallPrompt> CreateInstallUI();
[email protected]f8b23b42013-06-27 20:12:14123
[email protected]ce35418b2013-11-25 01:22:33124 // Create an approval to pass installation parameters to the CrxInstaller.
dchengc963c7142016-04-08 03:55:22125 virtual std::unique_ptr<WebstoreInstaller::Approval> CreateApproval() const;
[email protected]ce35418b2013-11-25 01:22:33126
rdevlin.cronin41593052016-01-08 01:40:12127 // Called once the install prompt has finished.
128 virtual void OnInstallPromptDone(ExtensionInstallPrompt::Result result);
rdevlin.cronin5f6b69d2014-09-20 01:23:35129
[email protected]d44ec7b2013-03-15 04:34:34130 // Accessors to be used by subclasses.
[email protected]dcde34b32013-07-31 02:28:45131 bool show_user_count() const { return show_user_count_; }
[email protected]d44ec7b2013-03-15 04:34:34132 const std::string& localized_user_count() const {
133 return localized_user_count_;
134 }
135 double average_rating() const { return average_rating_; }
136 int rating_count() const { return rating_count_; }
[email protected]084e35482013-09-25 02:46:19137 void set_install_source(WebstoreInstaller::InstallSource source) {
138 install_source_ = source;
139 }
140 WebstoreInstaller::InstallSource install_source() const {
141 return install_source_;
142 }
[email protected]1a93d8d2013-10-27 23:09:20143 Profile* profile() const { return profile_; }
144 const std::string& id() const { return id_; }
[email protected]cb1078de2013-12-23 20:04:22145 const base::DictionaryValue* manifest() const { return manifest_.get(); }
[email protected]9c7b3092014-06-21 01:19:03146 const Extension* localized_extension_for_display() const {
147 return localized_extension_for_display_.get();
148 }
[email protected]d44ec7b2013-03-15 04:34:34149
[email protected]8915f342011-08-29 22:14:37150 private:
[email protected]734bcec2012-10-08 20:29:05151 friend class base::RefCountedThreadSafe<WebstoreStandaloneInstaller>;
[email protected]8915f342011-08-29 22:14:37152
[email protected]d44ec7b2013-03-15 04:34:34153 // Several delegate/client interface implementations follow. The normal flow
[email protected]8915f342011-08-29 22:14:37154 // (for successful installs) is:
155 //
156 // 1. BeginInstall: starts the fetch of data from the webstore
157 // 2. OnURLFetchComplete: starts the parsing of data from the webstore
158 // 3. OnWebstoreResponseParseSuccess: starts the parsing of the manifest and
159 // fetching of icon data.
160 // 4. OnWebstoreParseSuccess: shows the install UI
161 // 5. InstallUIProceed: initiates the .crx download/install
162 //
163 // All flows (whether successful or not) end up in CompleteInstall, which
164 // informs our delegate of success/failure.
165
[email protected]7b3941052013-02-13 01:21:59166 // WebstoreDataFetcherDelegate interface implementation.
dchengae36a4a2014-10-21 12:36:36167 void OnWebstoreRequestFailure() override;
[email protected]d44ec7b2013-03-15 04:34:34168
dchengae36a4a2014-10-21 12:36:36169 void OnWebstoreResponseParseSuccess(
dchengc963c7142016-04-08 03:55:22170 std::unique_ptr<base::DictionaryValue> webstore_data) override;
[email protected]d44ec7b2013-03-15 04:34:34171
dchengae36a4a2014-10-21 12:36:36172 void OnWebstoreResponseParseFailure(const std::string& error) override;
[email protected]8915f342011-08-29 22:14:37173
174 // WebstoreInstallHelper::Delegate interface implementation.
Istiaque Ahmed27a63802018-11-07 20:34:38175 void OnWebstoreParseSuccess(
176 const std::string& id,
177 const SkBitmap& icon,
178 std::unique_ptr<base::DictionaryValue> parsed_manifest) override;
dchengae36a4a2014-10-21 12:36:36179 void OnWebstoreParseFailure(const std::string& id,
180 InstallHelperResultCode result_code,
181 const std::string& error_message) override;
[email protected]8915f342011-08-29 22:14:37182
[email protected]cb08ba22011-10-19 21:41:40183 // WebstoreInstaller::Delegate interface implementation.
dchengae36a4a2014-10-21 12:36:36184 void OnExtensionInstallSuccess(const std::string& id) override;
185 void OnExtensionInstallFailure(
[email protected]bcd1eaf72012-10-03 05:42:29186 const std::string& id,
187 const std::string& error,
mostynba15bee12014-10-04 00:40:32188 WebstoreInstaller::FailureReason reason) override;
[email protected]cb08ba22011-10-19 21:41:40189
[email protected]f8b23b42013-06-27 20:12:14190 void ShowInstallUI();
[email protected]e263a6b62014-06-05 23:19:49191 void OnWebStoreDataFetcherDone();
[email protected]bdb74a22012-01-25 20:33:33192
[email protected]c1b2d042013-02-23 00:31:04193 // Input configuration.
[email protected]8915f342011-08-29 22:14:37194 std::string id_;
[email protected]d2a639e2012-09-17 07:41:21195 Callback callback_;
[email protected]d44ec7b2013-03-15 04:34:34196 Profile* profile_;
[email protected]084e35482013-09-25 02:46:19197 WebstoreInstaller::InstallSource install_source_;
[email protected]c1b2d042013-02-23 00:31:04198
[email protected]d44ec7b2013-03-15 04:34:34199 // Installation dialog and its underlying prompt.
dchengc963c7142016-04-08 03:55:22200 std::unique_ptr<ExtensionInstallPrompt> install_ui_;
201 std::unique_ptr<ExtensionInstallPrompt::Prompt> install_prompt_;
[email protected]8915f342011-08-29 22:14:37202
203 // For fetching webstore JSON data.
dchengc963c7142016-04-08 03:55:22204 std::unique_ptr<WebstoreDataFetcher> webstore_data_fetcher_;
[email protected]8915f342011-08-29 22:14:37205
206 // Extracted from the webstore JSON data response.
207 std::string localized_name_;
[email protected]5fdbd6f2011-09-01 17:33:04208 std::string localized_description_;
[email protected]dcde34b32013-07-31 02:28:45209 bool show_user_count_;
[email protected]5fdbd6f2011-09-01 17:33:04210 std::string localized_user_count_;
211 double average_rating_;
212 int rating_count_;
dchengc963c7142016-04-08 03:55:22213 std::unique_ptr<base::DictionaryValue> webstore_data_;
214 std::unique_ptr<base::DictionaryValue> manifest_;
[email protected]8915f342011-08-29 22:14:37215 SkBitmap icon_;
216
[email protected]773272b2014-07-18 05:48:35217 // Active install registered with the InstallTracker.
dchengc963c7142016-04-08 03:55:22218 std::unique_ptr<ScopedActiveInstall> scoped_active_install_;
[email protected]773272b2014-07-18 05:48:35219
[email protected]f8b23b42013-06-27 20:12:14220 // Created by ShowInstallUI() when a prompt is shown (if
[email protected]d44ec7b2013-03-15 04:34:34221 // the implementor returns a non-NULL in CreateInstallPrompt()).
222 scoped_refptr<Extension> localized_extension_for_display_;
223
[email protected]734bcec2012-10-08 20:29:05224 DISALLOW_IMPLICIT_CONSTRUCTORS(WebstoreStandaloneInstaller);
[email protected]8915f342011-08-29 22:14:37225};
226
[email protected]3d61a7f2012-07-12 19:11:25227} // namespace extensions
228
[email protected]734bcec2012-10-08 20:29:05229#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_STANDALONE_INSTALLER_H_