Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Side by Side Diff: chrome/browser/extensions/unpacked_installer.cc

Issue 8417012: Refactor loading out of ExtensionService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa's Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "chrome/browser/extensions/unpacked_installer.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/file_util.h"
10 #include "chrome/browser/extensions/extension_install_ui.h"
11 #include "chrome/browser/extensions/extension_prefs.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/extensions/extension_file_util.h"
15
16 namespace {
17
18 // Manages an ExtensionInstallUI for a particular extension.
19 class SimpleExtensionLoadPrompt : public ExtensionInstallUI::Delegate {
20 public:
21 SimpleExtensionLoadPrompt(Profile* profile,
22 base::WeakPtr<ExtensionService> extension_service,
23 const Extension* extension);
24 ~SimpleExtensionLoadPrompt();
25
26 void ShowPrompt();
27
28 // ExtensionInstallUI::Delegate
29 virtual void InstallUIProceed();
30 virtual void InstallUIAbort(bool user_initiated);
31
32 private:
33 base::WeakPtr<ExtensionService> service_weak_;
34 scoped_ptr<ExtensionInstallUI> install_ui_;
35 scoped_refptr<const Extension> extension_;
36 };
37
38 SimpleExtensionLoadPrompt::SimpleExtensionLoadPrompt(
39 Profile* profile,
40 base::WeakPtr<ExtensionService> extension_service,
41 const Extension* extension)
42 : service_weak_(extension_service),
43 install_ui_(new ExtensionInstallUI(profile)),
44 extension_(extension) {
45 }
46
47 SimpleExtensionLoadPrompt::~SimpleExtensionLoadPrompt() {
48 }
49
50 void SimpleExtensionLoadPrompt::ShowPrompt() {
51 install_ui_->ConfirmInstall(this, extension_);
52 }
53
54 void SimpleExtensionLoadPrompt::InstallUIProceed() {
55 if (service_weak_.get())
56 service_weak_->OnExtensionInstalled(
57 extension_, false, -1); // Not from web store.
58 delete this;
59 }
60
61 void SimpleExtensionLoadPrompt::InstallUIAbort(bool user_initiated) {
62 delete this;
63 }
64
65 } // namespace
66
67 namespace extensions {
68
69 UnpackedInstaller::UnpackedInstaller(
70 base::WeakPtr<ExtensionService> extension_service)
71 : service_weak_(extension_service),
72 prompt_for_plugins_(true) {
73 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 }
75
76 UnpackedInstaller::~UnpackedInstaller() {
77 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
78 BrowserThread::CurrentlyOn(BrowserThread::FILE));
79 }
80
81 void UnpackedInstaller::Load(const FilePath& extension_path) {
82 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
83 base::Bind(&UnpackedInstaller::LoadSingleExtension,
84 this, extension_path));
85 }
86
87 void UnpackedInstaller::LoadFromCommandLine(
88 const FilePath& path_in) {
89 if (!service_weak_.get())
90 return;
91 // Load extensions from the command line synchronously to avoid a race
92 // between extension loading and loading an URL from the command line.
93 base::ThreadRestrictions::ScopedAllowIO allow_io;
94
95 FilePath extension_path = path_in;
96 file_util::AbsolutePath(&extension_path);
97
98 std::string id = Extension::GenerateIdForPath(extension_path);
99 bool allow_file_access =
100 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
101 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
102 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
103
104 int flags = Extension::NO_FLAGS;
105 if (allow_file_access)
106 flags |= Extension::ALLOW_FILE_ACCESS;
107 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
108 flags |= Extension::STRICT_ERROR_CHECKS;
109
110 std::string error;
111 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
112 extension_path,
113 Extension::LOAD,
114 flags,
115 &error));
116
117 if (!extension) {
118 // TODO(aa): Remove ReportExtensionLoadError. It doesn't do enough to be
119 // worth the dependency on ES.
120 service_weak_->ReportExtensionLoadError(extension_path, error, true);
121 return;
122 }
123
124 OnLoadSingleExtension(extension);
125 }
126
127 void UnpackedInstaller::LoadSingleExtension(const FilePath& extension_path) {
128 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
129
130 FilePath absolute_path = extension_path;
131 file_util::AbsolutePath(&absolute_path);
132
133 // TODO(yoz): It would be nice to remove this ping-pong, but we need to know
134 // what file access flags to pass to extension_file_util::LoadExtension.
135 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
136 base::Bind(&UnpackedInstaller::CheckExtensionFileAccess,
137 this, absolute_path));
138 }
139
140 void UnpackedInstaller::CheckExtensionFileAccess(
141 const FilePath& extension_path) {
142 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143 std::string id = Extension::GenerateIdForPath(extension_path);
144 // Unpacked extensions default to allowing file access, but if that has been
145 // overridden, don't reset the value.
146 bool allow_file_access =
147 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
148 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
149 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
150
151 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
152 base::Bind(
153 &UnpackedInstaller::LoadSingleExtensionWithFileAccess,
154 this, extension_path, allow_file_access));
155 }
156
157 void UnpackedInstaller::LoadSingleExtensionWithFileAccess(
158 const FilePath& extension_path,
159 bool allow_file_access) {
160 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
161 int flags = allow_file_access ?
162 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
163 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
164 flags |= Extension::STRICT_ERROR_CHECKS;
165 std::string error;
166 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
167 extension_path,
168 Extension::LOAD,
169 flags,
170 &error));
171
172 if (!extension) {
173 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
174 base::Bind(
175 &UnpackedInstaller::ReportExtensionLoadError,
176 this,
177 extension_path, error));
178 return;
179 }
180
181 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
182 base::Bind(
183 &UnpackedInstaller::OnLoadSingleExtension,
184 this, extension));
185 }
186
187 void UnpackedInstaller::ReportExtensionLoadError(
188 const FilePath& extension_path, const std::string &error) {
189 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 if (!service_weak_.get())
191 return;
192 service_weak_->ReportExtensionLoadError(extension_path, error, true);
193 }
194
195 void UnpackedInstaller::OnLoadSingleExtension(
196 const scoped_refptr<const Extension>& extension) {
197 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
198 if (!service_weak_.get())
199 return;
200 const ExtensionList* disabled_extensions =
201 service_weak_->disabled_extensions();
202 if (service_weak_->show_extensions_prompts() &&
203 prompt_for_plugins_ &&
204 !extension->plugins().empty() &&
205 std::find(disabled_extensions->begin(),
206 disabled_extensions->end(),
207 extension) !=
208 disabled_extensions->end()) {
209 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
210 service_weak_->profile(),
211 service_weak_,
212 extension);
213 prompt->ShowPrompt();
214 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
215 }
216 // Not from web store.
217 service_weak_->OnExtensionInstalled(extension, false, -1);
218 }
219
220 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698