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

Side by Side Diff: chrome/common/extensions/extension_file_util.cc

Issue 11724002: Move ContentScripts out of Extension (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_browser_action
Patch Set: Created 7 years, 9 months 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/common/extensions/extension_file_util.h" 5 #include "chrome/common/extensions/extension_file_util.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 29 matching lines...) Expand all
40 namespace errors = extension_manifest_errors; 40 namespace errors = extension_manifest_errors;
41 41
42 namespace { 42 namespace {
43 43
44 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); 44 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp");
45 45
46 } // namespace 46 } // namespace
47 47
48 namespace extension_file_util { 48 namespace extension_file_util {
49 49
50 // Returns false and sets the error if script file can't be loaded,
51 // or if it's not UTF-8 encoded.
52 static bool IsScriptValid(const base::FilePath& path,
53 const base::FilePath& relative_path,
54 int message_id, std::string* error);
55
56 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, 50 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir,
57 const std::string& id, 51 const std::string& id,
58 const std::string& version, 52 const std::string& version,
59 const base::FilePath& extensions_dir) { 53 const base::FilePath& extensions_dir) {
60 base::FilePath extension_dir = extensions_dir.AppendASCII(id); 54 base::FilePath extension_dir = extensions_dir.AppendASCII(id);
61 base::FilePath version_dir; 55 base::FilePath version_dir;
62 56
63 // Create the extension directory if it doesn't exist already. 57 // Create the extension directory if it doesn't exist already.
64 if (!file_util::PathExists(extension_dir)) { 58 if (!file_util::PathExists(extension_dir)) {
65 if (!file_util::CreateDirectory(extension_dir)) 59 if (!file_util::CreateDirectory(extension_dir))
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 239 }
246 240
247 bool ValidateExtension(const Extension* extension, 241 bool ValidateExtension(const Extension* extension,
248 std::string* error, 242 std::string* error,
249 std::vector<extensions::InstallWarning>* warnings) { 243 std::vector<extensions::InstallWarning>* warnings) {
250 // Ask registered manifest handlers to validate their paths. 244 // Ask registered manifest handlers to validate their paths.
251 if (!extensions::ManifestHandler::ValidateExtension( 245 if (!extensions::ManifestHandler::ValidateExtension(
252 extension, error, warnings)) 246 extension, error, warnings))
253 return false; 247 return false;
254 248
255 // TODO(yoz): Move this to content scripts manifest handler.
256 // Validate that claimed script resources actually exist,
257 // and are UTF-8 encoded.
258 ExtensionResource::SymlinkPolicy symlink_policy;
259 if ((extension->creation_flags() &
260 Extension::FOLLOW_SYMLINKS_ANYWHERE) != 0) {
261 symlink_policy = ExtensionResource::FOLLOW_SYMLINKS_ANYWHERE;
262 } else {
263 symlink_policy = ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT;
264 }
265
266 for (size_t i = 0; i < extension->content_scripts().size(); ++i) {
267 const extensions::UserScript& script = extension->content_scripts()[i];
268
269 for (size_t j = 0; j < script.js_scripts().size(); j++) {
270 const extensions::UserScript::File& js_script = script.js_scripts()[j];
271 const base::FilePath& path = ExtensionResource::GetFilePath(
272 js_script.extension_root(), js_script.relative_path(),
273 symlink_policy);
274 if (!IsScriptValid(path, js_script.relative_path(),
275 IDS_EXTENSION_LOAD_JAVASCRIPT_FAILED, error))
276 return false;
277 }
278
279 for (size_t j = 0; j < script.css_scripts().size(); j++) {
280 const extensions::UserScript::File& css_script = script.css_scripts()[j];
281 const base::FilePath& path = ExtensionResource::GetFilePath(
282 css_script.extension_root(), css_script.relative_path(),
283 symlink_policy);
284 if (!IsScriptValid(path, css_script.relative_path(),
285 IDS_EXTENSION_LOAD_CSS_FAILED, error))
286 return false;
287 }
288 }
289
290 // Check children of extension root to see if any of them start with _ and is 249 // Check children of extension root to see if any of them start with _ and is
291 // not on the reserved list. 250 // not on the reserved list.
292 if (!CheckForIllegalFilenames(extension->path(), error)) { 251 if (!CheckForIllegalFilenames(extension->path(), error)) {
293 return false; 252 return false;
294 } 253 }
295 254
296 // Check that extensions don't include private key files. 255 // Check that extensions don't include private key files.
297 std::vector<base::FilePath> private_keys = 256 std::vector<base::FilePath> private_keys =
298 FindPrivateKeyFiles(extension->path()); 257 FindPrivateKeyFiles(extension->path());
299 if (extension->creation_flags() & Extension::ERROR_ON_PRIVATE_KEY) { 258 if (extension->creation_flags() & Extension::ERROR_ON_PRIVATE_KEY) {
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } 403 }
445 404
446 // Add @@extension_id reserved message here, so it's available to 405 // Add @@extension_id reserved message here, so it's available to
447 // non-localized extensions too. 406 // non-localized extensions too.
448 returnValue->insert( 407 returnValue->insert(
449 std::make_pair(extensions::MessageBundle::kExtensionIdKey, extension_id)); 408 std::make_pair(extensions::MessageBundle::kExtensionIdKey, extension_id));
450 409
451 return returnValue; 410 return returnValue;
452 } 411 }
453 412
454 static bool IsScriptValid(const base::FilePath& path,
455 const base::FilePath& relative_path,
456 int message_id,
457 std::string* error) {
458 std::string content;
459 if (!file_util::PathExists(path) ||
460 !file_util::ReadFileToString(path, &content)) {
461 *error = l10n_util::GetStringFUTF8(
462 message_id,
463 relative_path.LossyDisplayName());
464 return false;
465 }
466
467 if (!IsStringUTF8(content)) {
468 *error = l10n_util::GetStringFUTF8(
469 IDS_EXTENSION_BAD_FILE_ENCODING,
470 relative_path.LossyDisplayName());
471 return false;
472 }
473
474 return true;
475 }
476
477 bool CheckForIllegalFilenames(const base::FilePath& extension_path, 413 bool CheckForIllegalFilenames(const base::FilePath& extension_path,
478 std::string* error) { 414 std::string* error) {
479 // Reserved underscore names. 415 // Reserved underscore names.
480 static const base::FilePath::CharType* reserved_names[] = { 416 static const base::FilePath::CharType* reserved_names[] = {
481 Extension::kLocaleFolder, 417 Extension::kLocaleFolder,
482 FILE_PATH_LITERAL("__MACOSX"), 418 FILE_PATH_LITERAL("__MACOSX"),
483 }; 419 };
484 CR_DEFINE_STATIC_LOCAL( 420 CR_DEFINE_STATIC_LOCAL(
485 std::set<base::FilePath::StringType>, reserved_underscore_names, 421 std::set<base::FilePath::StringType>, reserved_underscore_names,
486 (reserved_names, reserved_names + arraysize(reserved_names))); 422 (reserved_names, reserved_names + arraysize(reserved_names)));
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 return base::FilePath(); 528 return base::FilePath();
593 } 529 }
594 return temp_path; 530 return temp_path;
595 } 531 }
596 532
597 void DeleteFile(const base::FilePath& path, bool recursive) { 533 void DeleteFile(const base::FilePath& path, bool recursive) {
598 file_util::Delete(path, recursive); 534 file_util::Delete(path, recursive);
599 } 535 }
600 536
601 } // namespace extension_file_util 537 } // namespace extension_file_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698