OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_path.h" | 10 #include "base/file_path.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "chrome/common/extensions/message_bundle.h" | 28 #include "chrome/common/extensions/message_bundle.h" |
29 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
30 #include "net/base/escape.h" | 30 #include "net/base/escape.h" |
31 #include "net/base/file_stream.h" | 31 #include "net/base/file_stream.h" |
32 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
33 | 33 |
34 using extensions::Extension; | 34 using extensions::Extension; |
35 | 35 |
36 namespace errors = extension_manifest_errors; | 36 namespace errors = extension_manifest_errors; |
37 | 37 |
| 38 namespace { |
| 39 |
| 40 bool ValidateExtensionIconSet(const ExtensionIconSet* icon_set, |
| 41 const Extension* extension, |
| 42 int error_message_id, |
| 43 std::string* error) { |
| 44 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set->map().begin(); |
| 45 iter != icon_set->map().end(); |
| 46 ++iter) { |
| 47 const FilePath path = extension->GetResource(iter->second).GetFilePath(); |
| 48 if (!extension_file_util::ValidateFilePath(path)) { |
| 49 *error = l10n_util::GetStringFUTF8(error_message_id, |
| 50 UTF8ToUTF16(iter->second)); |
| 51 return false; |
| 52 } |
| 53 } |
| 54 return true; |
| 55 } |
| 56 |
| 57 bool ValidateExtensionActionIcons(const Extension* extension, |
| 58 const ExtensionAction* action, |
| 59 int error_message_id, |
| 60 std::string* error) { |
| 61 if (!action) |
| 62 return true; |
| 63 |
| 64 // Validate default icon set. |
| 65 const ExtensionIconSet* default_icon = action->default_icon(); |
| 66 // Default icon may not be set. |
| 67 if (default_icon && |
| 68 !ValidateExtensionIconSet(default_icon, extension, error_message_id, |
| 69 error)) { |
| 70 return false; |
| 71 } |
| 72 |
| 73 // Validate page action icons's icon sets. |
| 74 const std::vector<const ExtensionIconSet*>& page_action_icons = |
| 75 action->page_action_icons(); |
| 76 for (size_t icon = 0; icon < page_action_icons.size(); ++icon) { |
| 77 const ExtensionIconSet* icon_set = page_action_icons[icon]; |
| 78 if (!ValidateExtensionIconSet(icon_set, extension, error_message_id, error)) |
| 79 return false; |
| 80 } |
| 81 return true; |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
38 namespace extension_file_util { | 86 namespace extension_file_util { |
39 | 87 |
40 // Validates locale info. Doesn't check if messages.json files are valid. | 88 // Validates locale info. Doesn't check if messages.json files are valid. |
41 static bool ValidateLocaleInfo(const Extension& extension, | 89 static bool ValidateLocaleInfo(const Extension& extension, |
42 std::string* error); | 90 std::string* error); |
43 | 91 |
44 // Returns false and sets the error if script file can't be loaded, | 92 // Returns false and sets the error if script file can't be loaded, |
45 // or if it's not UTF-8 encoded. | 93 // or if it's not UTF-8 encoded. |
46 static bool IsScriptValid(const FilePath& path, const FilePath& relative_path, | 94 static bool IsScriptValid(const FilePath& path, const FilePath& relative_path, |
47 int message_id, std::string* error); | 95 int message_id, std::string* error); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 const Extension::PluginInfo& plugin = extension->plugins()[i]; | 361 const Extension::PluginInfo& plugin = extension->plugins()[i]; |
314 if (!file_util::PathExists(plugin.path)) { | 362 if (!file_util::PathExists(plugin.path)) { |
315 *error = | 363 *error = |
316 l10n_util::GetStringFUTF8( | 364 l10n_util::GetStringFUTF8( |
317 IDS_EXTENSION_LOAD_PLUGIN_PATH_FAILED, | 365 IDS_EXTENSION_LOAD_PLUGIN_PATH_FAILED, |
318 plugin.path.LossyDisplayName()); | 366 plugin.path.LossyDisplayName()); |
319 return false; | 367 return false; |
320 } | 368 } |
321 } | 369 } |
322 | 370 |
323 // Validate icon location and icon file size for page actions. | 371 if (!ValidateExtensionActionIcons(extension, extension->page_action(), |
324 ExtensionAction* page_action = extension->page_action(); | 372 IDS_EXTENSION_LOAD_ICON_FOR_PAGE_ACTION_FAILED, error)) { |
325 if (page_action) { | 373 return false; |
326 std::vector<std::string> icon_paths(*page_action->icon_paths()); | |
327 if (!page_action->default_icon_path().empty()) | |
328 icon_paths.push_back(page_action->default_icon_path()); | |
329 for (std::vector<std::string>::iterator iter = icon_paths.begin(); | |
330 iter != icon_paths.end(); ++iter) { | |
331 const FilePath path = extension->GetResource(*iter).GetFilePath(); | |
332 if (!ValidateFilePath(path)) { | |
333 *error = | |
334 l10n_util::GetStringFUTF8( | |
335 IDS_EXTENSION_LOAD_ICON_FOR_PAGE_ACTION_FAILED, | |
336 UTF8ToUTF16(*iter)); | |
337 return false; | |
338 } | |
339 } | |
340 } | 374 } |
341 | 375 |
342 // Validate icon location and icon file size for browser actions. | 376 if (!ValidateExtensionActionIcons(extension, extension->browser_action(), |
343 // Note: browser actions don't use the icon_paths(). | 377 IDS_EXTENSION_LOAD_ICON_FOR_BROWSER_ACTION_FAILED, error)) { |
344 ExtensionAction* browser_action = extension->browser_action(); | 378 return false; |
345 if (browser_action) { | |
346 std::string path = browser_action->default_icon_path(); | |
347 if (!path.empty()) { | |
348 const FilePath file_path = extension->GetResource(path).GetFilePath(); | |
349 if (!ValidateFilePath(file_path)) { | |
350 *error = | |
351 l10n_util::GetStringFUTF8( | |
352 IDS_EXTENSION_LOAD_ICON_FOR_BROWSER_ACTION_FAILED, | |
353 UTF8ToUTF16(path)); | |
354 return false; | |
355 } | |
356 } | |
357 } | 379 } |
358 | 380 |
359 // Validate that background scripts exist. | 381 // Validate that background scripts exist. |
360 for (size_t i = 0; i < extension->background_scripts().size(); ++i) { | 382 for (size_t i = 0; i < extension->background_scripts().size(); ++i) { |
361 if (!file_util::PathExists( | 383 if (!file_util::PathExists( |
362 extension->GetResource( | 384 extension->GetResource( |
363 extension->background_scripts()[i]).GetFilePath())) { | 385 extension->background_scripts()[i]).GetFilePath())) { |
364 *error = l10n_util::GetStringFUTF8( | 386 *error = l10n_util::GetStringFUTF8( |
365 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, | 387 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, |
366 UTF8ToUTF16(extension->background_scripts()[i])); | 388 UTF8ToUTF16(extension->background_scripts()[i])); |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 return temp_path; | 808 return temp_path; |
787 | 809 |
788 return FilePath(); | 810 return FilePath(); |
789 } | 811 } |
790 | 812 |
791 void DeleteFile(const FilePath& path, bool recursive) { | 813 void DeleteFile(const FilePath& path, bool recursive) { |
792 file_util::Delete(path, recursive); | 814 file_util::Delete(path, recursive); |
793 } | 815 } |
794 | 816 |
795 } // namespace extension_file_util | 817 } // namespace extension_file_util |
OLD | NEW |