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

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: finally 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 // static
70 scoped_refptr<UnpackedInstaller> UnpackedInstaller::Create(
71 ExtensionService* extension_service) {
72 return scoped_refptr<UnpackedInstaller>(
73 new UnpackedInstaller(extension_service));
74 }
75
76 UnpackedInstaller::UnpackedInstaller(ExtensionService* extension_service)
77 : service_weak_(extension_service->AsWeakPtr()),
78 prompt_for_plugins_(true) {
79 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 }
81
82 UnpackedInstaller::~UnpackedInstaller() {
83 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
84 BrowserThread::CurrentlyOn(BrowserThread::FILE));
85 }
86
87 void UnpackedInstaller::Load(const FilePath& path_in) {
88 extension_path_ = path_in;
89 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
90 base::Bind(&UnpackedInstaller::GetAbsolutePath, this));
91 }
92
93 void UnpackedInstaller::LoadFromCommandLine(const FilePath& path_in) {
94 if (!service_weak_.get())
95 return;
96 // Load extensions from the command line synchronously to avoid a race
97 // between extension loading and loading an URL from the command line.
98 base::ThreadRestrictions::ScopedAllowIO allow_io;
99
100 extension_path_ = path_in;
101 file_util::AbsolutePath(&extension_path_);
102
103 std::string id = Extension::GenerateIdForPath(extension_path_);
104 bool allow_file_access =
105 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
106 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
107 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
108
109 int flags = Extension::NO_FLAGS;
110 if (allow_file_access)
111 flags |= Extension::ALLOW_FILE_ACCESS;
112 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
113 flags |= Extension::STRICT_ERROR_CHECKS;
114
115 std::string error;
116 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
117 extension_path_,
118 Extension::LOAD,
119 flags,
120 &error));
121
122 if (!extension) {
123 service_weak_->ReportExtensionLoadError(extension_path_, error, true);
124 return;
125 }
126
127 OnLoaded(extension);
128 }
129
130 void UnpackedInstaller::GetAbsolutePath() {
131 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
132
133 file_util::AbsolutePath(&extension_path_);
134
135 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
136 base::Bind(&UnpackedInstaller::CheckExtensionFileAccess, this));
137 }
138
139 void UnpackedInstaller::CheckExtensionFileAccess() {
140 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
141 std::string id = Extension::GenerateIdForPath(extension_path_);
142 // Unpacked extensions default to allowing file access, but if that has been
143 // overridden, don't reset the value.
144 bool allow_file_access =
145 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD);
146 if (service_weak_->extension_prefs()->HasAllowFileAccessSetting(id))
147 allow_file_access = service_weak_->extension_prefs()->AllowFileAccess(id);
148
149 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
150 base::Bind(
151 &UnpackedInstaller::LoadWithFileAccess,
152 this, allow_file_access));
153 }
154
155 void UnpackedInstaller::LoadWithFileAccess(bool allow_file_access) {
156 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
157 int flags = allow_file_access ?
158 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
159 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
160 flags |= Extension::STRICT_ERROR_CHECKS;
161 std::string error;
162 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
163 extension_path_,
164 Extension::LOAD,
165 flags,
166 &error));
167
168 if (!extension) {
169 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
170 base::Bind(
171 &UnpackedInstaller::ReportExtensionLoadError,
172 this, error));
173 return;
174 }
175
176 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
177 base::Bind(
178 &UnpackedInstaller::OnLoaded,
179 this, extension));
180 }
181
182 void UnpackedInstaller::ReportExtensionLoadError(const std::string &error) {
183 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
184 if (!service_weak_.get())
185 return;
186 service_weak_->ReportExtensionLoadError(extension_path_, error, true);
187 }
188
189 void UnpackedInstaller::OnLoaded(
190 const scoped_refptr<const Extension>& extension) {
191 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
192 if (!service_weak_.get())
193 return;
194 const ExtensionList* disabled_extensions =
195 service_weak_->disabled_extensions();
196 if (service_weak_->show_extensions_prompts() &&
197 prompt_for_plugins_ &&
198 !extension->plugins().empty() &&
199 std::find(disabled_extensions->begin(),
200 disabled_extensions->end(),
201 extension) !=
202 disabled_extensions->end()) {
203 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
204 service_weak_->profile(),
205 service_weak_,
206 extension);
207 prompt->ShowPrompt();
208 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
209 }
210 service_weak_->OnExtensionInstalled(extension,
211 false, // Not from web store.
212 -1);
213 }
214
215 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/unpacked_installer.h ('k') | chrome/browser/extensions/user_script_listener_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698