blob: a8b6ce43edbe9cca24fc20e5ccc6c15e65694580 [file] [log] [blame]
[email protected]29679dea2012-03-10 03:20:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]655b2b1a2011-10-13 17:13:062// 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_WEBSTORE_INSTALLER_H_
6#define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_
[email protected]655b2b1a2011-10-13 17:13:067
[email protected]669b2372013-10-17 15:04:588#include <list>
[email protected]655b2b1a2011-10-13 17:13:069#include <string>
[email protected]655b2b1a2011-10-13 17:13:0610
11#include "base/compiler_specific.h"
[email protected]98e4e522011-10-25 13:00:1612#include "base/memory/ref_counted.h"
[email protected]21a5ad62012-04-03 04:48:4513#include "base/memory/scoped_ptr.h"
[email protected]d59d40c2012-08-08 18:18:3714#include "base/supports_user_data.h"
[email protected]21a5ad62012-04-03 04:48:4515#include "base/values.h"
[email protected]669b2372013-10-17 15:04:5816#include "base/version.h"
[email protected]af6efb22012-10-12 02:23:0517#include "chrome/browser/extensions/extension_install_prompt.h"
[email protected]ed0757ec2012-08-02 17:23:2618#include "content/public/browser/browser_thread.h"
[email protected]7f3918882014-01-14 06:14:5619#include "content/public/browser/download_interrupt_reasons.h"
[email protected]8adbe3112012-03-27 05:42:2220#include "content/public/browser/download_item.h"
[email protected]6c2381d2011-10-19 02:52:5321#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
[email protected]2acf3a52014-02-13 00:26:0023#include "content/public/browser/web_contents_observer.h"
[email protected]301116c62013-11-26 10:37:4524#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]7b4eea602013-02-08 06:19:4725#include "ui/gfx/image/image_skia.h"
[email protected]a6483d22013-07-03 22:11:0026#include "url/gurl.h"
[email protected]655b2b1a2011-10-13 17:13:0627
[email protected]655b2b1a2011-10-13 17:13:0628class Profile;
29
[email protected]a3ef4832013-02-02 05:12:3330namespace base {
31class FilePath;
32}
33
[email protected]cdcb1dee2012-01-04 00:46:2034namespace content {
[email protected]2acf3a52014-02-13 00:26:0035class WebContents;
[email protected]cdcb1dee2012-01-04 00:46:2036}
37
[email protected]3d61a7f2012-07-12 19:11:2538namespace extensions {
39
[email protected]669b2372013-10-17 15:04:5840class Extension;
[email protected]21c01042013-03-10 23:41:1441class Manifest;
42
[email protected]655b2b1a2011-10-13 17:13:0643// Downloads and installs extensions from the web store.
[email protected]2acf3a52014-02-13 00:26:0044class WebstoreInstaller : public content::NotificationObserver,
45 public content::DownloadItem::Observer,
46 public content::WebContentsObserver,
47 public base::RefCountedThreadSafe<
[email protected]ed0757ec2012-08-02 17:23:2648 WebstoreInstaller, content::BrowserThread::DeleteOnUIThread> {
[email protected]655b2b1a2011-10-13 17:13:0649 public:
[email protected]084e35482013-09-25 02:46:1950 enum InstallSource {
[email protected]99dfecd2011-10-18 01:11:5051 // Inline installs trigger slightly different behavior (install source
52 // is different, download referrers are the item's page in the gallery).
[email protected]084e35482013-09-25 02:46:1953 INSTALL_SOURCE_INLINE,
54 INSTALL_SOURCE_APP_LAUNCHER,
55 INSTALL_SOURCE_OTHER
[email protected]655b2b1a2011-10-13 17:13:0656 };
57
[email protected]bcd1eaf72012-10-03 05:42:2958 enum FailureReason {
59 FAILURE_REASON_CANCELLED,
[email protected]669b2372013-10-17 15:04:5860 FAILURE_REASON_DEPENDENCY_NOT_FOUND,
61 FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE,
[email protected]bcd1eaf72012-10-03 05:42:2962 FAILURE_REASON_OTHER
63 };
64
[email protected]669b2372013-10-17 15:04:5865 enum ManifestCheckLevel {
66 // Do not check for any manifest equality.
67 MANIFEST_CHECK_LEVEL_NONE,
68
69 // Only check that the expected and actual permissions have the same
70 // effective permissions.
71 MANIFEST_CHECK_LEVEL_LOOSE,
72
73 // All data in the expected and actual manifests must match.
74 MANIFEST_CHECK_LEVEL_STRICT,
75 };
76
[email protected]655b2b1a2011-10-13 17:13:0677 class Delegate {
78 public:
[email protected]bcd1eaf72012-10-03 05:42:2979 virtual void OnExtensionDownloadStarted(const std::string& id,
80 content::DownloadItem* item);
81 virtual void OnExtensionDownloadProgress(const std::string& id,
82 content::DownloadItem* item);
[email protected]655b2b1a2011-10-13 17:13:0683 virtual void OnExtensionInstallSuccess(const std::string& id) = 0;
84 virtual void OnExtensionInstallFailure(const std::string& id,
[email protected]bcd1eaf72012-10-03 05:42:2985 const std::string& error,
86 FailureReason reason) = 0;
[email protected]512d03f2012-06-26 01:06:0687
88 protected:
89 virtual ~Delegate() {}
[email protected]655b2b1a2011-10-13 17:13:0690 };
91
[email protected]89019d62012-05-17 18:47:0992 // Contains information about what parts of the extension install process can
93 // be skipped or modified. If one of these is present, it means that a CRX
94 // download was initiated by WebstoreInstaller. The Approval instance should
95 // be checked further for additional details.
[email protected]d59d40c2012-08-08 18:18:3796 struct Approval : public base::SupportsUserData::Data {
[email protected]89019d62012-05-17 18:47:0997 static scoped_ptr<Approval> CreateWithInstallPrompt(Profile* profile);
[email protected]85290822013-08-23 20:27:3898
[email protected]669b2372013-10-17 15:04:5899 // Creates an Approval for installing a shared module.
100 static scoped_ptr<Approval> CreateForSharedModule(Profile* profile);
101
[email protected]85290822013-08-23 20:27:38102 // Creates an Approval that will skip putting up an install confirmation
103 // prompt if the actual manifest from the extension to be installed matches
104 // |parsed_manifest|. The |strict_manifest_check| controls whether we want
105 // to require an exact manifest match, or are willing to tolerate a looser
106 // check just that the effective permissions are the same.
[email protected]89019d62012-05-17 18:47:09107 static scoped_ptr<Approval> CreateWithNoInstallPrompt(
108 Profile* profile,
109 const std::string& extension_id,
[email protected]85290822013-08-23 20:27:38110 scoped_ptr<base::DictionaryValue> parsed_manifest,
111 bool strict_manifest_check);
[email protected]89019d62012-05-17 18:47:09112
[email protected]21a5ad62012-04-03 04:48:45113 virtual ~Approval();
114
115 // The extension id that was approved for installation.
116 std::string extension_id;
117
118 // The profile the extension should be installed into.
119 Profile* profile;
120
121 // The expected manifest, before localization.
[email protected]21c01042013-03-10 23:41:14122 scoped_ptr<Manifest> manifest;
[email protected]21a5ad62012-04-03 04:48:45123
124 // Whether to use a bubble notification when an app is installed, instead of
125 // the default behavior of transitioning to the new tab page.
126 bool use_app_installed_bubble;
127
128 // Whether to skip the post install UI like the extension installed bubble.
129 bool skip_post_install_ui;
[email protected]89019d62012-05-17 18:47:09130
131 // Whether to skip the install dialog once the extension has been downloaded
132 // and unpacked. One reason this can be true is that in the normal webstore
133 // installation, the dialog is shown earlier, before any download is done,
134 // so there's no need to show it again.
135 bool skip_install_dialog;
136
[email protected]fc652c02013-02-04 05:16:23137 // Whether we should enable the launcher before installing the app.
138 bool enable_launcher;
139
[email protected]669b2372013-10-17 15:04:58140 // Manifest check level for checking actual manifest against expected
141 // manifest.
142 ManifestCheckLevel manifest_check_level;
[email protected]85290822013-08-23 20:27:38143
[email protected]af6efb22012-10-12 02:23:05144 // Used to show the install dialog.
145 ExtensionInstallPrompt::ShowDialogCallback show_dialog_callback;
146
[email protected]7b4eea602013-02-08 06:19:47147 // The icon to use to display the extension while it is installing.
148 gfx::ImageSkia installing_icon;
149
[email protected]669b2372013-10-17 15:04:58150 // A dummy extension created from |manifest|;
151 scoped_refptr<Extension> dummy_extension;
152
153 // Required minimum version.
[email protected]c5e4a2222014-01-03 16:06:13154 scoped_ptr<Version> minimum_version;
[email protected]669b2372013-10-17 15:04:58155
[email protected]ce35418b2013-11-25 01:22:33156 // Ephemeral apps (experimental) are not permanently installed in Chrome.
157 bool is_ephemeral;
158
[email protected]89019d62012-05-17 18:47:09159 private:
160 Approval();
[email protected]21a5ad62012-04-03 04:48:45161 };
162
163 // Gets the Approval associated with the |download|, or NULL if there's none.
164 // Note that the Approval is owned by |download|.
165 static const Approval* GetAssociatedApproval(
166 const content::DownloadItem& download);
167
[email protected]98e4e522011-10-25 13:00:16168 // Creates a WebstoreInstaller for downloading and installing the extension
169 // with the given |id| from the Chrome Web Store. If |delegate| is not NULL,
170 // it will be notified when the install succeeds or fails. The installer will
171 // use the specified |controller| to download the extension. Only one
[email protected]21a5ad62012-04-03 04:48:45172 // WebstoreInstaller can use a specific controller at any given time. This
173 // also associates the |approval| with this install.
[email protected]98e4e522011-10-25 13:00:16174 // Note: the delegate should stay alive until being called back.
175 WebstoreInstaller(Profile* profile,
176 Delegate* delegate,
[email protected]2acf3a52014-02-13 00:26:00177 content::WebContents* web_contents,
[email protected]98e4e522011-10-25 13:00:16178 const std::string& id,
[email protected]21a5ad62012-04-03 04:48:45179 scoped_ptr<Approval> approval,
[email protected]084e35482013-09-25 02:46:19180 InstallSource source);
[email protected]655b2b1a2011-10-13 17:13:06181
[email protected]98e4e522011-10-25 13:00:16182 // Starts downloading and installing the extension.
183 void Start();
[email protected]655b2b1a2011-10-13 17:13:06184
[email protected]6c2381d2011-10-19 02:52:53185 // content::NotificationObserver
[email protected]655b2b1a2011-10-13 17:13:06186 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53187 const content::NotificationSource& source,
188 const content::NotificationDetails& details) OVERRIDE;
[email protected]655b2b1a2011-10-13 17:13:06189
[email protected]e074e322012-10-30 01:12:09190 // Removes the reference to the delegate passed in the constructor. Used when
191 // the delegate object must be deleted before this object.
192 void InvalidateDelegate();
193
[email protected]9c265d02011-12-29 22:13:43194 // Instead of using the default download directory, use |directory| instead.
195 // This does *not* transfer ownership of |directory|.
[email protected]a3ef4832013-02-02 05:12:33196 static void SetDownloadDirectoryForTests(base::FilePath* directory);
[email protected]9c265d02011-12-29 22:13:43197
[email protected]655b2b1a2011-10-13 17:13:06198 private:
[email protected]902a8a012013-05-04 05:04:16199 FRIEND_TEST_ALL_PREFIXES(WebstoreInstallerTest, PlatformParams);
[email protected]ed0757ec2012-08-02 17:23:26200 friend struct content::BrowserThread::DeleteOnThread<
201 content::BrowserThread::UI>;
202 friend class base::DeleteHelper<WebstoreInstaller>;
[email protected]5f2a4752012-04-27 22:18:58203 virtual ~WebstoreInstaller();
204
[email protected]902a8a012013-05-04 05:04:16205 // Helper to get install URL.
206 static GURL GetWebstoreInstallURL(const std::string& extension_id,
[email protected]669b2372013-10-17 15:04:58207 InstallSource source);
[email protected]902a8a012013-05-04 05:04:16208
[email protected]8adbe3112012-03-27 05:42:22209 // DownloadManager::DownloadUrl callback.
[email protected]7f3918882014-01-14 06:14:56210 void OnDownloadStarted(content::DownloadItem* item,
211 content::DownloadInterruptReason interrupt_reason);
[email protected]8adbe3112012-03-27 05:42:22212
213 // DownloadItem::Observer implementation:
214 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
[email protected]7e834f02012-08-09 20:38:56215 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE;
[email protected]8adbe3112012-03-27 05:42:22216
[email protected]669b2372013-10-17 15:04:58217 // Downloads next pending module in |pending_modules_|.
218 void DownloadNextPendingModule();
219
220 // Downloads and installs a single Crx with the given |extension_id|.
221 // This function is used for both the extension Crx and dependences.
222 void DownloadCrx(const std::string& extension_id, InstallSource source);
223
[email protected]f66a50a2011-11-02 23:53:46224 // Starts downloading the extension to |file_path|.
[email protected]a3ef4832013-02-02 05:12:33225 void StartDownload(const base::FilePath& file_path);
[email protected]f66a50a2011-11-02 23:53:46226
[email protected]655b2b1a2011-10-13 17:13:06227 // Reports an install |error| to the delegate for the given extension if this
228 // managed its installation. This also removes the associated PendingInstall.
[email protected]bcd1eaf72012-10-03 05:42:29229 void ReportFailure(const std::string& error, FailureReason reason);
[email protected]655b2b1a2011-10-13 17:13:06230
231 // Reports a successful install to the delegate for the given extension if
232 // this managed its installation. This also removes the associated
233 // PendingInstall.
[email protected]98e4e522011-10-25 13:00:16234 void ReportSuccess();
[email protected]655b2b1a2011-10-13 17:13:06235
[email protected]0a2a7242013-11-20 20:49:24236 // Records stats regarding an interrupted webstore download item.
237 void RecordInterrupt(const content::DownloadItem* download) const;
238
[email protected]6c2381d2011-10-19 02:52:53239 content::NotificationRegistrar registrar_;
[email protected]655b2b1a2011-10-13 17:13:06240 Profile* profile_;
[email protected]98e4e522011-10-25 13:00:16241 Delegate* delegate_;
[email protected]98e4e522011-10-25 13:00:16242 std::string id_;
[email protected]669b2372013-10-17 15:04:58243 InstallSource install_source_;
[email protected]8adbe3112012-03-27 05:42:22244 // The DownloadItem is owned by the DownloadManager and is valid from when
[email protected]81b80bc2012-08-31 18:27:44245 // OnDownloadStarted is called (with no error) until OnDownloadDestroyed().
[email protected]8adbe3112012-03-27 05:42:22246 content::DownloadItem* download_item_;
[email protected]21a5ad62012-04-03 04:48:45247 scoped_ptr<Approval> approval_;
[email protected]98e4e522011-10-25 13:00:16248 GURL download_url_;
[email protected]669b2372013-10-17 15:04:58249
250 // Pending modules.
251 std::list<SharedModuleInfo::ImportInfo> pending_modules_;
252 // Total extension modules we need download and install (the main module and
253 // depedences).
254 int total_modules_;
255 bool download_started_;
[email protected]655b2b1a2011-10-13 17:13:06256};
257
[email protected]3d61a7f2012-07-12 19:11:25258} // namespace extensions
259
[email protected]655b2b1a2011-10-13 17:13:06260#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_