Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / extensions / bundle_installer.h
blob61bf273aeb05f322ce13810cb9b8138891fafadf
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_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/browser/extensions/extension_install_prompt.h"
16 #include "chrome/browser/extensions/webstore_install_helper.h"
17 #include "chrome/browser/extensions/webstore_installer.h"
18 #include "chrome/browser/ui/browser_list_observer.h"
19 #include "chrome/browser/ui/host_desktop.h"
20 #include "extensions/common/extension.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "url/gurl.h"
24 namespace base {
25 class DictionaryValue;
26 } // namespace base
28 namespace content {
29 class WebContents;
30 } // namespace content
32 class Browser;
33 class Profile;
35 namespace extensions {
37 // Manages the installation life cycle for extension bundles.
39 // We install bundles in two steps:
40 // 1) PromptForApproval: parse manifests and prompt the user
41 // 2) CompleteInstall: install the CRXs and show confirmation bubble
43 class BundleInstaller : public WebstoreInstallHelper::Delegate,
44 public ExtensionInstallPrompt::Delegate,
45 public WebstoreInstaller::Delegate,
46 public chrome::BrowserListObserver {
47 public:
48 // Auto approve or cancel the permission prompt.
49 static void SetAutoApproveForTesting(bool approve);
51 // Represents an individual member of the bundle.
52 struct Item {
53 // Items are in the PENDING state until they've been installed, or the
54 // install has failed or been canceled.
55 enum State {
56 STATE_PENDING,
57 STATE_INSTALLED,
58 STATE_FAILED
61 Item();
62 ~Item();
64 // Gets the localized name, formatted for display in the bubble.
65 base::string16 GetNameForDisplay() const;
67 std::string id;
68 std::string manifest;
69 std::string localized_name;
70 GURL icon_url;
71 SkBitmap icon;
72 State state;
75 enum ApprovalState {
76 APPROVED,
77 USER_CANCELED,
78 APPROVAL_ERROR
81 typedef base::Callback<void(ApprovalState)> ApprovalCallback;
83 typedef std::vector<Item> ItemList;
85 BundleInstaller(Browser* browser,
86 const std::string& localized_name,
87 const SkBitmap& icon,
88 const std::string& authuser,
89 const std::string& delegated_username,
90 const ItemList& items);
91 ~BundleInstaller() override;
93 // Returns true if the user has approved the bundle's permissions.
94 bool approved() const { return approved_; }
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_; }
100 // Gets the items in the given |state|.
101 ItemList GetItemsWithState(Item::State state) const;
103 // Returns whether there is at least one item with the given |state|.
104 bool HasItemWithState(Item::State state) const;
106 // Returns the number of items with the given |state|.
107 size_t CountItemsWithState(Item::State state) const;
109 // Parses the extension manifests and then prompts the user to approve their
110 // permissions.
111 void PromptForApproval(const ApprovalCallback& callback);
113 // If the bundle has been approved, this downloads and installs the member
114 // extensions. The download process uses the NavigationController of the
115 // specified |web_contents|. When complete, we show a confirmation bubble.
116 void CompleteInstall(content::WebContents* web_contents,
117 const base::Closure& callback);
119 // We change the headings in the install prompt and installed bubble depending
120 // on whether the bundle contains apps, extensions or both. This method gets
121 // the correct heading for the items in the specified |state|, or an empty
122 // string if no items are in the |state|.
123 // STATE_PENDING - install prompt
124 // STATE_INSTALLED - installed bubble successful installs list
125 // STATE_FAILED - installed bubble failed installs list
126 base::string16 GetHeadingTextFor(Item::State state) const;
128 private:
129 typedef std::map<std::string, Item> ItemMap;
130 typedef std::map<std::string, linked_ptr<base::DictionaryValue> > ManifestMap;
132 // Displays the install bubble for |bundle| on |browser|.
133 // Note: this is a platform specific implementation.
134 static void ShowInstalledBubble(const BundleInstaller* bundle,
135 Browser* browser);
137 // Parses the manifests using WebstoreInstallHelper.
138 void ParseManifests();
140 // Prompts the user to install the bundle once we have dummy extensions for
141 // all the pending items.
142 void ShowPromptIfDoneParsing();
144 // Prompts the user to install the bundle.
145 void ShowPrompt();
147 // Displays the installed bubble once all items have installed or failed.
148 void ShowInstalledBubbleIfDone();
150 // WebstoreInstallHelper::Delegate implementation:
151 void OnWebstoreParseSuccess(const std::string& id,
152 const SkBitmap& icon,
153 base::DictionaryValue* parsed_manifest) override;
154 void OnWebstoreParseFailure(const std::string& id,
155 InstallHelperResultCode result_code,
156 const std::string& error_message) override;
158 // ExtensionInstallPrompt::Delegate implementation:
159 void InstallUIProceed() override;
160 void InstallUIAbort(bool user_initiated) override;
162 // WebstoreInstaller::Delegate implementation:
163 void OnExtensionInstallSuccess(const std::string& id) override;
164 void OnExtensionInstallFailure(
165 const std::string& id,
166 const std::string& error,
167 WebstoreInstaller::FailureReason reason) override;
169 // chrome::BrowserListObserver implementation:
170 void OnBrowserRemoved(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.
179 bool approved_;
181 // Holds the bundle's Items, indexed by their ids.
182 ItemMap items_;
184 // The browser to show the confirmation bubble for.
185 Browser* browser_;
187 // The bundle's display name.
188 std::string name_;
190 // The bundle's icon.
191 SkBitmap icon_;
193 // The authuser query parameter value which should be used with CRX download
194 // requests. May be empty.
195 std::string authuser_;
197 // The display name of the user for which this install happens, in the case
198 // of delegated installs. Empty for regular installs.
199 std::string delegated_username_;
201 // The desktop type of the browser.
202 chrome::HostDesktopType host_desktop_type_;
204 // The profile that the bundle should be installed in.
205 Profile* profile_;
207 // The UI that shows the confirmation prompt.
208 scoped_ptr<ExtensionInstallPrompt> install_ui_;
210 ApprovalCallback approval_callback_;
211 base::Closure install_callback_;
213 DISALLOW_COPY_AND_ASSIGN(BundleInstaller);
216 } // namespace extensions
218 #endif // CHROME_BROWSER_EXTENSIONS_BUNDLE_INSTALLER_H_