blob: 56458e2ac1a5bc185346b632e3bf86696d1d2ccd [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]669b2372013-10-17 15:04:5818#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]ed0757ec2012-08-02 17:23:2619#include "content/public/browser/browser_thread.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]d59d40c2012-08-08 18:18:3723#include "net/base/net_errors.h"
[email protected]7b4eea602013-02-08 06:19:4724#include "ui/gfx/image/image_skia.h"
[email protected]a6483d22013-07-03 22:11:0025#include "url/gurl.h"
[email protected]655b2b1a2011-10-13 17:13:0626
[email protected]655b2b1a2011-10-13 17:13:0627class Profile;
28
[email protected]a3ef4832013-02-02 05:12:3329namespace base {
30class FilePath;
31}
32
[email protected]cdcb1dee2012-01-04 00:46:2033namespace content {
34class NavigationController;
35}
36
[email protected]3d61a7f2012-07-12 19:11:2537namespace extensions {
38
[email protected]669b2372013-10-17 15:04:5839class Extension;
[email protected]21c01042013-03-10 23:41:1440class Manifest;
41
[email protected]655b2b1a2011-10-13 17:13:0642// Downloads and installs extensions from the web store.
[email protected]ed0757ec2012-08-02 17:23:2643class WebstoreInstaller :public content::NotificationObserver,
44 public content::DownloadItem::Observer,
45 public base::RefCountedThreadSafe<
46 WebstoreInstaller, content::BrowserThread::DeleteOnUIThread> {
[email protected]655b2b1a2011-10-13 17:13:0647 public:
[email protected]084e35482013-09-25 02:46:1948 enum InstallSource {
[email protected]99dfecd2011-10-18 01:11:5049 // Inline installs trigger slightly different behavior (install source
50 // is different, download referrers are the item's page in the gallery).
[email protected]084e35482013-09-25 02:46:1951 INSTALL_SOURCE_INLINE,
52 INSTALL_SOURCE_APP_LAUNCHER,
53 INSTALL_SOURCE_OTHER
[email protected]655b2b1a2011-10-13 17:13:0654 };
55
[email protected]bcd1eaf72012-10-03 05:42:2956 enum FailureReason {
57 FAILURE_REASON_CANCELLED,
[email protected]669b2372013-10-17 15:04:5858 FAILURE_REASON_DEPENDENCY_NOT_FOUND,
59 FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE,
[email protected]bcd1eaf72012-10-03 05:42:2960 FAILURE_REASON_OTHER
61 };
62
[email protected]669b2372013-10-17 15:04:5863 enum ManifestCheckLevel {
64 // Do not check for any manifest equality.
65 MANIFEST_CHECK_LEVEL_NONE,
66
67 // Only check that the expected and actual permissions have the same
68 // effective permissions.
69 MANIFEST_CHECK_LEVEL_LOOSE,
70
71 // All data in the expected and actual manifests must match.
72 MANIFEST_CHECK_LEVEL_STRICT,
73 };
74
[email protected]655b2b1a2011-10-13 17:13:0675 class Delegate {
76 public:
[email protected]bcd1eaf72012-10-03 05:42:2977 virtual void OnExtensionDownloadStarted(const std::string& id,
78 content::DownloadItem* item);
79 virtual void OnExtensionDownloadProgress(const std::string& id,
80 content::DownloadItem* item);
[email protected]655b2b1a2011-10-13 17:13:0681 virtual void OnExtensionInstallSuccess(const std::string& id) = 0;
82 virtual void OnExtensionInstallFailure(const std::string& id,
[email protected]bcd1eaf72012-10-03 05:42:2983 const std::string& error,
84 FailureReason reason) = 0;
[email protected]512d03f2012-06-26 01:06:0685
86 protected:
87 virtual ~Delegate() {}
[email protected]655b2b1a2011-10-13 17:13:0688 };
89
[email protected]89019d62012-05-17 18:47:0990 // Contains information about what parts of the extension install process can
91 // be skipped or modified. If one of these is present, it means that a CRX
92 // download was initiated by WebstoreInstaller. The Approval instance should
93 // be checked further for additional details.
[email protected]d59d40c2012-08-08 18:18:3794 struct Approval : public base::SupportsUserData::Data {
[email protected]89019d62012-05-17 18:47:0995 static scoped_ptr<Approval> CreateWithInstallPrompt(Profile* profile);
[email protected]85290822013-08-23 20:27:3896
[email protected]669b2372013-10-17 15:04:5897 // Creates an Approval for installing a shared module.
98 static scoped_ptr<Approval> CreateForSharedModule(Profile* profile);
99
[email protected]85290822013-08-23 20:27:38100 // Creates an Approval that will skip putting up an install confirmation
101 // prompt if the actual manifest from the extension to be installed matches
102 // |parsed_manifest|. The |strict_manifest_check| controls whether we want
103 // to require an exact manifest match, or are willing to tolerate a looser
104 // check just that the effective permissions are the same.
[email protected]89019d62012-05-17 18:47:09105 static scoped_ptr<Approval> CreateWithNoInstallPrompt(
106 Profile* profile,
107 const std::string& extension_id,
[email protected]85290822013-08-23 20:27:38108 scoped_ptr<base::DictionaryValue> parsed_manifest,
109 bool strict_manifest_check);
[email protected]89019d62012-05-17 18:47:09110
[email protected]21a5ad62012-04-03 04:48:45111 virtual ~Approval();
112
113 // The extension id that was approved for installation.
114 std::string extension_id;
115
116 // The profile the extension should be installed into.
117 Profile* profile;
118
119 // The expected manifest, before localization.
[email protected]21c01042013-03-10 23:41:14120 scoped_ptr<Manifest> manifest;
[email protected]21a5ad62012-04-03 04:48:45121
122 // Whether to use a bubble notification when an app is installed, instead of
123 // the default behavior of transitioning to the new tab page.
124 bool use_app_installed_bubble;
125
126 // Whether to skip the post install UI like the extension installed bubble.
127 bool skip_post_install_ui;
[email protected]89019d62012-05-17 18:47:09128
129 // Whether to skip the install dialog once the extension has been downloaded
130 // and unpacked. One reason this can be true is that in the normal webstore
131 // installation, the dialog is shown earlier, before any download is done,
132 // so there's no need to show it again.
133 bool skip_install_dialog;
134
[email protected]fc652c02013-02-04 05:16:23135 // Whether we should enable the launcher before installing the app.
136 bool enable_launcher;
137
[email protected]669b2372013-10-17 15:04:58138 // Manifest check level for checking actual manifest against expected
139 // manifest.
140 ManifestCheckLevel manifest_check_level;
[email protected]85290822013-08-23 20:27:38141
[email protected]af6efb22012-10-12 02:23:05142 // Used to show the install dialog.
143 ExtensionInstallPrompt::ShowDialogCallback show_dialog_callback;
144
[email protected]7b4eea602013-02-08 06:19:47145 // The icon to use to display the extension while it is installing.
146 gfx::ImageSkia installing_icon;
147
[email protected]669b2372013-10-17 15:04:58148 // A dummy extension created from |manifest|;
149 scoped_refptr<Extension> dummy_extension;
150
151 // Required minimum version.
152 scoped_ptr<Version> minimum_version;
153
[email protected]89019d62012-05-17 18:47:09154 private:
155 Approval();
[email protected]21a5ad62012-04-03 04:48:45156 };
157
158 // Gets the Approval associated with the |download|, or NULL if there's none.
159 // Note that the Approval is owned by |download|.
160 static const Approval* GetAssociatedApproval(
161 const content::DownloadItem& download);
162
[email protected]98e4e522011-10-25 13:00:16163 // Creates a WebstoreInstaller for downloading and installing the extension
164 // with the given |id| from the Chrome Web Store. If |delegate| is not NULL,
165 // it will be notified when the install succeeds or fails. The installer will
166 // use the specified |controller| to download the extension. Only one
[email protected]21a5ad62012-04-03 04:48:45167 // WebstoreInstaller can use a specific controller at any given time. This
168 // also associates the |approval| with this install.
[email protected]98e4e522011-10-25 13:00:16169 // Note: the delegate should stay alive until being called back.
170 WebstoreInstaller(Profile* profile,
171 Delegate* delegate,
[email protected]cdcb1dee2012-01-04 00:46:20172 content::NavigationController* controller,
[email protected]98e4e522011-10-25 13:00:16173 const std::string& id,
[email protected]21a5ad62012-04-03 04:48:45174 scoped_ptr<Approval> approval,
[email protected]084e35482013-09-25 02:46:19175 InstallSource source);
[email protected]655b2b1a2011-10-13 17:13:06176
[email protected]98e4e522011-10-25 13:00:16177 // Starts downloading and installing the extension.
178 void Start();
[email protected]655b2b1a2011-10-13 17:13:06179
[email protected]6c2381d2011-10-19 02:52:53180 // content::NotificationObserver
[email protected]655b2b1a2011-10-13 17:13:06181 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53182 const content::NotificationSource& source,
183 const content::NotificationDetails& details) OVERRIDE;
[email protected]655b2b1a2011-10-13 17:13:06184
[email protected]e074e322012-10-30 01:12:09185 // Removes the reference to the delegate passed in the constructor. Used when
186 // the delegate object must be deleted before this object.
187 void InvalidateDelegate();
188
[email protected]9c265d02011-12-29 22:13:43189 // Instead of using the default download directory, use |directory| instead.
190 // This does *not* transfer ownership of |directory|.
[email protected]a3ef4832013-02-02 05:12:33191 static void SetDownloadDirectoryForTests(base::FilePath* directory);
[email protected]9c265d02011-12-29 22:13:43192
[email protected]655b2b1a2011-10-13 17:13:06193 private:
[email protected]902a8a012013-05-04 05:04:16194 FRIEND_TEST_ALL_PREFIXES(WebstoreInstallerTest, PlatformParams);
[email protected]ed0757ec2012-08-02 17:23:26195 friend struct content::BrowserThread::DeleteOnThread<
196 content::BrowserThread::UI>;
197 friend class base::DeleteHelper<WebstoreInstaller>;
[email protected]5f2a4752012-04-27 22:18:58198 virtual ~WebstoreInstaller();
199
[email protected]902a8a012013-05-04 05:04:16200 // Helper to get install URL.
201 static GURL GetWebstoreInstallURL(const std::string& extension_id,
[email protected]669b2372013-10-17 15:04:58202 InstallSource source);
[email protected]902a8a012013-05-04 05:04:16203
[email protected]8adbe3112012-03-27 05:42:22204 // DownloadManager::DownloadUrl callback.
[email protected]3d43eef2012-10-09 23:17:56205 void OnDownloadStarted(content::DownloadItem* item, net::Error error);
[email protected]8adbe3112012-03-27 05:42:22206
207 // DownloadItem::Observer implementation:
208 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
[email protected]7e834f02012-08-09 20:38:56209 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE;
[email protected]8adbe3112012-03-27 05:42:22210
[email protected]669b2372013-10-17 15:04:58211 // Downloads next pending module in |pending_modules_|.
212 void DownloadNextPendingModule();
213
214 // Downloads and installs a single Crx with the given |extension_id|.
215 // This function is used for both the extension Crx and dependences.
216 void DownloadCrx(const std::string& extension_id, InstallSource source);
217
[email protected]f66a50a2011-11-02 23:53:46218 // Starts downloading the extension to |file_path|.
[email protected]a3ef4832013-02-02 05:12:33219 void StartDownload(const base::FilePath& file_path);
[email protected]f66a50a2011-11-02 23:53:46220
[email protected]655b2b1a2011-10-13 17:13:06221 // Reports an install |error| to the delegate for the given extension if this
222 // managed its installation. This also removes the associated PendingInstall.
[email protected]bcd1eaf72012-10-03 05:42:29223 void ReportFailure(const std::string& error, FailureReason reason);
[email protected]655b2b1a2011-10-13 17:13:06224
225 // Reports a successful install to the delegate for the given extension if
226 // this managed its installation. This also removes the associated
227 // PendingInstall.
[email protected]98e4e522011-10-25 13:00:16228 void ReportSuccess();
[email protected]655b2b1a2011-10-13 17:13:06229
[email protected]6c2381d2011-10-19 02:52:53230 content::NotificationRegistrar registrar_;
[email protected]655b2b1a2011-10-13 17:13:06231 Profile* profile_;
[email protected]98e4e522011-10-25 13:00:16232 Delegate* delegate_;
[email protected]cdcb1dee2012-01-04 00:46:20233 content::NavigationController* controller_;
[email protected]98e4e522011-10-25 13:00:16234 std::string id_;
[email protected]669b2372013-10-17 15:04:58235 InstallSource install_source_;
[email protected]8adbe3112012-03-27 05:42:22236 // The DownloadItem is owned by the DownloadManager and is valid from when
[email protected]81b80bc2012-08-31 18:27:44237 // OnDownloadStarted is called (with no error) until OnDownloadDestroyed().
[email protected]8adbe3112012-03-27 05:42:22238 content::DownloadItem* download_item_;
[email protected]21a5ad62012-04-03 04:48:45239 scoped_ptr<Approval> approval_;
[email protected]98e4e522011-10-25 13:00:16240 GURL download_url_;
[email protected]669b2372013-10-17 15:04:58241
242 // Pending modules.
243 std::list<SharedModuleInfo::ImportInfo> pending_modules_;
244 // Total extension modules we need download and install (the main module and
245 // depedences).
246 int total_modules_;
247 bool download_started_;
[email protected]655b2b1a2011-10-13 17:13:06248};
249
[email protected]3d61a7f2012-07-12 19:11:25250} // namespace extensions
251
[email protected]655b2b1a2011-10-13 17:13:06252#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_