[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_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/compiler_specific.h" |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/values.h" |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 16 | #include "content/public/browser/download_id.h" |
| 17 | #include "content/public/browser/download_item.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
| 19 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 20 | #include "net/base/net_errors.h" |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 21 | #include "googleurl/src/gurl.h" |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 22 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 23 | class FilePath; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 24 | class Profile; |
| 25 | |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 26 | namespace content { |
| 27 | class NavigationController; |
| 28 | } |
| 29 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 30 | // Downloads and installs extensions from the web store. |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 31 | class WebstoreInstaller : public content::NotificationObserver, |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 32 | public content::DownloadItem::Observer, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 33 | public base::RefCounted<WebstoreInstaller> { |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 34 | public: |
| 35 | enum Flag { |
| 36 | FLAG_NONE = 0, |
| 37 | |
[email protected] | 99dfecd | 2011-10-18 01:11:50 | [diff] [blame] | 38 | // Inline installs trigger slightly different behavior (install source |
| 39 | // is different, download referrers are the item's page in the gallery). |
| 40 | FLAG_INLINE_INSTALL = 1 << 0 |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class Delegate { |
| 44 | public: |
| 45 | virtual void OnExtensionInstallSuccess(const std::string& id) = 0; |
| 46 | virtual void OnExtensionInstallFailure(const std::string& id, |
| 47 | const std::string& error) = 0; |
| 48 | }; |
| 49 | |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 50 | // If added to the WebstoreInstaller, an Approval indicates that the user has |
| 51 | // already approved the installation and that the CrxInstaller can bypass its |
| 52 | // install prompt. |
| 53 | struct Approval : public content::DownloadItem::ExternalData { |
| 54 | Approval(); |
| 55 | virtual ~Approval(); |
| 56 | |
| 57 | // The extension id that was approved for installation. |
| 58 | std::string extension_id; |
| 59 | |
| 60 | // The profile the extension should be installed into. |
| 61 | Profile* profile; |
| 62 | |
| 63 | // The expected manifest, before localization. |
| 64 | scoped_ptr<base::DictionaryValue> parsed_manifest; |
| 65 | |
| 66 | // Whether to use a bubble notification when an app is installed, instead of |
| 67 | // the default behavior of transitioning to the new tab page. |
| 68 | bool use_app_installed_bubble; |
| 69 | |
| 70 | // Whether to skip the post install UI like the extension installed bubble. |
| 71 | bool skip_post_install_ui; |
| 72 | }; |
| 73 | |
| 74 | // Gets the Approval associated with the |download|, or NULL if there's none. |
| 75 | // Note that the Approval is owned by |download|. |
| 76 | static const Approval* GetAssociatedApproval( |
| 77 | const content::DownloadItem& download); |
| 78 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 79 | // Creates a WebstoreInstaller for downloading and installing the extension |
| 80 | // with the given |id| from the Chrome Web Store. If |delegate| is not NULL, |
| 81 | // it will be notified when the install succeeds or fails. The installer will |
| 82 | // use the specified |controller| to download the extension. Only one |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 83 | // WebstoreInstaller can use a specific controller at any given time. This |
| 84 | // also associates the |approval| with this install. |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 85 | // Note: the delegate should stay alive until being called back. |
| 86 | WebstoreInstaller(Profile* profile, |
| 87 | Delegate* delegate, |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 88 | content::NavigationController* controller, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 89 | const std::string& id, |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 90 | scoped_ptr<Approval> approval, |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 91 | int flags); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 92 | |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 93 | // Starts downloading and installing the extension. |
| 94 | void Start(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 95 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 96 | // content::NotificationObserver |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 97 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 98 | const content::NotificationSource& source, |
| 99 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 100 | |
[email protected] | 9c265d0 | 2011-12-29 22:13:43 | [diff] [blame] | 101 | // Instead of using the default download directory, use |directory| instead. |
| 102 | // This does *not* transfer ownership of |directory|. |
| 103 | static void SetDownloadDirectoryForTests(FilePath* directory); |
| 104 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 105 | private: |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame^] | 106 | friend class base::RefCounted<WebstoreInstaller>; |
| 107 | virtual ~WebstoreInstaller(); |
| 108 | |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 109 | // DownloadManager::DownloadUrl callback. |
| 110 | void OnDownloadStarted(content::DownloadId id, net::Error error); |
| 111 | |
| 112 | // DownloadItem::Observer implementation: |
| 113 | virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 114 | virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE; |
| 115 | |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 116 | // Starts downloading the extension to |file_path|. |
[email protected] | 29679dea | 2012-03-10 03:20:28 | [diff] [blame] | 117 | void StartDownload(const FilePath& file_path); |
[email protected] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 118 | |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 119 | // Reports an install |error| to the delegate for the given extension if this |
| 120 | // managed its installation. This also removes the associated PendingInstall. |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 121 | void ReportFailure(const std::string& error); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 122 | |
| 123 | // Reports a successful install to the delegate for the given extension if |
| 124 | // this managed its installation. This also removes the associated |
| 125 | // PendingInstall. |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 126 | void ReportSuccess(); |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 127 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 128 | content::NotificationRegistrar registrar_; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 129 | Profile* profile_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 130 | Delegate* delegate_; |
[email protected] | cdcb1dee | 2012-01-04 00:46:20 | [diff] [blame] | 131 | content::NavigationController* controller_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 132 | std::string id_; |
[email protected] | 8adbe311 | 2012-03-27 05:42:22 | [diff] [blame] | 133 | // The DownloadItem is owned by the DownloadManager and is valid from when |
| 134 | // OnDownloadStarted is called (with no error) until the DownloadItem |
| 135 | // transitions to state REMOVING. |
| 136 | content::DownloadItem* download_item_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 137 | int flags_; |
[email protected] | 21a5ad6 | 2012-04-03 04:48:45 | [diff] [blame] | 138 | scoped_ptr<Approval> approval_; |
[email protected] | 98e4e52 | 2011-10-25 13:00:16 | [diff] [blame] | 139 | GURL download_url_; |
[email protected] | 655b2b1a | 2011-10-13 17:13:06 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_ |