blob: bda4aa0636d03ca0418357b059eb832254abafb2 [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
8#include <string>
9#include <vector>
10
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]af6efb22012-10-12 02:23:0516#include "chrome/browser/extensions/extension_install_prompt.h"
[email protected]ed0757ec2012-08-02 17:23:2617#include "content/public/browser/browser_thread.h"
[email protected]8adbe3112012-03-27 05:42:2218#include "content/public/browser/download_item.h"
[email protected]6c2381d2011-10-19 02:52:5319#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]98e4e522011-10-25 13:00:1621#include "googleurl/src/gurl.h"
[email protected]d59d40c2012-08-08 18:18:3722#include "net/base/net_errors.h"
[email protected]655b2b1a2011-10-13 17:13:0623
[email protected]655b2b1a2011-10-13 17:13:0624class Profile;
25
[email protected]a3ef4832013-02-02 05:12:3326namespace base {
27class FilePath;
28}
29
[email protected]cdcb1dee2012-01-04 00:46:2030namespace content {
31class NavigationController;
32}
33
[email protected]3d61a7f2012-07-12 19:11:2534namespace extensions {
35
[email protected]655b2b1a2011-10-13 17:13:0636// Downloads and installs extensions from the web store.
[email protected]ed0757ec2012-08-02 17:23:2637class WebstoreInstaller :public content::NotificationObserver,
38 public content::DownloadItem::Observer,
39 public base::RefCountedThreadSafe<
40 WebstoreInstaller, content::BrowserThread::DeleteOnUIThread> {
[email protected]655b2b1a2011-10-13 17:13:0641 public:
42 enum Flag {
43 FLAG_NONE = 0,
44
[email protected]99dfecd2011-10-18 01:11:5045 // Inline installs trigger slightly different behavior (install source
46 // is different, download referrers are the item's page in the gallery).
47 FLAG_INLINE_INSTALL = 1 << 0
[email protected]655b2b1a2011-10-13 17:13:0648 };
49
[email protected]bcd1eaf72012-10-03 05:42:2950 enum FailureReason {
51 FAILURE_REASON_CANCELLED,
52 FAILURE_REASON_OTHER
53 };
54
[email protected]655b2b1a2011-10-13 17:13:0655 class Delegate {
56 public:
[email protected]bcd1eaf72012-10-03 05:42:2957 virtual void OnExtensionDownloadStarted(const std::string& id,
58 content::DownloadItem* item);
59 virtual void OnExtensionDownloadProgress(const std::string& id,
60 content::DownloadItem* item);
[email protected]655b2b1a2011-10-13 17:13:0661 virtual void OnExtensionInstallSuccess(const std::string& id) = 0;
62 virtual void OnExtensionInstallFailure(const std::string& id,
[email protected]bcd1eaf72012-10-03 05:42:2963 const std::string& error,
64 FailureReason reason) = 0;
[email protected]512d03f2012-06-26 01:06:0665
66 protected:
67 virtual ~Delegate() {}
[email protected]655b2b1a2011-10-13 17:13:0668 };
69
[email protected]89019d62012-05-17 18:47:0970 // Contains information about what parts of the extension install process can
71 // be skipped or modified. If one of these is present, it means that a CRX
72 // download was initiated by WebstoreInstaller. The Approval instance should
73 // be checked further for additional details.
[email protected]d59d40c2012-08-08 18:18:3774 struct Approval : public base::SupportsUserData::Data {
[email protected]89019d62012-05-17 18:47:0975 static scoped_ptr<Approval> CreateWithInstallPrompt(Profile* profile);
76 static scoped_ptr<Approval> CreateWithNoInstallPrompt(
77 Profile* profile,
78 const std::string& extension_id,
79 scoped_ptr<base::DictionaryValue> parsed_manifest);
80
[email protected]21a5ad62012-04-03 04:48:4581 virtual ~Approval();
82
83 // The extension id that was approved for installation.
84 std::string extension_id;
85
86 // The profile the extension should be installed into.
87 Profile* profile;
88
89 // The expected manifest, before localization.
90 scoped_ptr<base::DictionaryValue> parsed_manifest;
91
92 // Whether to use a bubble notification when an app is installed, instead of
93 // the default behavior of transitioning to the new tab page.
94 bool use_app_installed_bubble;
95
96 // Whether to skip the post install UI like the extension installed bubble.
97 bool skip_post_install_ui;
[email protected]89019d62012-05-17 18:47:0998
99 // Whether to skip the install dialog once the extension has been downloaded
100 // and unpacked. One reason this can be true is that in the normal webstore
101 // installation, the dialog is shown earlier, before any download is done,
102 // so there's no need to show it again.
103 bool skip_install_dialog;
104
[email protected]b70a2d92012-06-28 19:51:21105 // Whether we should record an oauth2 grant for the extensions.
106 bool record_oauth2_grant;
107
[email protected]af6efb22012-10-12 02:23:05108 // Used to show the install dialog.
109 ExtensionInstallPrompt::ShowDialogCallback show_dialog_callback;
110
[email protected]89019d62012-05-17 18:47:09111 private:
112 Approval();
[email protected]21a5ad62012-04-03 04:48:45113 };
114
115 // Gets the Approval associated with the |download|, or NULL if there's none.
116 // Note that the Approval is owned by |download|.
117 static const Approval* GetAssociatedApproval(
118 const content::DownloadItem& download);
119
[email protected]98e4e522011-10-25 13:00:16120 // Creates a WebstoreInstaller for downloading and installing the extension
121 // with the given |id| from the Chrome Web Store. If |delegate| is not NULL,
122 // it will be notified when the install succeeds or fails. The installer will
123 // use the specified |controller| to download the extension. Only one
[email protected]21a5ad62012-04-03 04:48:45124 // WebstoreInstaller can use a specific controller at any given time. This
125 // also associates the |approval| with this install.
[email protected]98e4e522011-10-25 13:00:16126 // Note: the delegate should stay alive until being called back.
127 WebstoreInstaller(Profile* profile,
128 Delegate* delegate,
[email protected]cdcb1dee2012-01-04 00:46:20129 content::NavigationController* controller,
[email protected]98e4e522011-10-25 13:00:16130 const std::string& id,
[email protected]21a5ad62012-04-03 04:48:45131 scoped_ptr<Approval> approval,
[email protected]98e4e522011-10-25 13:00:16132 int flags);
[email protected]655b2b1a2011-10-13 17:13:06133
[email protected]98e4e522011-10-25 13:00:16134 // Starts downloading and installing the extension.
135 void Start();
[email protected]655b2b1a2011-10-13 17:13:06136
[email protected]6c2381d2011-10-19 02:52:53137 // content::NotificationObserver
[email protected]655b2b1a2011-10-13 17:13:06138 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53139 const content::NotificationSource& source,
140 const content::NotificationDetails& details) OVERRIDE;
[email protected]655b2b1a2011-10-13 17:13:06141
[email protected]e074e322012-10-30 01:12:09142 // Removes the reference to the delegate passed in the constructor. Used when
143 // the delegate object must be deleted before this object.
144 void InvalidateDelegate();
145
[email protected]9c265d02011-12-29 22:13:43146 // Instead of using the default download directory, use |directory| instead.
147 // This does *not* transfer ownership of |directory|.
[email protected]a3ef4832013-02-02 05:12:33148 static void SetDownloadDirectoryForTests(base::FilePath* directory);
[email protected]9c265d02011-12-29 22:13:43149
[email protected]655b2b1a2011-10-13 17:13:06150 private:
[email protected]ed0757ec2012-08-02 17:23:26151 friend struct content::BrowserThread::DeleteOnThread<
152 content::BrowserThread::UI>;
153 friend class base::DeleteHelper<WebstoreInstaller>;
[email protected]5f2a4752012-04-27 22:18:58154 virtual ~WebstoreInstaller();
155
[email protected]8adbe3112012-03-27 05:42:22156 // DownloadManager::DownloadUrl callback.
[email protected]3d43eef2012-10-09 23:17:56157 void OnDownloadStarted(content::DownloadItem* item, net::Error error);
[email protected]8adbe3112012-03-27 05:42:22158
159 // DownloadItem::Observer implementation:
160 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
[email protected]7e834f02012-08-09 20:38:56161 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE;
[email protected]8adbe3112012-03-27 05:42:22162
[email protected]f66a50a2011-11-02 23:53:46163 // Starts downloading the extension to |file_path|.
[email protected]a3ef4832013-02-02 05:12:33164 void StartDownload(const base::FilePath& file_path);
[email protected]f66a50a2011-11-02 23:53:46165
[email protected]655b2b1a2011-10-13 17:13:06166 // Reports an install |error| to the delegate for the given extension if this
167 // managed its installation. This also removes the associated PendingInstall.
[email protected]bcd1eaf72012-10-03 05:42:29168 void ReportFailure(const std::string& error, FailureReason reason);
[email protected]655b2b1a2011-10-13 17:13:06169
170 // Reports a successful install to the delegate for the given extension if
171 // this managed its installation. This also removes the associated
172 // PendingInstall.
[email protected]98e4e522011-10-25 13:00:16173 void ReportSuccess();
[email protected]655b2b1a2011-10-13 17:13:06174
[email protected]6c2381d2011-10-19 02:52:53175 content::NotificationRegistrar registrar_;
[email protected]655b2b1a2011-10-13 17:13:06176 Profile* profile_;
[email protected]98e4e522011-10-25 13:00:16177 Delegate* delegate_;
[email protected]cdcb1dee2012-01-04 00:46:20178 content::NavigationController* controller_;
[email protected]98e4e522011-10-25 13:00:16179 std::string id_;
[email protected]8adbe3112012-03-27 05:42:22180 // The DownloadItem is owned by the DownloadManager and is valid from when
[email protected]81b80bc2012-08-31 18:27:44181 // OnDownloadStarted is called (with no error) until OnDownloadDestroyed().
[email protected]8adbe3112012-03-27 05:42:22182 content::DownloadItem* download_item_;
[email protected]21a5ad62012-04-03 04:48:45183 scoped_ptr<Approval> approval_;
[email protected]98e4e522011-10-25 13:00:16184 GURL download_url_;
[email protected]655b2b1a2011-10-13 17:13:06185};
186
[email protected]3d61a7f2012-07-12 19:11:25187} // namespace extensions
188
[email protected]655b2b1a2011-10-13 17:13:06189#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_