Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/memory/linked_ptr.h" | |
| 13 #include "chrome/browser/extensions/extension_install_ui.h" | |
| 14 #include "chrome/browser/extensions/webstore_installer.h" | |
| 15 #include "chrome/browser/extensions/webstore_install_helper.h" | |
| 16 #include "chrome/browser/ui/browser_list.h" | |
| 17 #include "chrome/common/extensions/extension.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class DictionaryValue; | |
| 21 } // namespace base | |
| 22 | |
| 23 namespace content { | |
| 24 class NavigationController; | |
| 25 } // namespace content | |
| 26 | |
| 27 class Browser; | |
| 28 class Profile; | |
| 29 | |
| 30 namespace extensions { | |
| 31 | |
| 32 // Manages the installation life cycle for extension bundles. | |
| 33 // | |
| 34 // We install bundles in two steps: | |
| 35 // 1) PromptForApproval: parse manifests and prompt the user | |
| 36 // 2) CompleteInstall: install the CRXs and show confirmation bubble | |
| 37 // | |
| 38 class BundleInstaller : public WebstoreInstallHelper::Delegate, | |
| 39 public ExtensionInstallUI::Delegate, | |
| 40 public WebstoreInstaller::Delegate, | |
| 41 public BrowserList::Observer, | |
| 42 public base::RefCountedThreadSafe<BundleInstaller> { | |
| 43 public: | |
| 44 // Auto approve or cancel the permission prompt. | |
| 45 static void SetAutoApproveForTesting(bool approve); | |
| 46 | |
| 47 class Delegate { | |
| 48 public: | |
| 49 virtual void OnBundleInstallApproved() {} | |
| 50 virtual void OnBundleInstallCanceled(bool user_initiated) {} | |
| 51 virtual void OnBundleInstallCompleted() {} | |
| 52 }; | |
| 53 | |
| 54 // Represents an individual member of the bundle. | |
| 55 struct Item { | |
| 56 // Items are in the PENDING state until they've been installed, or the | |
| 57 // install has failed or been canceled. | |
| 58 enum State { | |
| 59 STATE_PENDING, | |
| 60 STATE_INSTALLED, | |
| 61 STATE_FAILED | |
| 62 }; | |
| 63 | |
| 64 Item(); | |
| 65 | |
| 66 // Creates a dummy extension and sets the manifest's name to the item's | |
| 67 // localized name. | |
| 68 scoped_refptr<Extension> CreateDummyExtension( | |
| 69 base::DictionaryValue* manifest); | |
| 70 | |
| 71 std::string id; | |
| 72 std::string manifest; | |
| 73 std::string localized_name; | |
| 74 State state; | |
| 75 }; | |
| 76 | |
| 77 typedef std::vector<Item> ItemList; | |
| 78 | |
| 79 BundleInstaller(Profile* profile, const ItemList& items); | |
| 80 virtual ~BundleInstaller(); | |
| 81 | |
| 82 // Returns true if the user has approved the bundle's permissions. | |
| 83 bool approved() const { return approved_; } | |
| 84 | |
| 85 // Gets the items in the given state. | |
| 86 ItemList GetItemsByState(Item::State state) const; | |
|
Yoyo Zhou
2012/02/22 00:20:26
GetItemsWithState, perhaps?
jstritar
2012/02/22 15:45:48
Done.
| |
| 87 | |
| 88 // Parses the extension manifests and then prompts the user to approve their | |
| 89 // permissions. One of OnBundleInstallApproved or OnBundleInstallCanceled | |
| 90 // will be called when complete if |delegate| is not NULL. | |
| 91 // Note: the |delegate| must stay alive until receiving the callback. | |
| 92 void PromptForApproval(Delegate* delegate); | |
| 93 | |
| 94 // If the bundle has been approved, this downloads and installs the member | |
| 95 // extensions. OnBundleInstallComplete will be called when the process is | |
| 96 // complete and |delegate| is not NULL. The download process uses the | |
| 97 // specified |controller|. When complete, we show a confirmation bubble in | |
| 98 // the specified |browser|. | |
| 99 // Note: the |delegate| must stay alive until receiving the callback. | |
| 100 void CompleteInstall(content::NavigationController* controller, | |
| 101 Browser* browser, | |
| 102 Delegate* delegate); | |
| 103 | |
| 104 private: | |
| 105 friend class base::RefCountedThreadSafe<BundleInstaller>; | |
| 106 | |
| 107 typedef std::map<std::string, Item> ItemMap; | |
| 108 typedef std::map<std::string, linked_ptr<base::DictionaryValue> > ManifestMap; | |
| 109 | |
| 110 // Displays the install bubble for |bundle| on |browser|. | |
| 111 // Note: this is a platform specific implementation. | |
| 112 static void ShowInstalledBubble(const BundleInstaller* bundle, | |
| 113 Browser* browser); | |
| 114 | |
| 115 // Parses the manifests using WebstoreInstallHelper. | |
| 116 void ParseManifests(); | |
| 117 | |
| 118 // Notifies the delegate that the installation has been approved. | |
| 119 void ReportApproved(); | |
| 120 | |
| 121 // Notifies the delegate that the installation was canceled. | |
| 122 void ReportCanceled(bool user_initiated); | |
| 123 | |
| 124 // Notifies the delegate that the installation is complete. | |
| 125 void ReportComplete(); | |
| 126 | |
| 127 // Prompts the user to install the bundle once we have dummy extensions for | |
| 128 // all the pending items. | |
| 129 void ShowPromptIfDoneParsing(); | |
| 130 | |
| 131 // Prompts the user to install the bundle. | |
| 132 void ShowPrompt(); | |
| 133 | |
| 134 // Displays the installed bubble once all items have installed or failed. | |
| 135 void ShowInstalledBubbleIfDone(); | |
| 136 | |
| 137 // WebstoreInstallHelper::Delegate implementation: | |
| 138 virtual void OnWebstoreParseSuccess( | |
| 139 const std::string& id, | |
| 140 const SkBitmap& icon, | |
| 141 base::DictionaryValue* parsed_manifest) OVERRIDE; | |
| 142 virtual void OnWebstoreParseFailure( | |
| 143 const std::string& id, | |
| 144 InstallHelperResultCode result_code, | |
| 145 const std::string& error_message) OVERRIDE; | |
| 146 | |
| 147 // ExtensionInstallUI::Delegate implementation: | |
| 148 virtual void InstallUIProceed() OVERRIDE; | |
| 149 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; | |
| 150 | |
| 151 // WebstoreInstaller::Delegate implementation: | |
| 152 virtual void OnExtensionInstallSuccess(const std::string& id) OVERRIDE; | |
| 153 virtual void OnExtensionInstallFailure(const std::string& id, | |
| 154 const std::string& error) OVERRIDE; | |
| 155 | |
| 156 // BrowserList::observer implementation: | |
| 157 virtual void OnBrowserAdded(const Browser* browser) OVERRIDE; | |
| 158 virtual void OnBrowserRemoved(const Browser* browser) OVERRIDE; | |
| 159 virtual void OnBrowserSetLastActive(const Browser* browser) OVERRIDE; | |
| 160 | |
| 161 // Holds the Extensions used to generate the permission warnings. | |
| 162 ExtensionList dummy_extensions_; | |
| 163 | |
| 164 // Holds the parsed manifests, indexed by the extension ids. | |
| 165 ManifestMap parsed_manifests_; | |
| 166 | |
| 167 // True if the user has approved the bundle. | |
| 168 bool approved_; | |
| 169 | |
| 170 // Holds the bundle's Items, indexed by their ids. | |
| 171 ItemMap items_; | |
| 172 | |
| 173 // The browser to show the confirmation bubble for. | |
| 174 Browser* browser_; | |
| 175 | |
| 176 // The profile that the bundle should be installed in. | |
| 177 Profile* profile_; | |
| 178 | |
| 179 Delegate* delegate_; | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(BundleInstaller); | |
| 182 }; | |
| 183 | |
| 184 } // namespace extensions | |
| 185 | |
| 186 #endif // CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_ | |
| OLD | NEW |