blob: 510904b1f5f6bf23b75c1f4b8414c04fa1ea6f43 [file] [log] [blame]
[email protected]8e7b2cf42012-04-18 14:26:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d8c8f25f2011-11-02 18:18:012// 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]d8c8f25f2011-11-02 18:18:017
dchengc963c7142016-04-08 03:55:228#include <memory>
[email protected]d8c8f25f2011-11-02 18:18:019#include <string>
[email protected]98270432012-09-11 20:51:2410#include <vector>
[email protected]d8c8f25f2011-11-02 18:18:0111
[email protected]500c7bb2014-04-26 22:44:3312#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
avia2f4804a2015-12-24 23:11:1314#include "base/macros.h"
[email protected]d8c8f25f2011-11-02 18:18:0115#include "base/memory/ref_counted.h"
16#include "base/memory/weak_ptr.h"
Karandeep Bhatiaa8930652017-10-11 17:41:1217#include "base/optional.h"
michaelpg6a4874f2017-04-13 20:41:3318#include "extensions/browser/preload_check.h"
Karan Bhatiafe12281a2017-09-16 02:38:1819#include "extensions/common/manifest.h"
[email protected]d8c8f25f2011-11-02 18:18:0120
dgozmand741d25d2017-03-23 00:35:3421class Profile;
[email protected]d8c8f25f2011-11-02 18:18:0122
23namespace extensions {
24
[email protected]1c321ee2012-05-21 03:02:3425class Extension;
Devlin Cronineea1b7a2018-05-26 02:46:2126class ExtensionService;
michaelpg6a4874f2017-04-13 20:41:3327class PreloadCheckGroup;
[email protected]1c321ee2012-05-21 03:02:3428
[email protected]98270432012-09-11 20:51:2429// Installs and loads an unpacked extension. Because internal state needs to be
30// held about the instalation process, only one call to Load*() should be made
31// per UnpackedInstaller.
[email protected]d8c8f25f2011-11-02 18:18:0132// TODO(erikkay): It might be useful to be able to load a packed extension
33// (presumably into memory) without installing it.
34class UnpackedInstaller
35 : public base::RefCountedThreadSafe<UnpackedInstaller> {
36 public:
rdevlin.croninf6ea63a2015-03-04 17:51:0437 using CompletionCallback = base::Callback<void(const Extension* extension,
38 const base::FilePath&,
39 const std::string&)>;
[email protected]500c7bb2014-04-26 22:44:3340
[email protected]d8c8f25f2011-11-02 18:18:0141 static scoped_refptr<UnpackedInstaller> Create(
42 ExtensionService* extension_service);
43
44 // Loads the extension from the directory |extension_path|, which is
45 // the top directory of a specific extension where its manifest file lives.
Devlin Cronin9722a722017-12-16 03:35:1046 // Errors are reported through LoadErrorReporter. On success,
[email protected]d8c8f25f2011-11-02 18:18:0147 // ExtensionService::AddExtension() is called.
[email protected]650b2d52013-02-10 03:41:4548 void Load(const base::FilePath& extension_path);
[email protected]d8c8f25f2011-11-02 18:18:0149
50 // Loads the extension from the directory |extension_path|;
[email protected]2a69b942013-05-31 09:37:5351 // for use with command line switch --load-extension=path or
52 // --load-and-launch-app=path.
53 // This is equivalent to Load, except that it reads the extension from
54 // |extension_path| synchronously.
55 // The return value indicates whether the installation has begun successfully.
56 // The id of the extension being loaded is returned in |extension_id|.
proberge80a37f32016-08-04 19:44:5557 // |only_allow_apps| is used to avoid side-loading of non-app extensions.
[email protected]2a69b942013-05-31 09:37:5358 bool LoadFromCommandLine(const base::FilePath& extension_path,
proberge80a37f32016-08-04 19:44:5559 std::string* extension_id,
60 bool only_allow_apps);
[email protected]d8c8f25f2011-11-02 18:18:0161
[email protected]b7462f32012-09-02 15:18:1262 // Allows overriding of whether modern manifest versions are required;
63 // intended for testing.
64 bool require_modern_manifest_version() const {
65 return require_modern_manifest_version_;
66 }
67 void set_require_modern_manifest_version(bool val) {
68 require_modern_manifest_version_ = val;
69 }
70
[email protected]1f0722442014-05-01 17:26:0271 void set_be_noisy_on_failure(bool be_noisy_on_failure) {
72 be_noisy_on_failure_ = be_noisy_on_failure;
73 }
74
rdevlin.croninf6ea63a2015-03-04 17:51:0475 void set_completion_callback(const CompletionCallback& callback) {
76 callback_ = callback;
77 }
78
[email protected]d8c8f25f2011-11-02 18:18:0179 private:
80 friend class base::RefCountedThreadSafe<UnpackedInstaller>;
81
82 explicit UnpackedInstaller(ExtensionService* extension_service);
83 virtual ~UnpackedInstaller();
84
Karan Bhatiaf70d840b2017-11-22 19:21:0985 // Must be called from the UI thread. Begin management policy and requirements
86 // checks.
[email protected]253fc2bb2014-07-10 04:21:1887 void StartInstallChecks();
[email protected]98270432012-09-11 20:51:2488
michaelpg6a4874f2017-04-13 20:41:3389 // Callback from PreloadCheckGroup.
Istiaque Ahmed400c83a2017-10-11 02:39:3590 void OnInstallChecksComplete(const PreloadCheck::Errors& errors);
[email protected]98270432012-09-11 20:51:2491
[email protected]8e7b2cf42012-04-18 14:26:5892 // Verifies if loading unpacked extensions is allowed.
93 bool IsLoadingUnpackedAllowed() const;
94
[email protected]d8c8f25f2011-11-02 18:18:0195 // We change the input extension path to an absolute path, on the file thread.
96 // Then we need to check the file access preference, which needs
97 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on
98 // the UI thread. In turn, once that gets the pref, it goes back to the
99 // file thread with LoadWithFileAccess.
100 // TODO(yoz): It would be nice to remove this ping-pong, but we need to know
[email protected]85df9d12014-04-15 17:02:14101 // what file access flags to pass to file_util::LoadExtension.
[email protected]d8c8f25f2011-11-02 18:18:01102 void GetAbsolutePath();
103 void CheckExtensionFileAccess();
[email protected]b7462f32012-09-02 15:18:12104 void LoadWithFileAccess(int flags);
[email protected]d8c8f25f2011-11-02 18:18:01105
[email protected]add57db72014-04-01 23:25:22106 // Notify the frontend that an attempt to retry will not be necessary.
107 void UnregisterLoadRetryListener();
108
[email protected]d8c8f25f2011-11-02 18:18:01109 // Notify the frontend that there was an error loading an extension.
110 void ReportExtensionLoadError(const std::string& error);
111
[email protected]253fc2bb2014-07-10 04:21:18112 // Passes the extension onto extension service.
113 void InstallExtension();
[email protected]d8c8f25f2011-11-02 18:18:01114
[email protected]b7462f32012-09-02 15:18:12115 // Helper to get the Extension::CreateFlags for the installing extension.
116 int GetFlags();
117
Karan Bhatiafe12281a2017-09-16 02:38:18118 // Helper to load an extension. Should be called on a sequence where file IO
Karan Bhatia328e31a2017-09-18 21:46:16119 // is allowed. Loads the extension, validates extension locales and persists
120 // the ruleset for the Declarative Net Request API, if needed. In case of an
121 // error, returns false and populates |error|.
Karan Bhatiafe12281a2017-09-16 02:38:18122 bool LoadExtension(Manifest::Location location,
123 int flags,
124 std::string* error);
125
Karan Bhatia328e31a2017-09-18 21:46:16126 // Reads the Declarative Net Request JSON ruleset for the extension, if it
127 // provided one, and persists the indexed ruleset. Returns false and populates
128 // |error| in case of an error. Should be called on a sequence where file IO
129 // is allowed.
130 bool IndexAndPersistRulesIfNeeded(std::string* error);
131
dgozmand741d25d2017-03-23 00:35:34132 const Extension* extension() { return extension_.get(); }
[email protected]253fc2bb2014-07-10 04:21:18133
[email protected]849749d2013-05-06 17:30:45134 // The service we will report results back to.
[email protected]d8c8f25f2011-11-02 18:18:01135 base::WeakPtr<ExtensionService> service_weak_;
136
dgozmand741d25d2017-03-23 00:35:34137 // The Profile the extension is being installed in.
138 Profile* profile_;
139
[email protected]d8c8f25f2011-11-02 18:18:01140 // The pathname of the directory to load from, which is an absolute path
141 // after GetAbsolutePath has been called.
[email protected]650b2d52013-02-10 03:41:45142 base::FilePath extension_path_;
[email protected]d8c8f25f2011-11-02 18:18:01143
dgozmand741d25d2017-03-23 00:35:34144 // The extension being installed.
Karan Bhatia328e31a2017-09-18 21:46:16145 scoped_refptr<Extension> extension_;
dgozmand741d25d2017-03-23 00:35:34146
[email protected]b7462f32012-09-02 15:18:12147 // Whether to require the extension installed to have a modern manifest
148 // version.
149 bool require_modern_manifest_version_;
150
[email protected]1f0722442014-05-01 17:26:02151 // Whether or not to be noisy (show a dialog) on failure. Defaults to true.
152 bool be_noisy_on_failure_;
153
michaelpg6a4874f2017-04-13 20:41:33154 // Checks to run before the extension can be installed.
155 std::unique_ptr<PreloadCheck> policy_check_;
156 std::unique_ptr<PreloadCheck> requirements_check_;
157
158 // Runs the above checks.
159 std::unique_ptr<PreloadCheckGroup> check_group_;
[email protected]849749d2013-05-06 17:30:45160
Karandeep Bhatiaa8930652017-10-11 17:41:12161 // The checksum for the indexed ruleset corresponding to the Declarative Net
162 // Request API.
163 base::Optional<int> dnr_ruleset_checksum_;
164
rdevlin.croninf6ea63a2015-03-04 17:51:04165 CompletionCallback callback_;
166
[email protected]d8c8f25f2011-11-02 18:18:01167 DISALLOW_COPY_AND_ASSIGN(UnpackedInstaller);
168};
169
170} // namespace extensions
171
172#endif // CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_