| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" |
| 11 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "chrome/browser/extensions/extension_install_prompt.h" | 15 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 15 #include "chrome/browser/extensions/webstore_install_helper.h" | 16 #include "chrome/browser/extensions/webstore_install_helper.h" |
| 16 #include "chrome/browser/extensions/webstore_installer.h" | 17 #include "chrome/browser/extensions/webstore_installer.h" |
| 17 #include "chrome/browser/ui/browser_list_observer.h" | 18 #include "chrome/browser/ui/browser_list_observer.h" |
| 18 #include "chrome/browser/ui/host_desktop.h" | 19 #include "chrome/browser/ui/host_desktop.h" |
| 19 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "url/gurl.h" |
| 20 | 23 |
| 21 namespace base { | 24 namespace base { |
| 22 class DictionaryValue; | 25 class DictionaryValue; |
| 23 } // namespace base | 26 } // namespace base |
| 24 | 27 |
| 25 namespace content { | 28 namespace content { |
| 26 class WebContents; | 29 class WebContents; |
| 27 } // namespace content | 30 } // namespace content |
| 28 | 31 |
| 29 class Browser; | 32 class Browser; |
| 30 class Profile; | 33 class Profile; |
| 31 | 34 |
| 32 namespace extensions { | 35 namespace extensions { |
| 33 | 36 |
| 34 // Manages the installation life cycle for extension bundles. | 37 // Manages the installation life cycle for extension bundles. |
| 35 // | 38 // |
| 36 // We install bundles in two steps: | 39 // We install bundles in two steps: |
| 37 // 1) PromptForApproval: parse manifests and prompt the user | 40 // 1) PromptForApproval: parse manifests and prompt the user |
| 38 // 2) CompleteInstall: install the CRXs and show confirmation bubble | 41 // 2) CompleteInstall: install the CRXs and show confirmation bubble |
| 39 // | 42 // |
| 40 class BundleInstaller : public WebstoreInstallHelper::Delegate, | 43 class BundleInstaller : public WebstoreInstallHelper::Delegate, |
| 41 public ExtensionInstallPrompt::Delegate, | 44 public ExtensionInstallPrompt::Delegate, |
| 42 public WebstoreInstaller::Delegate, | 45 public WebstoreInstaller::Delegate, |
| 43 public chrome::BrowserListObserver, | 46 public chrome::BrowserListObserver { |
| 44 public base::RefCountedThreadSafe<BundleInstaller> { | |
| 45 public: | 47 public: |
| 46 // Auto approve or cancel the permission prompt. | 48 // Auto approve or cancel the permission prompt. |
| 47 static void SetAutoApproveForTesting(bool approve); | 49 static void SetAutoApproveForTesting(bool approve); |
| 48 | 50 |
| 49 class Delegate { | |
| 50 public: | |
| 51 virtual void OnBundleInstallApproved() {} | |
| 52 virtual void OnBundleInstallCanceled(bool user_initiated) {} | |
| 53 virtual void OnBundleInstallCompleted() {} | |
| 54 | |
| 55 protected: | |
| 56 virtual ~Delegate() {} | |
| 57 }; | |
| 58 | |
| 59 // Represents an individual member of the bundle. | 51 // Represents an individual member of the bundle. |
| 60 struct Item { | 52 struct Item { |
| 61 // Items are in the PENDING state until they've been installed, or the | 53 // Items are in the PENDING state until they've been installed, or the |
| 62 // install has failed or been canceled. | 54 // install has failed or been canceled. |
| 63 enum State { | 55 enum State { |
| 64 STATE_PENDING, | 56 STATE_PENDING, |
| 65 STATE_INSTALLED, | 57 STATE_INSTALLED, |
| 66 STATE_FAILED | 58 STATE_FAILED |
| 67 }; | 59 }; |
| 68 | 60 |
| 69 Item(); | 61 Item(); |
| 62 ~Item(); |
| 70 | 63 |
| 71 // Gets the localized name, formatted for display in the prompt or bubble. | 64 // Gets the localized name, formatted for display in the bubble. |
| 72 base::string16 GetNameForDisplay(); | 65 base::string16 GetNameForDisplay() const; |
| 73 | 66 |
| 74 std::string id; | 67 std::string id; |
| 75 std::string manifest; | 68 std::string manifest; |
| 76 std::string localized_name; | 69 std::string localized_name; |
| 70 GURL icon_url; |
| 71 std::string icon_data; |
| 72 SkBitmap icon; |
| 77 State state; | 73 State state; |
| 78 }; | 74 }; |
| 79 | 75 |
| 76 enum ApprovalState { |
| 77 APPROVED, |
| 78 USER_CANCELED, |
| 79 APPROVAL_ERROR |
| 80 }; |
| 81 |
| 82 typedef base::Callback<void(ApprovalState)> ApprovalCallback; |
| 83 |
| 80 typedef std::vector<Item> ItemList; | 84 typedef std::vector<Item> ItemList; |
| 81 | 85 |
| 82 BundleInstaller(Browser* browser, const ItemList& items); | 86 BundleInstaller(Browser* browser, |
| 87 const std::string& name, |
| 88 const SkBitmap& icon, |
| 89 const std::string& authuser, |
| 90 const ItemList& items); |
| 91 ~BundleInstaller() override; |
| 83 | 92 |
| 84 // Returns true if the user has approved the bundle's permissions. | 93 // Returns true if the user has approved the bundle's permissions. |
| 85 bool approved() const { return approved_; } | 94 bool approved() const { return approved_; } |
| 86 | 95 |
| 87 // Gets the items in the given state. | 96 // Returns the browser window associated with the bundle's installation. |
| 97 // Can return null if the browser is closed during the installation. |
| 98 Browser* browser() { return browser_; } |
| 99 |
| 100 // Gets the items in the given |state|. |
| 88 ItemList GetItemsWithState(Item::State state) const; | 101 ItemList GetItemsWithState(Item::State state) const; |
| 89 | 102 |
| 103 // Returns whether there is at least one item with the given |state|. |
| 104 bool HasItemWithState(Item::State state) const; |
| 105 |
| 106 // Returns the number of items with the given |state|. |
| 107 size_t CountItemsWithState(Item::State state) const; |
| 108 |
| 90 // Parses the extension manifests and then prompts the user to approve their | 109 // Parses the extension manifests and then prompts the user to approve their |
| 91 // permissions. One of OnBundleInstallApproved or OnBundleInstallCanceled | 110 // permissions. |
| 92 // will be called when complete if |delegate| is not NULL. | 111 void PromptForApproval(const ApprovalCallback& callback); |
| 93 // Note: the |delegate| must stay alive until receiving the callback. | |
| 94 void PromptForApproval(Delegate* delegate); | |
| 95 | 112 |
| 96 // If the bundle has been approved, this downloads and installs the member | 113 // If the bundle has been approved, this downloads and installs the member |
| 97 // extensions. OnBundleInstallComplete will be called when the process is | 114 // extensions. The download process uses the NavigationController of the |
| 98 // complete and |delegate| is not NULL. The download process uses the | 115 // specified |web_contents|. When complete, we show a confirmation bubble. |
| 99 // NavigationController of the specified |web_contents|. When complete, we | 116 void CompleteInstall(content::WebContents* web_contents, |
| 100 // show a confirmation bubble in the specified |browser|. | 117 const base::Closure& callback); |
| 101 // Note: the |delegate| must stay alive until receiving the callback. | |
| 102 void CompleteInstall(content::WebContents* web_contents, Delegate* delegate); | |
| 103 | 118 |
| 104 // We change the headings in the install prompt and installed bubble depending | 119 // We change the headings in the install prompt and installed bubble depending |
| 105 // on whether the bundle contains apps, extensions or both. This method gets | 120 // on whether the bundle contains apps, extensions or both. This method gets |
| 106 // the correct heading for the items in the specified |state|, or an empty | 121 // the correct heading for the items in the specified |state|, or an empty |
| 107 // string if no items are in the |state|. | 122 // string if no items are in the |state|. |
| 108 // STATE_PENDING - install prompt | 123 // STATE_PENDING - install prompt |
| 109 // STATE_INSTALLED - installed bubble successful installs list | 124 // STATE_INSTALLED - installed bubble successful installs list |
| 110 // STATE_FAILED - installed bubble failed installs list | 125 // STATE_FAILED - installed bubble failed installs list |
| 111 base::string16 GetHeadingTextFor(Item::State state) const; | 126 base::string16 GetHeadingTextFor(Item::State state) const; |
| 112 | 127 |
| 113 private: | 128 private: |
| 114 friend class base::RefCountedThreadSafe<BundleInstaller>; | |
| 115 | |
| 116 typedef std::map<std::string, Item> ItemMap; | 129 typedef std::map<std::string, Item> ItemMap; |
| 117 typedef std::map<std::string, linked_ptr<base::DictionaryValue> > ManifestMap; | 130 typedef std::map<std::string, linked_ptr<base::DictionaryValue> > ManifestMap; |
| 118 | 131 |
| 119 ~BundleInstaller() override; | |
| 120 | |
| 121 // Displays the install bubble for |bundle| on |browser|. | 132 // Displays the install bubble for |bundle| on |browser|. |
| 122 // Note: this is a platform specific implementation. | 133 // Note: this is a platform specific implementation. |
| 123 static void ShowInstalledBubble(const BundleInstaller* bundle, | 134 static void ShowInstalledBubble(const BundleInstaller* bundle, |
| 124 Browser* browser); | 135 Browser* browser); |
| 125 | 136 |
| 126 // Parses the manifests using WebstoreInstallHelper. | 137 // Parses the manifests using WebstoreInstallHelper. |
| 127 void ParseManifests(); | 138 void ParseManifests(); |
| 128 | 139 |
| 129 // Notifies the delegate that the installation has been approved. | |
| 130 void ReportApproved(); | |
| 131 | |
| 132 // Notifies the delegate that the installation was canceled. | |
| 133 void ReportCanceled(bool user_initiated); | |
| 134 | |
| 135 // Notifies the delegate that the installation is complete. | |
| 136 void ReportComplete(); | |
| 137 | |
| 138 // Prompts the user to install the bundle once we have dummy extensions for | 140 // Prompts the user to install the bundle once we have dummy extensions for |
| 139 // all the pending items. | 141 // all the pending items. |
| 140 void ShowPromptIfDoneParsing(); | 142 void ShowPromptIfDoneParsing(); |
| 141 | 143 |
| 142 // Prompts the user to install the bundle. | 144 // Prompts the user to install the bundle. |
| 143 void ShowPrompt(); | 145 void ShowPrompt(); |
| 144 | 146 |
| 145 // Displays the installed bubble once all items have installed or failed. | 147 // Displays the installed bubble once all items have installed or failed. |
| 146 void ShowInstalledBubbleIfDone(); | 148 void ShowInstalledBubbleIfDone(); |
| 147 | 149 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 158 void InstallUIAbort(bool user_initiated) override; | 160 void InstallUIAbort(bool user_initiated) override; |
| 159 | 161 |
| 160 // WebstoreInstaller::Delegate implementation: | 162 // WebstoreInstaller::Delegate implementation: |
| 161 void OnExtensionInstallSuccess(const std::string& id) override; | 163 void OnExtensionInstallSuccess(const std::string& id) override; |
| 162 void OnExtensionInstallFailure( | 164 void OnExtensionInstallFailure( |
| 163 const std::string& id, | 165 const std::string& id, |
| 164 const std::string& error, | 166 const std::string& error, |
| 165 WebstoreInstaller::FailureReason reason) override; | 167 WebstoreInstaller::FailureReason reason) override; |
| 166 | 168 |
| 167 // chrome::BrowserListObserver implementation: | 169 // chrome::BrowserListObserver implementation: |
| 168 void OnBrowserAdded(Browser* browser) override; | |
| 169 void OnBrowserRemoved(Browser* browser) override; | 170 void OnBrowserRemoved(Browser* browser) override; |
| 170 void OnBrowserSetLastActive(Browser* browser) override; | |
| 171 | 171 |
| 172 // Holds the Extensions used to generate the permission warnings. | 172 // Holds the Extensions used to generate the permission warnings. |
| 173 ExtensionList dummy_extensions_; | 173 ExtensionList dummy_extensions_; |
| 174 | 174 |
| 175 // Holds the parsed manifests, indexed by the extension ids. | 175 // Holds the parsed manifests, indexed by the extension ids. |
| 176 ManifestMap parsed_manifests_; | 176 ManifestMap parsed_manifests_; |
| 177 | 177 |
| 178 // True if the user has approved the bundle. | 178 // True if the user has approved the bundle. |
| 179 bool approved_; | 179 bool approved_; |
| 180 | 180 |
| 181 // Holds the bundle's Items, indexed by their ids. | 181 // Holds the bundle's Items, indexed by their ids. |
| 182 ItemMap items_; | 182 ItemMap items_; |
| 183 | 183 |
| 184 // The browser to show the confirmation bubble for. | 184 // The browser to show the confirmation bubble for. |
| 185 Browser* browser_; | 185 Browser* browser_; |
| 186 | 186 |
| 187 // The bundle's display name. |
| 188 std::string name_; |
| 189 |
| 190 // The bundle's icon. |
| 191 SkBitmap icon_; |
| 192 |
| 193 // The authuser query parameter value which should be used with CRX download |
| 194 // requests. May be empty. |
| 195 std::string authuser_; |
| 196 |
| 187 // The desktop type of the browser. | 197 // The desktop type of the browser. |
| 188 chrome::HostDesktopType host_desktop_type_; | 198 chrome::HostDesktopType host_desktop_type_; |
| 189 | 199 |
| 190 // The profile that the bundle should be installed in. | 200 // The profile that the bundle should be installed in. |
| 191 Profile* profile_; | 201 Profile* profile_; |
| 192 | 202 |
| 193 // The UI that shows the confirmation prompt. | 203 // The UI that shows the confirmation prompt. |
| 194 scoped_ptr<ExtensionInstallPrompt> install_ui_; | 204 scoped_ptr<ExtensionInstallPrompt> install_ui_; |
| 195 | 205 |
| 196 Delegate* delegate_; | 206 ApprovalCallback approval_callback_; |
| 207 base::Closure install_callback_; |
| 197 | 208 |
| 198 DISALLOW_COPY_AND_ASSIGN(BundleInstaller); | 209 DISALLOW_COPY_AND_ASSIGN(BundleInstaller); |
| 199 }; | 210 }; |
| 200 | 211 |
| 201 } // namespace extensions | 212 } // namespace extensions |
| 202 | 213 |
| 203 #endif // CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | 214 #endif // CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ |
| OLD | NEW |