[email protected] | 29679dea | 2012-03-10 03:20:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 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_WEBSTORE_INSTALLER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/compiler_specific.h" |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 14 | #include "base/supports_user_data.h" |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_install_prompt.h" |
[email protected] | ed0757ec | 2012-08-02 17:23:26 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 18 | #include "content/public/browser/download_item.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 19 | #include "content/public/browser/notification_observer.h" |
| 20 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 21 | #include "googleurl/src/gurl.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 22 | #include "net/base/net_errors.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 23 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 24 | class Profile; |
| 25 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame^] | 26 | namespace base { |
| 27 | class FilePath; |
| 28 | } |
| 29 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 30 | namespace content { |
| 31 | class NavigationController; |
| 32 | } |
| 33 | |
[email protected] | 3d61a7f | 2012-07-12 19:11:25 | [diff] [blame] | 34 | namespace extensions { |
| 35 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 36 | // Downloads and installs extensions from the web store. |
[email protected] | ed0757ec | 2012-08-02 17:23:26 | [diff] [blame] | 37 | class WebstoreInstaller :public content::NotificationObserver, |
| 38 | public content::DownloadItem::Observer, |
| 39 | public base::RefCountedThreadSafe< |
| 40 | WebstoreInstaller, content::BrowserThread::DeleteOnUIThread> { |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 41 | public: |
| 42 | enum Flag { |
| 43 | FLAG_NONE = 0, |
| 44 | |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 45 | // 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] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 48 | }; |
| 49 | |
[email protected] | bcd1eaf7 | 2012-10-03 05:42:29 | [diff] [blame] | 50 | enum FailureReason { |
| 51 | FAILURE_REASON_CANCELLED, |
| 52 | FAILURE_REASON_OTHER |
| 53 | }; |
| 54 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 55 | class Delegate { |
| 56 | public: |
[email protected] | bcd1eaf7 | 2012-10-03 05:42:29 | [diff] [blame] | 57 | 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] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 61 | virtual void OnExtensionInstallSuccess(const std::string& id) = 0; |
| 62 | virtual void OnExtensionInstallFailure(const std::string& id, |
[email protected] | bcd1eaf7 | 2012-10-03 05:42:29 | [diff] [blame] | 63 | const std::string& error, |
| 64 | FailureReason reason) = 0; |
[email protected] | 512d03f | 2012-06-26 01:06:06 | [diff] [blame] | 65 | |
| 66 | protected: |
| 67 | virtual ~Delegate() {} |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 68 | }; |
| 69 | |
[email protected] | 89019d6 | 2012-05-17 18:47:09 | [diff] [blame] | 70 | // 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] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 74 | struct Approval : public base::SupportsUserData::Data { |
[email protected] | 89019d6 | 2012-05-17 18:47:09 | [diff] [blame] | 75 | 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] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 81 | 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] | 89019d6 | 2012-05-17 18:47:09 | [diff] [blame] | 98 | |
| 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] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 105 | // Whether we should record an oauth2 grant for the extensions. |
| 106 | bool record_oauth2_grant; |
| 107 | |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 108 | // Used to show the install dialog. |
| 109 | ExtensionInstallPrompt::ShowDialogCallback show_dialog_callback; |
| 110 | |
[email protected] | 89019d6 | 2012-05-17 18:47:09 | [diff] [blame] | 111 | private: |
| 112 | Approval(); |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 113 | }; |
| 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] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 120 | // 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] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 124 | // WebstoreInstaller can use a specific controller at any given time. This |
| 125 | // also associates the |approval| with this install. |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 126 | // Note: the delegate should stay alive until being called back. |
| 127 | WebstoreInstaller(Profile* profile, |
| 128 | Delegate* delegate, |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 129 | content::NavigationController* controller, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 130 | const std::string& id, |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 131 | scoped_ptr<Approval> approval, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 132 | int flags); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 133 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 134 | // Starts downloading and installing the extension. |
| 135 | void Start(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 136 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 137 | // content::NotificationObserver |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 138 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 139 | const content::NotificationSource& source, |
| 140 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 141 | |
[email protected] | e074e32 | 2012-10-30 01:12:09 | [diff] [blame] | 142 | // 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] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 146 | // Instead of using the default download directory, use |directory| instead. |
| 147 | // This does *not* transfer ownership of |directory|. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame^] | 148 | static void SetDownloadDirectoryForTests(base::FilePath* directory); |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 149 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 150 | private: |
[email protected] | ed0757ec | 2012-08-02 17:23:26 | [diff] [blame] | 151 | friend struct content::BrowserThread::DeleteOnThread< |
| 152 | content::BrowserThread::UI>; |
| 153 | friend class base::DeleteHelper<WebstoreInstaller>; |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 154 | virtual ~WebstoreInstaller(); |
| 155 | |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 156 | // DownloadManager::DownloadUrl callback. |
[email protected] | 3d43eef | 2012-10-09 23:17:56 | [diff] [blame] | 157 | void OnDownloadStarted(content::DownloadItem* item, net::Error error); |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 158 | |
| 159 | // DownloadItem::Observer implementation: |
| 160 | virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 161 | virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 162 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 163 | // Starts downloading the extension to |file_path|. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame^] | 164 | void StartDownload(const base::FilePath& file_path); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 165 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 166 | // 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] | bcd1eaf7 | 2012-10-03 05:42:29 | [diff] [blame] | 168 | void ReportFailure(const std::string& error, FailureReason reason); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 169 | |
| 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] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 173 | void ReportSuccess(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 174 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 175 | content::NotificationRegistrar registrar_; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 176 | Profile* profile_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 177 | Delegate* delegate_; |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 178 | content::NavigationController* controller_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 179 | std::string id_; |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 180 | // The DownloadItem is owned by the DownloadManager and is valid from when |
[email protected] | 81b80bc | 2012-08-31 18:27:44 | [diff] [blame] | 181 | // OnDownloadStarted is called (with no error) until OnDownloadDestroyed(). |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 182 | content::DownloadItem* download_item_; |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 183 | scoped_ptr<Approval> approval_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 184 | GURL download_url_; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 185 | }; |
| 186 | |
[email protected] | 3d61a7f | 2012-07-12 19:11:25 | [diff] [blame] | 187 | } // namespace extensions |
| 188 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 189 | #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |