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

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: 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 {
Aaron Boodman 2011/10/28 22:21:58 I think that we can remove this. I believe the onl
Matt Perry 2011/10/28 22:28:55 Actually, we show prompts for the first unpacked-l
Yoyo Zhou 2011/10/31 21:58:15 prompt_for_plugins is supposed to be true by defau
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 UnpackedInstaller::UnpackedInstaller(
68 base::WeakPtr<ExtensionService> extension_service)
69 : service_weak_(extension_service) {
70 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 }
72
73 UnpackedInstaller::~UnpackedInstaller() {
74 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
75 BrowserThread::CurrentlyOn(BrowserThread::FILE));
76 }
77
78 void UnpackedInstaller::Load(const FilePath& extension_path) {
79 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
80 base::Bind(&UnpackedInstaller::LoadSingleExtension,
81 this, extension_path));
82 }
83
84 void UnpackedInstaller::LoadFromCommandLine(
Aaron Boodman 2011/10/28 22:21:58 This only shares OnLoadSingleExtension with the re
Yoyo Zhou 2011/10/31 21:58:15 This actually does exactly the same thing as the r
Aaron Boodman 2011/10/31 22:33:25 What I meant is that it doesn't rely on anything e
85 const FilePath& path_in) {
86 if (!service_weak_.get())
87 return;
88 // Load extensions from the command line synchronously to avoid a race
89 // between extension loading and loading an URL from the command line.
90 base::ThreadRestrictions::ScopedAllowIO allow_io;
91
92 FilePath extension_path = path_in;
93 file_util::AbsolutePath(&extension_path);
94
95 std::string id = Extension::GenerateIdForPath(extension_path);
96 bool allow_file_access =
97 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
98 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
99 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
100
101 int flags = Extension::NO_FLAGS;
102 if (allow_file_access)
103 flags |= Extension::ALLOW_FILE_ACCESS;
104 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
105 flags |= Extension::STRICT_ERROR_CHECKS;
106
107 std::string error;
108 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
109 extension_path,
110 Extension::LOAD,
111 flags,
112 &error));
113
114 if (!extension) {
115 service_weak_->ReportExtensionLoadError(extension_path, error, true);
116 return;
117 }
118
119 OnLoadSingleExtension(extension);
120 }
121
122 void UnpackedInstaller::LoadSingleExtension(const FilePath& extension_path) {
123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
124
125 FilePath absolute_path = extension_path;
126 file_util::AbsolutePath(&absolute_path);
127
128 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
129 base::Bind(&UnpackedInstaller::CheckExtensionFileAccess,
Aaron Boodman 2011/10/28 22:21:58 Add a TODO that it would be nice to remove this ex
Yoyo Zhou 2011/10/31 21:58:15 Done.
130 this, absolute_path));
131 }
132
133 void UnpackedInstaller::CheckExtensionFileAccess(
134 const FilePath& extension_path) {
135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136 std::string id = Extension::GenerateIdForPath(extension_path);
137 // Unpacked extensions default to allowing file access, but if that has been
138 // overridden, don't reset the value.
139 bool allow_file_access =
140 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
141 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
142 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
143
144 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
145 base::Bind(
146 &UnpackedInstaller::LoadSingleExtensionWithFileAccess,
147 this, extension_path, allow_file_access));
148 }
149
150 void UnpackedInstaller::LoadSingleExtensionWithFileAccess(
151 const FilePath& extension_path,
152 bool allow_file_access) {
153 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
154 int flags = allow_file_access ?
155 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
156 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
157 flags |= Extension::STRICT_ERROR_CHECKS;
158 std::string error;
159 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
160 extension_path,
161 Extension::LOAD,
162 flags,
163 &error));
164
165 if (!extension) {
166 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
167 base::Bind(
168 &UnpackedInstaller::ReportExtensionLoadError,
169 this,
170 extension_path, error));
171 return;
172 }
173
174 // Report this as an installed extension so that it gets remembered in the
Aaron Boodman 2011/10/28 22:21:58 Comment is no longer needed since it is obvious fr
Yoyo Zhou 2011/10/31 21:58:15 Done.
175 // prefs.
176 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
177 base::Bind(
178 &UnpackedInstaller::OnLoadSingleExtension,
179 this, extension));
180 }
181
182 void UnpackedInstaller::ReportExtensionLoadError(
183 const FilePath& extension_path, const std::string &error) {
184 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
185 if (!service_weak_.get())
186 return;
187 service_weak_->ReportExtensionLoadError(extension_path, error, true);
188 }
189
190 void UnpackedInstaller::OnLoadSingleExtension(
191 const scoped_refptr<const Extension>& extension) {
192 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
193 if (!service_weak_.get())
194 return;
195 const ExtensionList* disabled_extensions =
196 service_weak_->disabled_extensions();
197 if (service_weak_->show_extensions_prompts() &&
198 prompt_for_plugins_ &&
199 !extension->plugins().empty() &&
200 std::find(disabled_extensions->begin(),
201 disabled_extensions->end(),
202 extension) !=
203 disabled_extensions->end()) {
204 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
205 service_weak_->profile(),
206 service_weak_,
207 extension);
208 prompt->ShowPrompt();
209 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
210 }
211 // Not from web store.
Aaron Boodman 2011/10/28 22:21:58 Is this comment still legit? I don't see how it re
Yoyo Zhou 2011/10/31 21:58:15 I believe the comment is about the 'false' paramet
Aaron Boodman 2011/10/31 22:33:25 Ah, can you format it like this then: extension,
Yoyo Zhou 2011/11/01 21:50:57 Done.
212 service_weak_->OnExtensionInstalled(extension, false, -1);
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698