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" | 15 #include "chrome/browser/extensions/path_util.h" |
16 #include "chrome/browser/extensions/unpacked_installer.h" | 16 #include "chrome/browser/extensions/unpacked_installer.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/chrome_select_file_policy.h" | 18 #include "chrome/browser/ui/chrome_select_file_policy.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/user_metrics.h" | 20 #include "content/public/browser/user_metrics.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
23 #include "content/public/browser/web_ui_data_source.h" | 23 #include "content/public/browser/web_ui_data_source.h" |
24 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/file_highlighter.h" | 25 #include "extensions/browser/file_highlighter.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 124 } |
125 | 125 |
126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( | 126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( |
127 const std::vector<base::FilePath>& files, void* params) { | 127 const std::vector<base::FilePath>& files, void* params) { |
128 NOTREACHED(); | 128 NOTREACHED(); |
129 } | 129 } |
130 | 130 |
131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) | 131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) |
132 : profile_(profile), | 132 : profile_(profile), |
133 file_helper_(new FileHelper(this)), | 133 file_helper_(new FileHelper(this)), |
| 134 extension_error_reporter_observer_(this), |
| 135 ui_ready_(false), |
134 weak_ptr_factory_(this) { | 136 weak_ptr_factory_(this) { |
135 DCHECK(profile_); | 137 DCHECK(profile_); |
| 138 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance()); |
136 } | 139 } |
137 | 140 |
138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { | 141 ExtensionLoaderHandler::~ExtensionLoaderHandler() { |
139 } | 142 } |
140 | 143 |
141 void ExtensionLoaderHandler::GetLocalizedValues( | 144 void ExtensionLoaderHandler::GetLocalizedValues( |
142 content::WebUIDataSource* source) { | 145 content::WebUIDataSource* source) { |
143 source->AddString( | 146 source->AddString( |
144 "extensionLoadErrorHeading", | 147 "extensionLoadErrorHeading", |
145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); | 148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); |
146 source->AddString( | 149 source->AddString( |
147 "extensionLoadErrorMessage", | 150 "extensionLoadErrorMessage", |
148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); | 151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); |
149 source->AddString( | 152 source->AddString( |
150 "extensionLoadErrorRetry", | 153 "extensionLoadErrorRetry", |
151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); | 154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); |
152 source->AddString( | 155 source->AddString( |
153 "extensionLoadErrorGiveUp", | 156 "extensionLoadErrorGiveUp", |
154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); | 157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); |
155 source->AddString( | 158 source->AddString( |
156 "extensionLoadCouldNotLoadManifest", | 159 "extensionLoadCouldNotLoadManifest", |
157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); | 160 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); |
| 161 source->AddString( |
| 162 "extensionLoadAdditionalFailures", |
| 163 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ADDITIONAL_FAILURES)); |
158 } | 164 } |
159 | 165 |
160 void ExtensionLoaderHandler::RegisterMessages() { | 166 void ExtensionLoaderHandler::RegisterMessages() { |
| 167 // We observe WebContents in order to detect page refreshes, since notifying |
| 168 // the frontend of load failures must be delayed until the page finishes |
| 169 // loading. We never call Observe(NULL) because this object is constructed |
| 170 // on page load and persists between refreshes. |
| 171 content::WebContentsObserver::Observe(web_ui()->GetWebContents()); |
| 172 |
161 web_ui()->RegisterMessageCallback( | 173 web_ui()->RegisterMessageCallback( |
162 "extensionLoaderLoadUnpacked", | 174 "extensionLoaderLoadUnpacked", |
163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, | 175 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, |
164 weak_ptr_factory_.GetWeakPtr())); | 176 weak_ptr_factory_.GetWeakPtr())); |
165 web_ui()->RegisterMessageCallback( | 177 web_ui()->RegisterMessageCallback( |
166 "extensionLoaderRetry", | 178 "extensionLoaderRetry", |
167 base::Bind(&ExtensionLoaderHandler::HandleRetry, | 179 base::Bind(&ExtensionLoaderHandler::HandleRetry, |
168 weak_ptr_factory_.GetWeakPtr())); | 180 weak_ptr_factory_.GetWeakPtr())); |
| 181 web_ui()->RegisterMessageCallback( |
| 182 "extensionLoaderIgnoreFailure", |
| 183 base::Bind(&ExtensionLoaderHandler::HandleIgnoreFailure, |
| 184 weak_ptr_factory_.GetWeakPtr())); |
| 185 web_ui()->RegisterMessageCallback( |
| 186 "extensionLoaderDisplayFailures", |
| 187 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures, |
| 188 weak_ptr_factory_.GetWeakPtr())); |
169 } | 189 } |
170 | 190 |
171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { | 191 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { |
172 DCHECK(args->empty()); | 192 DCHECK(args->empty()); |
173 file_helper_->ChooseFile(); | 193 file_helper_->ChooseFile(); |
174 } | 194 } |
175 | 195 |
176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { | 196 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { |
177 DCHECK(args->empty()); | 197 DCHECK(args->empty()); |
178 LoadUnpackedExtensionImpl(failed_path_); | 198 const base::FilePath file_path = failed_paths_.back(); |
| 199 failed_paths_.pop_back(); |
| 200 LoadUnpackedExtensionImpl(file_path); |
| 201 } |
| 202 |
| 203 void ExtensionLoaderHandler::HandleIgnoreFailure(const base::ListValue* args) { |
| 204 DCHECK(args->empty()); |
| 205 failed_paths_.pop_back(); |
| 206 } |
| 207 |
| 208 void ExtensionLoaderHandler::HandleDisplayFailures( |
| 209 const base::ListValue* args) { |
| 210 DCHECK(args->empty()); |
| 211 ui_ready_ = true; |
| 212 |
| 213 // Notify the frontend of any load failures that were triggered while the |
| 214 // chrome://extensions page was loading. |
| 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; | |
199 size_t line = 0u; | 233 size_t line = 0u; |
200 size_t column = 0u; | 234 size_t column = 0u; |
201 std::string regex = | 235 std::string regex = |
202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", | 236 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", |
203 manifest_errors::kManifestParseError); | 237 manifest_errors::kManifestParseError); |
204 // If this was a JSON parse error, we can highlight the exact line with the | 238 // 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, | 239 // 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, | 240 // reference, and so that if we ever make this really fancy and add an editor, |
207 // it's ready). | 241 // it's ready). |
208 // | 242 // |
209 // This regex call can fail, but if it does, we just don't highlight anything. | 243 // This regex call can fail, but if it does, we just don't highlight anything. |
210 re2::RE2::FullMatch(error, regex, &line, &column); | 244 re2::RE2::FullMatch(error, regex, &line, &column); |
211 | 245 |
212 // This will read the manifest and call NotifyFrontendOfFailure with the read | 246 // This will read the manifest and call AddFailure with the read manifest |
213 // manifest contents. | 247 // contents. |
214 base::PostTaskAndReplyWithResult( | 248 base::PostTaskAndReplyWithResult( |
215 content::BrowserThread::GetBlockingPool(), | 249 content::BrowserThread::GetBlockingPool(), |
216 FROM_HERE, | 250 FROM_HERE, |
217 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), | 251 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), |
218 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, | 252 base::Bind(&ExtensionLoaderHandler::AddFailure, |
219 weak_ptr_factory_.GetWeakPtr(), | 253 weak_ptr_factory_.GetWeakPtr(), |
220 file_path, | 254 file_path, |
221 error, | 255 error, |
222 line)); | 256 line)); |
223 } | 257 } |
224 | 258 |
225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( | 259 void ExtensionLoaderHandler::DidStartNavigationToPendingEntry( |
| 260 const GURL& url, |
| 261 content::NavigationController::ReloadType reload_type) { |
| 262 // In the event of a page reload, we ensure that the frontend is not notified |
| 263 // until the UI finishes loading, so we set |ui_ready_| to false. This is |
| 264 // balanced in HandleDisplayFailures, which is called when the frontend is |
| 265 // ready to receive failure notifications. |
| 266 if (reload_type != content::NavigationController::NO_RELOAD) |
| 267 ui_ready_ = false; |
| 268 } |
| 269 |
| 270 void ExtensionLoaderHandler::AddFailure( |
226 const base::FilePath& file_path, | 271 const base::FilePath& file_path, |
227 const std::string& error, | 272 const std::string& error, |
228 size_t line_number, | 273 size_t line_number, |
229 const std::string& manifest) { | 274 const std::string& manifest) { |
230 base::StringValue file_value(file_path.LossyDisplayName()); | 275 failed_paths_.push_back(file_path); |
231 base::StringValue error_value(base::UTF8ToUTF16(error)); | 276 base::FilePath prettified_path = path_util::PrettifyPath(file_path); |
232 | 277 |
233 base::DictionaryValue manifest_value; | 278 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue()); |
234 SourceHighlighter highlighter(manifest, line_number); | 279 SourceHighlighter highlighter(manifest, line_number); |
235 // If the line number is 0, this highlights no regions, but still adds the | 280 // If the line number is 0, this highlights no regions, but still adds the |
236 // full manifest. | 281 // full manifest. |
237 highlighter.SetHighlightedRegions(&manifest_value); | 282 highlighter.SetHighlightedRegions(manifest_value.get()); |
238 | 283 |
| 284 scoped_ptr<base::DictionaryValue> failure(new base::DictionaryValue()); |
| 285 failure->Set("path", |
| 286 new base::StringValue(prettified_path.LossyDisplayName())); |
| 287 failure->Set("error", new base::StringValue(base::UTF8ToUTF16(error))); |
| 288 failure->Set("manifest", manifest_value.release()); |
| 289 failures_.Append(failure.release()); |
| 290 |
| 291 // Only notify the frontend if the frontend UI is ready. |
| 292 if (ui_ready_) |
| 293 NotifyFrontendOfFailure(); |
| 294 } |
| 295 |
| 296 void ExtensionLoaderHandler::NotifyFrontendOfFailure() { |
239 web_ui()->CallJavascriptFunction( | 297 web_ui()->CallJavascriptFunction( |
240 "extensions.ExtensionLoader.notifyLoadFailed", | 298 "extensions.ExtensionLoader.notifyLoadFailed", |
241 file_value, | 299 failures_); |
242 error_value, | 300 failures_.Clear(); |
243 manifest_value); | |
244 } | 301 } |
245 | 302 |
246 } // namespace extensions | 303 } // namespace extensions |
OLD | NEW |