OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/extensions/extension_loader_handler.h" | 5 #include "chrome/browser/ui/webui/extensions/extension_loader_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | |
16 #include "chrome/browser/extensions/unpacked_installer.h" | 15 #include "chrome/browser/extensions/unpacked_installer.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/chrome_select_file_policy.h" | 17 #include "chrome/browser/ui/chrome_select_file_policy.h" |
19 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/user_metrics.h" | 19 #include "content/public/browser/user_metrics.h" |
21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
22 #include "content/public/browser/web_ui.h" | 21 #include "content/public/browser/web_ui.h" |
23 #include "content/public/browser/web_ui_data_source.h" | 22 #include "content/public/browser/web_ui_data_source.h" |
24 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/file_highlighter.h" | 24 #include "extensions/browser/file_highlighter.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 } | 123 } |
125 | 124 |
126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( | 125 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( |
127 const std::vector<base::FilePath>& files, void* params) { | 126 const std::vector<base::FilePath>& files, void* params) { |
128 NOTREACHED(); | 127 NOTREACHED(); |
129 } | 128 } |
130 | 129 |
131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) | 130 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) |
132 : profile_(profile), | 131 : profile_(profile), |
133 file_helper_(new FileHelper(this)), | 132 file_helper_(new FileHelper(this)), |
133 extension_error_reporter_observer_(this), | |
134 display_ready_(false), | |
134 weak_ptr_factory_(this) { | 135 weak_ptr_factory_(this) { |
135 DCHECK(profile_); | 136 DCHECK(profile_); |
137 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance()); | |
136 } | 138 } |
137 | 139 |
138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { | 140 ExtensionLoaderHandler::~ExtensionLoaderHandler() { |
139 } | 141 } |
140 | 142 |
141 void ExtensionLoaderHandler::GetLocalizedValues( | 143 void ExtensionLoaderHandler::GetLocalizedValues( |
142 content::WebUIDataSource* source) { | 144 content::WebUIDataSource* source) { |
143 source->AddString( | 145 source->AddString( |
144 "extensionLoadErrorHeading", | 146 "extensionLoadErrorHeading", |
145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); | 147 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); |
146 source->AddString( | 148 source->AddString( |
147 "extensionLoadErrorMessage", | 149 "extensionLoadErrorMessage", |
148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); | 150 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); |
149 source->AddString( | 151 source->AddString( |
150 "extensionLoadErrorRetry", | 152 "extensionLoadErrorRetry", |
151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); | 153 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); |
152 source->AddString( | 154 source->AddString( |
153 "extensionLoadErrorGiveUp", | 155 "extensionLoadErrorGiveUp", |
154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); | 156 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); |
155 source->AddString( | 157 source->AddString( |
156 "extensionLoadCouldNotLoadManifest", | 158 "extensionLoadCouldNotLoadManifest", |
157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); | 159 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); |
160 source->AddString( | |
161 "extensionLoadAdditionalFailures", | |
162 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ADDITIONAL_FAILURES)); | |
158 } | 163 } |
159 | 164 |
160 void ExtensionLoaderHandler::RegisterMessages() { | 165 void ExtensionLoaderHandler::RegisterMessages() { |
161 web_ui()->RegisterMessageCallback( | 166 web_ui()->RegisterMessageCallback( |
162 "extensionLoaderLoadUnpacked", | 167 "extensionLoaderLoadUnpacked", |
163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, | 168 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, |
164 weak_ptr_factory_.GetWeakPtr())); | 169 weak_ptr_factory_.GetWeakPtr())); |
165 web_ui()->RegisterMessageCallback( | 170 web_ui()->RegisterMessageCallback( |
166 "extensionLoaderRetry", | 171 "extensionLoaderRetry", |
167 base::Bind(&ExtensionLoaderHandler::HandleRetry, | 172 base::Bind(&ExtensionLoaderHandler::HandleRetry, |
168 weak_ptr_factory_.GetWeakPtr())); | 173 weak_ptr_factory_.GetWeakPtr())); |
174 web_ui()->RegisterMessageCallback( | |
175 "extensionLoaderIgnoreFailure", | |
176 base::Bind(&ExtensionLoaderHandler::HandleIgnoreFailure, | |
177 weak_ptr_factory_.GetWeakPtr())); | |
178 web_ui()->RegisterMessageCallback( | |
179 "extensionLoaderSetDisplayLoading", | |
180 base::Bind(&ExtensionLoaderHandler::HandleSetDisplayLoading, | |
181 weak_ptr_factory_.GetWeakPtr())); | |
182 web_ui()->RegisterMessageCallback( | |
183 "extensionLoaderDisplayFailures", | |
184 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures, | |
185 weak_ptr_factory_.GetWeakPtr())); | |
169 } | 186 } |
170 | 187 |
171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { | 188 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { |
172 DCHECK(args->empty()); | 189 DCHECK(args->empty()); |
173 file_helper_->ChooseFile(); | 190 file_helper_->ChooseFile(); |
174 } | 191 } |
175 | 192 |
176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { | 193 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { |
177 DCHECK(args->empty()); | 194 DCHECK(args->empty()); |
178 LoadUnpackedExtensionImpl(failed_path_); | 195 LoadUnpackedExtensionImpl(failed_paths_.back()); |
Devlin
2014/07/07 20:44:22
We should probably pop, then run.
gpdavis
2014/07/09 01:35:57
But pop_back deletes the element and doesn't retur
Devlin
2014/07/09 19:53:17
Right, so because pop_back doesn't return it (one
gpdavis
2014/07/09 21:16:25
Done.
| |
196 failed_paths_.pop_back(); | |
197 } | |
198 | |
199 void ExtensionLoaderHandler::HandleIgnoreFailure(const base::ListValue* args) { | |
200 DCHECK(args->empty()); | |
201 failed_paths_.pop_back(); | |
202 } | |
203 | |
204 void ExtensionLoaderHandler::HandleSetDisplayLoading( | |
205 const base::ListValue* args) { | |
206 DCHECK(args->empty()); | |
207 display_ready_ = false; | |
208 } | |
209 | |
210 void ExtensionLoaderHandler::HandleDisplayFailures( | |
211 const base::ListValue* args) { | |
212 DCHECK(args->empty()); | |
213 display_ready_ = true; | |
214 | |
215 if (!failures_.empty()) | |
216 NotifyFrontendOfFailure(); | |
179 } | 217 } |
180 | 218 |
181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( | 219 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( |
182 const base::FilePath& file_path) { | 220 const base::FilePath& file_path) { |
183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( | 221 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( |
184 ExtensionSystem::Get(profile_)->extension_service()); | 222 ExtensionSystem::Get(profile_)->extension_service()); |
185 installer->set_on_failure_callback( | |
186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure, | |
187 weak_ptr_factory_.GetWeakPtr())); | |
188 | 223 |
189 // We do our own error handling, so we don't want a load failure to trigger | 224 // We do our own error handling, so we don't want a load failure to trigger |
190 // a dialog. | 225 // a dialog. |
191 installer->set_be_noisy_on_failure(false); | 226 installer->set_be_noisy_on_failure(false); |
192 | 227 |
193 installer->Load(file_path); | 228 installer->Load(file_path); |
194 } | 229 } |
195 | 230 |
196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, | 231 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, |
197 const std::string& error) { | 232 const std::string& error) { |
198 failed_path_ = file_path; | 233 failed_paths_.push_back(file_path); |
199 size_t line = 0u; | 234 size_t line = 0u; |
200 size_t column = 0u; | 235 size_t column = 0u; |
201 std::string regex = | 236 std::string regex = |
202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", | 237 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", |
203 manifest_errors::kManifestParseError); | 238 manifest_errors::kManifestParseError); |
204 // If this was a JSON parse error, we can highlight the exact line with the | 239 // If this was a JSON parse error, we can highlight the exact line with the |
205 // error. Otherwise, we should still display the manifest (for consistency, | 240 // error. Otherwise, we should still display the manifest (for consistency, |
206 // reference, and so that if we ever make this really fancy and add an editor, | 241 // reference, and so that if we ever make this really fancy and add an editor, |
207 // it's ready). | 242 // it's ready). |
208 // | 243 // |
209 // This regex call can fail, but if it does, we just don't highlight anything. | 244 // This regex call can fail, but if it does, we just don't highlight anything. |
210 re2::RE2::FullMatch(error, regex, &line, &column); | 245 re2::RE2::FullMatch(error, regex, &line, &column); |
211 | 246 |
212 // This will read the manifest and call NotifyFrontendOfFailure with the read | 247 // This will read the manifest and call NotifyFrontendOfFailure with the read |
213 // manifest contents. | 248 // manifest contents. |
214 base::PostTaskAndReplyWithResult( | 249 base::PostTaskAndReplyWithResult( |
215 content::BrowserThread::GetBlockingPool(), | 250 content::BrowserThread::GetBlockingPool(), |
216 FROM_HERE, | 251 FROM_HERE, |
217 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), | 252 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), |
218 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, | 253 base::Bind(&ExtensionLoaderHandler::AddFailure, |
219 weak_ptr_factory_.GetWeakPtr(), | 254 weak_ptr_factory_.GetWeakPtr(), |
220 file_path, | 255 file_path, |
221 error, | 256 error, |
222 line)); | 257 line)); |
223 } | 258 } |
224 | 259 |
225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( | 260 void ExtensionLoaderHandler::AddFailure( |
226 const base::FilePath& file_path, | 261 const base::FilePath& file_path, |
227 const std::string& error, | 262 const std::string& error, |
228 size_t line_number, | 263 size_t line_number, |
229 const std::string& manifest) { | 264 const std::string& manifest) { |
230 base::StringValue file_value(file_path.LossyDisplayName()); | 265 base::StringValue* file_value = |
Devlin
2014/07/07 20:44:21
inline these.
gpdavis
2014/07/09 01:35:57
Done.
| |
231 base::StringValue error_value(base::UTF8ToUTF16(error)); | 266 new base::StringValue(file_path.LossyDisplayName()); |
267 base::StringValue* error_value = | |
268 new base::StringValue(base::UTF8ToUTF16(error)); | |
232 | 269 |
233 base::DictionaryValue manifest_value; | 270 base::DictionaryValue* manifest_value = new base::DictionaryValue(); |
234 SourceHighlighter highlighter(manifest, line_number); | 271 SourceHighlighter highlighter(manifest, line_number); |
235 // If the line number is 0, this highlights no regions, but still adds the | 272 // If the line number is 0, this highlights no regions, but still adds the |
236 // full manifest. | 273 // full manifest. |
237 highlighter.SetHighlightedRegions(&manifest_value); | 274 highlighter.SetHighlightedRegions(manifest_value); |
238 | 275 |
276 base::DictionaryValue* failure = new base::DictionaryValue(); | |
Devlin
2014/07/07 20:44:21
For better or worse, we're overly paranoid about m
gpdavis
2014/07/09 01:35:57
You can never be overly paranoid about memory leak
Devlin
2014/07/09 19:53:17
Right, but we like extremely clear ownership model
gpdavis
2014/07/09 21:16:25
Ah, gotcha. I must have been doing something wron
| |
277 failure->Set("path", file_value); | |
278 failure->Set("error", error_value); | |
279 failure->Set("manifest", manifest_value); | |
280 failures_.Append(failure); | |
281 | |
282 // Only notify the frontend if the display is ready. | |
283 if (display_ready_) { | |
Devlin
2014/07/07 20:44:21
nit: no brackets around single-line ifs.
gpdavis
2014/07/09 01:35:57
Done.
| |
284 NotifyFrontendOfFailure(); | |
285 } | |
286 } | |
287 | |
288 void ExtensionLoaderHandler::NotifyFrontendOfFailure() { | |
239 web_ui()->CallJavascriptFunction( | 289 web_ui()->CallJavascriptFunction( |
240 "extensions.ExtensionLoader.notifyLoadFailed", | 290 "extensions.ExtensionLoader.notifyLoadFailed", |
241 file_value, | 291 failures_); |
242 error_value, | 292 failures_.Clear(); |
243 manifest_value); | |
244 } | 293 } |
245 | 294 |
246 } // namespace extensions | 295 } // namespace extensions |
OLD | NEW |