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_CRX_INSTALLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_CRX_INSTALLER_H_
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/version.h"
15 #include "chrome/browser/extensions/blacklist.h"
16 #include "chrome/browser/extensions/extension_install_prompt.h"
17 #include "chrome/browser/extensions/extension_installer.h"
18 #include "chrome/browser/extensions/sandboxed_unpacker.h"
19 #include "chrome/browser/extensions/webstore_installer.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/manifest.h"
22 #include "sync/api/string_ordinal.h"
24 class ExtensionService
;
25 class ExtensionServiceTest
;
27 struct WebApplicationInfo
;
30 class SequencedTaskRunner
;
33 namespace extensions
{
34 class CrxInstallerError
;
35 class ExtensionUpdaterTest
;
36 class RequirementsChecker
;
38 // This class installs a crx file into a profile.
40 // Installing a CRX is a multi-step process, including unpacking the crx,
41 // validating it, prompting the user, and installing. Since many of these
42 // steps must occur on the file thread, this class contains a copy of all data
43 // necessary to do its job. (This also minimizes external dependencies for
46 // Lifetime management:
48 // This class is ref-counted by each call it makes to itself on another thread,
49 // and by UtilityProcessHost.
51 // Additionally, we hold a reference to our own client so that it lives at least
52 // long enough to receive the result of unpacking.
54 // IMPORTANT: Callers should keep a reference to a CrxInstaller while they are
55 // working with it, eg:
57 // scoped_refptr<CrxInstaller> installer(new CrxInstaller(...));
58 // installer->set_foo();
59 // installer->set_bar();
60 // installer->InstallCrx(...);
62 // Installation is aborted if the extension service learns that Chrome is
63 // terminating during the install. We can't listen for the app termination
64 // notification here in this class because it can be destroyed on any thread
65 // and won't safely be able to clean up UI thread notification listeners.
67 : public SandboxedUnpackerClient
,
68 public ExtensionInstallPrompt::Delegate
{
70 // Used in histograms; do not change order.
71 enum OffStoreInstallAllowReason
{
72 OffStoreInstallDisallowed
,
73 OffStoreInstallAllowedFromSettingsPage
,
74 OffStoreInstallAllowedBecausePref
,
75 OffStoreInstallAllowedInTest
,
76 NumOffStoreInstallAllowReasons
79 // Extensions will be installed into service->install_directory(), then
80 // registered with |service|. This does a silent install - see below for
82 static scoped_refptr
<CrxInstaller
> CreateSilent(ExtensionService
* service
);
84 // Same as above, but use |client| to generate a confirmation prompt.
85 static scoped_refptr
<CrxInstaller
> Create(
86 ExtensionService
* service
,
87 scoped_ptr
<ExtensionInstallPrompt
> client
);
89 // Same as the previous method, except use the |approval| to bypass the
90 // prompt. Note that the caller retains ownership of |approval|.
91 static scoped_refptr
<CrxInstaller
> Create(
92 ExtensionService
* service
,
93 scoped_ptr
<ExtensionInstallPrompt
> client
,
94 const WebstoreInstaller::Approval
* approval
);
96 // Install the crx in |source_file|.
97 void InstallCrx(const base::FilePath
& source_file
);
99 // Convert the specified user script into an extension and install it.
100 void InstallUserScript(const base::FilePath
& source_file
,
101 const GURL
& download_url
);
103 // Convert the specified web app into an extension and install it.
104 void InstallWebApp(const WebApplicationInfo
& web_app
);
106 // Overridden from ExtensionInstallPrompt::Delegate:
107 virtual void InstallUIProceed() OVERRIDE
;
108 virtual void InstallUIAbort(bool user_initiated
) OVERRIDE
;
110 int creation_flags() const { return creation_flags_
; }
111 void set_creation_flags(int val
) { creation_flags_
= val
; }
113 const base::FilePath
& source_file() const { return source_file_
; }
115 Manifest::Location
install_source() const {
116 return install_source_
;
118 void set_install_source(Manifest::Location source
) {
119 install_source_
= source
;
122 const std::string
& expected_id() const { return expected_id_
; }
123 void set_expected_id(const std::string
& val
) { expected_id_
= val
; }
125 void set_expected_version(const Version
& val
) {
126 expected_version_
.reset(new Version(val
));
127 expected_version_strict_checking_
= true;
130 bool delete_source() const { return delete_source_
; }
131 void set_delete_source(bool val
) { delete_source_
= val
; }
133 bool allow_silent_install() const { return allow_silent_install_
; }
134 void set_allow_silent_install(bool val
) { allow_silent_install_
= val
; }
136 bool is_gallery_install() const {
137 return (creation_flags_
& Extension::FROM_WEBSTORE
) > 0;
139 void set_is_gallery_install(bool val
) {
141 creation_flags_
|= Extension::FROM_WEBSTORE
;
143 creation_flags_
&= ~Extension::FROM_WEBSTORE
;
146 // If |apps_require_extension_mime_type_| is set to true, be sure to set
147 // |original_mime_type_| as well.
148 void set_apps_require_extension_mime_type(
149 bool apps_require_extension_mime_type
) {
150 apps_require_extension_mime_type_
= apps_require_extension_mime_type
;
153 void set_original_mime_type(const std::string
& original_mime_type
) {
154 original_mime_type_
= original_mime_type
;
157 extension_misc::CrxInstallCause
install_cause() const {
158 return install_cause_
;
160 void set_install_cause(extension_misc::CrxInstallCause install_cause
) {
161 install_cause_
= install_cause
;
164 OffStoreInstallAllowReason
off_store_install_allow_reason() const {
165 return off_store_install_allow_reason_
;
167 void set_off_store_install_allow_reason(OffStoreInstallAllowReason reason
) {
168 off_store_install_allow_reason_
= reason
;
171 void set_page_ordinal(const syncer::StringOrdinal
& page_ordinal
) {
172 page_ordinal_
= page_ordinal
;
175 void set_error_on_unsupported_requirements(bool val
) {
176 error_on_unsupported_requirements_
= val
;
179 void set_install_wait_for_idle(bool val
) {
180 install_wait_for_idle_
= val
;
183 bool did_handle_successfully() const { return did_handle_successfully_
; }
185 Profile
* profile() { return installer_
.profile(); }
187 const Extension
* extension() { return installer_
.extension().get(); }
190 friend class ::ExtensionServiceTest
;
191 friend class ExtensionUpdaterTest
;
192 friend class ExtensionCrxInstallerTest
;
194 CrxInstaller(base::WeakPtr
<ExtensionService
> service_weak
,
195 scoped_ptr
<ExtensionInstallPrompt
> client
,
196 const WebstoreInstaller::Approval
* approval
);
197 virtual ~CrxInstaller();
199 // Converts the source user script to an extension.
200 void ConvertUserScriptOnFileThread();
202 // Converts the source web app to an extension.
203 void ConvertWebAppOnFileThread(const WebApplicationInfo
& web_app
,
204 const base::FilePath
& install_directory
);
206 // Called after OnUnpackSuccess as a last check to see whether the install
208 CrxInstallerError
AllowInstall(const Extension
* extension
);
210 // SandboxedUnpackerClient
211 virtual void OnUnpackFailure(const base::string16
& error_message
) OVERRIDE
;
212 virtual void OnUnpackSuccess(const base::FilePath
& temp_dir
,
213 const base::FilePath
& extension_dir
,
214 const base::DictionaryValue
* original_manifest
,
215 const Extension
* extension
,
216 const SkBitmap
& install_icon
) OVERRIDE
;
218 // Called on the UI thread to start the requirements check on the extension.
219 void CheckImportsAndRequirements();
221 // Runs on the UI thread. Callback from RequirementsChecker.
222 void OnRequirementsChecked(std::vector
<std::string
> requirement_errors
);
224 // Runs on the UI thread. Callback from Blacklist.
225 void OnBlacklistChecked(
226 extensions::BlacklistState blacklist_state
);
228 // Runs on the UI thread. Confirms the installation to the ExtensionService.
229 void ConfirmInstall();
231 // Runs on File thread. Install the unpacked extension into the profile and
232 // notify the frontend.
233 void CompleteInstall();
236 void ReportFailureFromFileThread(const CrxInstallerError
& error
);
237 void ReportFailureFromUIThread(const CrxInstallerError
& error
);
238 void ReportSuccessFromFileThread();
239 void ReportSuccessFromUIThread();
240 void NotifyCrxInstallBegin();
241 void NotifyCrxInstallComplete(bool success
);
243 // Deletes temporary directory and crx file if needed.
244 void CleanupTempFiles();
246 // Checks whether the current installation is initiated by the user from
247 // the extension settings page to update an existing extension or app.
248 void CheckUpdateFromSettingsPage();
250 // Show re-enable prompt if the update is initiated from the settings page
251 // and needs additional permissions.
252 void ConfirmReEnable();
254 // The file we're installing.
255 base::FilePath source_file_
;
257 // The URL the file was downloaded from.
260 // The directory extensions are installed to.
261 base::FilePath install_directory_
;
263 // The location the installation came from (bundled with Chromium, registry,
264 // manual install, etc). This metadata is saved with the installation if
265 // successful. Defaults to INTERNAL.
266 Manifest::Location install_source_
;
268 // Indicates whether the user has already approved the extension to be
269 // installed. If true, |expected_manifest_| and |expected_id_| must match
273 // For updates, external and webstore installs we have an ID we're expecting
274 // the extension to contain.
275 std::string expected_id_
;
277 // A parsed copy of the expected manifest, before any transformations like
278 // localization have taken place. If |approved_| is true, then the
279 // extension's manifest must match this for the install to proceed.
280 scoped_ptr
<Manifest
> expected_manifest_
;
282 // The level of checking when comparing the actual manifest against
283 // the |expected_manifest_|.
284 WebstoreInstaller::ManifestCheckLevel expected_manifest_check_level_
;
286 // If non-NULL, contains the expected version of the extension we're
287 // installing. Important for external sources, where claiming the wrong
288 // version could cause unnecessary unpacking of an extension at every
290 scoped_ptr
<Version
> expected_version_
;
292 // If true, the actual version should be same with the |expected_version_|,
293 // Otherwise the actual version should be equal to or newer than
294 // the |expected_version_|.
295 bool expected_version_strict_checking_
;
297 // Whether manual extension installation is enabled. We can't just check this
298 // before trying to install because themes are special-cased to always be
300 bool extensions_enabled_
;
302 // Whether we're supposed to delete the source file on destruction. Defaults
306 // Whether to create an app shortcut after successful installation. This is
307 // set based on the user's selection in the UI and can only ever be true for
309 bool create_app_shortcut_
;
311 // The ordinal of the NTP apps page |extension_| will be shown on.
312 syncer::StringOrdinal page_ordinal_
;
314 // A parsed copy of the unmodified original manifest, before any
315 // transformations like localization have taken place.
316 scoped_ptr
<Manifest
> original_manifest_
;
318 // If non-empty, contains the current version of the extension we're
319 // installing (for upgrades).
320 std::string current_version_
;
322 // The icon we will display in the installation UI, if any.
323 scoped_ptr
<SkBitmap
> install_icon_
;
325 // The temp directory extension resources were unpacked to. We own this and
326 // must delete it when we are done with it.
327 base::FilePath temp_dir_
;
329 // The frontend we will report results back to.
330 base::WeakPtr
<ExtensionService
> service_weak_
;
332 // The client we will work with to do the installation. This can be NULL, in
333 // which case the install is silent.
334 // NOTE: we may be deleted on the file thread. To ensure the UI is deleted on
335 // the main thread we don't use a scoped_ptr here.
336 ExtensionInstallPrompt
* client_
;
338 // The root of the unpacked extension directory. This is a subdirectory of
339 // temp_dir_, so we don't have to delete it explicitly.
340 base::FilePath unpacked_extension_root_
;
342 // True when the CRX being installed was just downloaded.
343 // Used to trigger extra checks before installing.
344 bool apps_require_extension_mime_type_
;
346 // Allows for the possibility of a normal install (one in which a |client|
347 // is provided in the ctor) to procede without showing the permissions prompt
349 bool allow_silent_install_
;
351 // The value of the content type header sent with the CRX.
352 // Ignorred unless |require_extension_mime_type_| is true.
353 std::string original_mime_type_
;
355 // What caused this install? Used only for histograms that report
356 // on failure rates, broken down by the cause of the install.
357 extension_misc::CrxInstallCause install_cause_
;
359 // Creation flags to use for the extension. These flags will be used
360 // when calling Extenion::Create() by the crx installer.
363 // Whether to allow off store installation.
364 OffStoreInstallAllowReason off_store_install_allow_reason_
;
366 // Whether the installation was handled successfully. This is used to
367 // indicate to the client whether the file should be removed and any UI
368 // initiating the installation can be removed. This is different than whether
369 // there was an error; if there was an error that rejects installation we
370 // still consider the installation 'handled'.
371 bool did_handle_successfully_
;
373 // Whether we should produce an error if the manifest declares requirements
374 // that are not met. If false and there is an unmet requirement, the install
375 // will continue but the extension will be distabled.
376 bool error_on_unsupported_requirements_
;
378 bool has_requirement_errors_
;
380 extensions::BlacklistState blacklist_state_
;
382 bool install_wait_for_idle_
;
384 // Sequenced task runner where file I/O operations will be performed.
385 scoped_refptr
<base::SequencedTaskRunner
> installer_task_runner_
;
387 // Used to show the install dialog.
388 ExtensionInstallPrompt::ShowDialogCallback show_dialog_callback_
;
390 // Whether the update is initiated by the user from the extension settings
392 bool update_from_settings_page_
;
394 // Gives access to common methods and data of an extension installer.
395 ExtensionInstaller installer_
;
397 DISALLOW_COPY_AND_ASSIGN(CrxInstaller
);
400 } // namespace extensions
402 #endif // CHROME_BROWSER_EXTENSIONS_CRX_INSTALLER_H_