[email protected] | 8e7b2cf4 | 2012-04-18 14:26:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 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_UNPACKED_INSTALLER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 7 | |
| 8 | #include <string> |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 9 | #include <vector> |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 10 | |
[email protected] | 500c7bb | 2014-04-26 22:44:33 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_install_checker.h" |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 17 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 18 | class ExtensionService; |
| 19 | |
| 20 | namespace extensions { |
| 21 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 22 | class Extension; |
| 23 | |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 24 | // Installs and loads an unpacked extension. Because internal state needs to be |
| 25 | // held about the instalation process, only one call to Load*() should be made |
| 26 | // per UnpackedInstaller. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 27 | // TODO(erikkay): It might be useful to be able to load a packed extension |
| 28 | // (presumably into memory) without installing it. |
| 29 | class UnpackedInstaller |
| 30 | : public base::RefCountedThreadSafe<UnpackedInstaller> { |
| 31 | public: |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame^] | 32 | using CompletionCallback = base::Callback<void(const Extension* extension, |
| 33 | const base::FilePath&, |
| 34 | const std::string&)>; |
[email protected] | 500c7bb | 2014-04-26 22:44:33 | [diff] [blame] | 35 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 36 | static scoped_refptr<UnpackedInstaller> Create( |
| 37 | ExtensionService* extension_service); |
| 38 | |
| 39 | // Loads the extension from the directory |extension_path|, which is |
| 40 | // the top directory of a specific extension where its manifest file lives. |
| 41 | // Errors are reported through ExtensionErrorReporter. On success, |
| 42 | // ExtensionService::AddExtension() is called. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 43 | void Load(const base::FilePath& extension_path); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 44 | |
| 45 | // Loads the extension from the directory |extension_path|; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 46 | // for use with command line switch --load-extension=path or |
| 47 | // --load-and-launch-app=path. |
| 48 | // This is equivalent to Load, except that it reads the extension from |
| 49 | // |extension_path| synchronously. |
| 50 | // The return value indicates whether the installation has begun successfully. |
| 51 | // The id of the extension being loaded is returned in |extension_id|. |
| 52 | bool LoadFromCommandLine(const base::FilePath& extension_path, |
| 53 | std::string* extension_id); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 54 | |
| 55 | // Allows prompting for plugins to be disabled; intended for testing only. |
| 56 | bool prompt_for_plugins() { return prompt_for_plugins_; } |
| 57 | void set_prompt_for_plugins(bool val) { prompt_for_plugins_ = val; } |
| 58 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 59 | // Allows overriding of whether modern manifest versions are required; |
| 60 | // intended for testing. |
| 61 | bool require_modern_manifest_version() const { |
| 62 | return require_modern_manifest_version_; |
| 63 | } |
| 64 | void set_require_modern_manifest_version(bool val) { |
| 65 | require_modern_manifest_version_ = val; |
| 66 | } |
| 67 | |
[email protected] | 1f072244 | 2014-05-01 17:26:02 | [diff] [blame] | 68 | void set_be_noisy_on_failure(bool be_noisy_on_failure) { |
| 69 | be_noisy_on_failure_ = be_noisy_on_failure; |
| 70 | } |
| 71 | |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame^] | 72 | void set_completion_callback(const CompletionCallback& callback) { |
| 73 | callback_ = callback; |
| 74 | } |
| 75 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 76 | private: |
| 77 | friend class base::RefCountedThreadSafe<UnpackedInstaller>; |
| 78 | |
| 79 | explicit UnpackedInstaller(ExtensionService* extension_service); |
| 80 | virtual ~UnpackedInstaller(); |
| 81 | |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 82 | // Must be called from the UI thread. |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 83 | void ShowInstallPrompt(); |
| 84 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 85 | // Begin management policy and requirements checks. |
| 86 | void StartInstallChecks(); |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 87 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 88 | // Callback from ExtensionInstallChecker. |
| 89 | void OnInstallChecksComplete(int failed_checks); |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 90 | |
[email protected] | 8e7b2cf4 | 2012-04-18 14:26:58 | [diff] [blame] | 91 | // Verifies if loading unpacked extensions is allowed. |
| 92 | bool IsLoadingUnpackedAllowed() const; |
| 93 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 94 | // We change the input extension path to an absolute path, on the file thread. |
| 95 | // Then we need to check the file access preference, which needs |
| 96 | // to happen back on the UI thread, so it posts CheckExtensionFileAccess on |
| 97 | // the UI thread. In turn, once that gets the pref, it goes back to the |
| 98 | // file thread with LoadWithFileAccess. |
| 99 | // TODO(yoz): It would be nice to remove this ping-pong, but we need to know |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 100 | // what file access flags to pass to file_util::LoadExtension. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 101 | void GetAbsolutePath(); |
| 102 | void CheckExtensionFileAccess(); |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 103 | void LoadWithFileAccess(int flags); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 104 | |
[email protected] | add57db7 | 2014-04-01 23:25:22 | [diff] [blame] | 105 | // Notify the frontend that an attempt to retry will not be necessary. |
| 106 | void UnregisterLoadRetryListener(); |
| 107 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 108 | // Notify the frontend that there was an error loading an extension. |
| 109 | void ReportExtensionLoadError(const std::string& error); |
| 110 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 111 | // Passes the extension onto extension service. |
| 112 | void InstallExtension(); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 113 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 114 | // Helper to get the Extension::CreateFlags for the installing extension. |
| 115 | int GetFlags(); |
| 116 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 117 | const Extension* extension() { return install_checker_.extension().get(); } |
| 118 | |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 119 | // The service we will report results back to. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 120 | base::WeakPtr<ExtensionService> service_weak_; |
| 121 | |
| 122 | // The pathname of the directory to load from, which is an absolute path |
| 123 | // after GetAbsolutePath has been called. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 124 | base::FilePath extension_path_; |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 125 | |
| 126 | // If true and the extension contains plugins, we prompt the user before |
| 127 | // loading. |
| 128 | bool prompt_for_plugins_; |
| 129 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 130 | // Whether to require the extension installed to have a modern manifest |
| 131 | // version. |
| 132 | bool require_modern_manifest_version_; |
| 133 | |
[email protected] | 1f072244 | 2014-05-01 17:26:02 | [diff] [blame] | 134 | // Whether or not to be noisy (show a dialog) on failure. Defaults to true. |
| 135 | bool be_noisy_on_failure_; |
| 136 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 137 | // Checks management policies and requirements before the extension can be |
| 138 | // installed. |
| 139 | ExtensionInstallChecker install_checker_; |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 140 | |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame^] | 141 | CompletionCallback callback_; |
| 142 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 143 | DISALLOW_COPY_AND_ASSIGN(UnpackedInstaller); |
| 144 | }; |
| 145 | |
| 146 | } // namespace extensions |
| 147 | |
| 148 | #endif // CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ |