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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/extensions/extension_install_prompt.h"
15 #include "chrome/browser/extensions/webstore_install_helper.h"
16 #include "chrome/browser/extensions/webstore_installer.h"
17 #include "chrome/browser/ui/browser_list_observer.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "extensions/common/extension.h"
22 class DictionaryValue;
27 } // namespace content
32 namespace extensions {
34 // Manages the installation life cycle for extension bundles.
36 // We install bundles in two steps:
37 // 1) PromptForApproval: parse manifests and prompt the user
38 // 2) CompleteInstall: install the CRXs and show confirmation bubble
40 class BundleInstaller : public WebstoreInstallHelper::Delegate,
41 public ExtensionInstallPrompt::Delegate,
42 public WebstoreInstaller::Delegate,
43 public chrome::BrowserListObserver,
44 public base::RefCountedThreadSafe<BundleInstaller> {
46 // Auto approve or cancel the permission prompt.
47 static void SetAutoApproveForTesting(bool approve);
51 virtual void OnBundleInstallApproved() {}
52 virtual void OnBundleInstallCanceled(bool user_initiated) {}
53 virtual void OnBundleInstallCompleted() {}
56 virtual ~Delegate() {}
59 // Represents an individual member of the bundle.
61 // Items are in the PENDING state until they've been installed, or the
62 // install has failed or been canceled.
71 // Gets the localized name, formatted for display in the prompt or bubble.
72 base::string16 GetNameForDisplay();
76 std::string localized_name;
80 typedef std::vector<Item> ItemList;
82 BundleInstaller(Browser* browser, const ItemList& items);
84 // Returns true if the user has approved the bundle's permissions.
85 bool approved() const { return approved_; }
87 // Gets the items in the given state.
88 ItemList GetItemsWithState(Item::State state) const;
90 // Parses the extension manifests and then prompts the user to approve their
91 // permissions. One of OnBundleInstallApproved or OnBundleInstallCanceled
92 // will be called when complete if |delegate| is not NULL.
93 // Note: the |delegate| must stay alive until receiving the callback.
94 void PromptForApproval(Delegate* delegate);
96 // If the bundle has been approved, this downloads and installs the member
97 // extensions. OnBundleInstallComplete will be called when the process is
98 // complete and |delegate| is not NULL. The download process uses the
99 // NavigationController of the specified |web_contents|. When complete, we
100 // show a confirmation bubble in the specified |browser|.
101 // Note: the |delegate| must stay alive until receiving the callback.
102 void CompleteInstall(content::WebContents* web_contents, Delegate* delegate);
104 // 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
106 // the correct heading for the items in the specified |state|, or an empty
107 // string if no items are in the |state|.
108 // STATE_PENDING - install prompt
109 // STATE_INSTALLED - installed bubble successful installs list
110 // STATE_FAILED - installed bubble failed installs list
111 base::string16 GetHeadingTextFor(Item::State state) const;
114 friend class base::RefCountedThreadSafe<BundleInstaller>;
116 typedef std::map<std::string, Item> ItemMap;
117 typedef std::map<std::string, linked_ptr<base::DictionaryValue> > ManifestMap;
119 ~BundleInstaller() override;
121 // Displays the install bubble for |bundle| on |browser|.
122 // Note: this is a platform specific implementation.
123 static void ShowInstalledBubble(const BundleInstaller* bundle,
126 // Parses the manifests using WebstoreInstallHelper.
127 void ParseManifests();
129 // Notifies the delegate that the installation has been approved.
130 void ReportApproved();
132 // Notifies the delegate that the installation was canceled.
133 void ReportCanceled(bool user_initiated);
135 // Notifies the delegate that the installation is complete.
136 void ReportComplete();
138 // Prompts the user to install the bundle once we have dummy extensions for
139 // all the pending items.
140 void ShowPromptIfDoneParsing();
142 // Prompts the user to install the bundle.
145 // Displays the installed bubble once all items have installed or failed.
146 void ShowInstalledBubbleIfDone();
148 // WebstoreInstallHelper::Delegate implementation:
149 void OnWebstoreParseSuccess(const std::string& id,
150 const SkBitmap& icon,
151 base::DictionaryValue* parsed_manifest) override;
152 void OnWebstoreParseFailure(const std::string& id,
153 InstallHelperResultCode result_code,
154 const std::string& error_message) override;
156 // ExtensionInstallPrompt::Delegate implementation:
157 void InstallUIProceed() override;
158 void InstallUIAbort(bool user_initiated) override;
160 // WebstoreInstaller::Delegate implementation:
161 void OnExtensionInstallSuccess(const std::string& id) override;
162 void OnExtensionInstallFailure(
163 const std::string& id,
164 const std::string& error,
165 WebstoreInstaller::FailureReason reason) override;
167 // chrome::BrowserListObserver implementation:
168 void OnBrowserAdded(Browser* browser) override;
169 void OnBrowserRemoved(Browser* browser) override;
170 void OnBrowserSetLastActive(Browser* browser) override;
172 // Holds the Extensions used to generate the permission warnings.
173 ExtensionList dummy_extensions_;
175 // Holds the parsed manifests, indexed by the extension ids.
176 ManifestMap parsed_manifests_;
178 // True if the user has approved the bundle.
181 // Holds the bundle's Items, indexed by their ids.
184 // The browser to show the confirmation bubble for.
187 // The desktop type of the browser.
188 chrome::HostDesktopType host_desktop_type_;
190 // The profile that the bundle should be installed in.
193 // The UI that shows the confirmation prompt.
194 scoped_ptr<ExtensionInstallPrompt> install_ui_;
198 DISALLOW_COPY_AND_ASSIGN(BundleInstaller);
201 } // namespace extensions
203 #endif // CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_