Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extensions/extension_file_util.h" | 5 #include "chrome/browser/extensions/extension_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/extensions/extension_l10n_util.h" | |
| 11 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 14 #include "chrome/common/json_value_serializer.h" |
| 14 #include "net/base/file_stream.h" | 15 #include "net/base/file_stream.h" |
| 15 | 16 |
| 16 namespace extension_file_util { | 17 namespace extension_file_util { |
| 17 | 18 |
| 18 const char kInstallDirectoryName[] = "Extensions"; | 19 const char kInstallDirectoryName[] = "Extensions"; |
| 19 const char kCurrentVersionFileName[] = "Current Version"; | 20 const char kCurrentVersionFileName[] = "Current Version"; |
| 20 | 21 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); | 258 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); |
| 258 iter != icon_paths.end(); ++iter) { | 259 iter != icon_paths.end(); ++iter) { |
| 259 if (!file_util::PathExists(extension->GetResourcePath(*iter))) { | 260 if (!file_util::PathExists(extension->GetResourcePath(*iter))) { |
| 260 *error = StringPrintf("Could not load icon '%s' for page action.", | 261 *error = StringPrintf("Could not load icon '%s' for page action.", |
| 261 iter->c_str()); | 262 iter->c_str()); |
| 262 return NULL; | 263 return NULL; |
| 263 } | 264 } |
| 264 } | 265 } |
| 265 } | 266 } |
| 266 | 267 |
| 268 // Load locale information if available. | |
| 269 FilePath locale_path = extension_path.AppendASCII(Extension::kLocaleFolder); | |
| 270 if (file_util::PathExists(locale_path)) { | |
| 271 if (!extension_l10n_util::AddValidLocales(locale_path, | |
| 272 extension.get(), | |
| 273 error)) { | |
| 274 return NULL; | |
| 275 } | |
| 276 | |
| 277 if (!extension_l10n_util::ValidateDefaultLocale(extension.get())) { | |
| 278 *error = extension_manifest_errors::kLocalesNoDefaultLocaleSpecified; | |
| 279 return NULL; | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 // Check children of extension root to see if any of them start with _ and is | |
| 284 // not on the reserved list. | |
| 285 if (!CheckForIllegalFilenames(extension_path, error)) { | |
| 286 return NULL; | |
| 287 } | |
| 288 | |
| 267 return extension.release(); | 289 return extension.release(); |
| 268 } | 290 } |
| 269 | 291 |
| 270 void UninstallExtension(const std::string& id, const FilePath& extensions_dir) { | 292 void UninstallExtension(const std::string& id, const FilePath& extensions_dir) { |
| 271 // First, delete the Current Version file. If the directory delete fails, then | 293 // First, delete the Current Version file. If the directory delete fails, then |
| 272 // at least the extension won't be loaded again. | 294 // at least the extension won't be loaded again. |
| 273 FilePath extension_root = extensions_dir.AppendASCII(id); | 295 FilePath extension_root = extensions_dir.AppendASCII(id); |
| 274 | 296 |
| 275 if (!file_util::PathExists(extension_root)) { | 297 if (!file_util::PathExists(extension_root)) { |
| 276 LOG(WARNING) << "Asked to remove a non-existent extension " << id; | 298 LOG(WARNING) << "Asked to remove a non-existent extension " << id; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 LOG(WARNING) << "Invalid extension ID encountered in extensions " | 352 LOG(WARNING) << "Invalid extension ID encountered in extensions " |
| 331 "directory: " << extension_id; | 353 "directory: " << extension_id; |
| 332 LOG(INFO) << "Deleting invalid extension directory " | 354 LOG(INFO) << "Deleting invalid extension directory " |
| 333 << WideToASCII(extension_path.ToWStringHack()) << "."; | 355 << WideToASCII(extension_path.ToWStringHack()) << "."; |
| 334 file_util::Delete(extension_path, true); // Recursive. | 356 file_util::Delete(extension_path, true); // Recursive. |
| 335 continue; | 357 continue; |
| 336 } | 358 } |
| 337 } | 359 } |
| 338 } | 360 } |
| 339 | 361 |
| 362 bool CheckForIllegalFilenames(const FilePath& extension_path, | |
| 363 std::string* error) { | |
| 364 // Reserved underscore names. | |
| 365 static const char* reserved_names[] = { | |
| 366 Extension::kLocaleFolder | |
| 367 }; | |
| 368 static std::set<std::string> reserved_underscore_names( | |
| 369 reserved_names, reserved_names + arraysize(reserved_names)); | |
| 370 | |
| 371 // Enumerate all files and directories in the extension root. | |
| 372 // There is a problem when using pattern "_*" with FileEnumerator, so we have | |
| 373 // to cheat with find_first_of and match all. | |
| 374 file_util::FileEnumerator all_files( | |
| 375 extension_path, | |
| 376 false, | |
| 377 static_cast<file_util::FileEnumerator::FILE_TYPE>( | |
| 378 file_util::FileEnumerator::DIRECTORIES | | |
| 379 file_util::FileEnumerator::FILES)); | |
| 380 | |
| 381 FilePath files; | |
| 382 while (!(files = all_files.Next()).empty()) { | |
| 383 std::string filename = | |
| 384 WideToASCII(files.BaseName().ToWStringHack()); | |
| 385 // Skip all that don't start with "_". | |
| 386 if (filename.find_first_of("_") != 0) continue; | |
| 387 if (reserved_underscore_names.find(filename) == | |
| 388 reserved_underscore_names.end()) { | |
| 389 *error = StringPrintf("Illegal file/directory name %s", filename); | |
|
Aaron Boodman
2009/08/19 03:00:49
Can you make this error a bit more developer frien
| |
| 390 return false; | |
| 391 } | |
| 392 } | |
| 393 | |
| 394 return true; | |
| 395 } | |
| 396 | |
| 340 } // extensionfile_util | 397 } // extensionfile_util |
| OLD | NEW |