OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_unpacker.h" | 5 #include "chrome/common/extensions/extension_unpacker.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/scoped_handle.h" | 9 #include "base/scoped_handle.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 const char* kCouldNotCreateDirectoryError = | 37 const char* kCouldNotCreateDirectoryError = |
38 "Could not create directory for unzipping: "; | 38 "Could not create directory for unzipping: "; |
39 const char* kCouldNotDecodeImageError = "Could not decode theme image."; | 39 const char* kCouldNotDecodeImageError = "Could not decode theme image."; |
40 const char* kCouldNotUnzipExtension = "Could not unzip extension."; | 40 const char* kCouldNotUnzipExtension = "Could not unzip extension."; |
41 const char* kPathNamesMustBeAbsoluteOrLocalError = | 41 const char* kPathNamesMustBeAbsoluteOrLocalError = |
42 "Path names must not be absolute or contain '..'."; | 42 "Path names must not be absolute or contain '..'."; |
43 | 43 |
44 // A limit to stop us passing dangerously large canvases to the browser. | 44 // A limit to stop us passing dangerously large canvases to the browser. |
45 const int kMaxImageCanvas = 4096 * 4096; | 45 const int kMaxImageCanvas = 4096 * 4096; |
46 | 46 |
47 } // namespace | 47 SkBitmap DecodeImage(const FilePath& path) { |
48 | |
49 static SkBitmap DecodeImage(const FilePath& path) { | |
50 // Read the file from disk. | 48 // Read the file from disk. |
51 std::string file_contents; | 49 std::string file_contents; |
52 if (!file_util::PathExists(path) || | 50 if (!file_util::PathExists(path) || |
53 !file_util::ReadFileToString(path, &file_contents)) { | 51 !file_util::ReadFileToString(path, &file_contents)) { |
54 return SkBitmap(); | 52 return SkBitmap(); |
55 } | 53 } |
56 | 54 |
57 // Decode the image using WebKit's image decoder. | 55 // Decode the image using WebKit's image decoder. |
58 const unsigned char* data = | 56 const unsigned char* data = |
59 reinterpret_cast<const unsigned char*>(file_contents.data()); | 57 reinterpret_cast<const unsigned char*>(file_contents.data()); |
60 webkit_glue::ImageDecoder decoder; | 58 webkit_glue::ImageDecoder decoder; |
61 SkBitmap bitmap = decoder.Decode(data, file_contents.length()); | 59 SkBitmap bitmap = decoder.Decode(data, file_contents.length()); |
62 Sk64 bitmap_size = bitmap.getSize64(); | 60 Sk64 bitmap_size = bitmap.getSize64(); |
63 if (!bitmap_size.is32() || bitmap_size.get32() > kMaxImageCanvas) | 61 if (!bitmap_size.is32() || bitmap_size.get32() > kMaxImageCanvas) |
64 return SkBitmap(); | 62 return SkBitmap(); |
65 return bitmap; | 63 return bitmap; |
66 } | 64 } |
67 | 65 |
68 static bool PathContainsParentDirectory(const FilePath& path) { | 66 bool PathContainsParentDirectory(const FilePath& path) { |
69 const FilePath::StringType kSeparators(FilePath::kSeparators); | 67 const FilePath::StringType kSeparators(FilePath::kSeparators); |
70 const FilePath::StringType kParentDirectory(FilePath::kParentDirectory); | 68 const FilePath::StringType kParentDirectory(FilePath::kParentDirectory); |
71 const size_t npos = FilePath::StringType::npos; | 69 const size_t npos = FilePath::StringType::npos; |
72 const FilePath::StringType& value = path.value(); | 70 const FilePath::StringType& value = path.value(); |
73 | 71 |
74 for (size_t i = 0; i < value.length(); ) { | 72 for (size_t i = 0; i < value.length(); ) { |
75 i = value.find(kParentDirectory, i); | 73 i = value.find(kParentDirectory, i); |
76 if (i != npos) { | 74 if (i != npos) { |
77 if ((i == 0 || kSeparators.find(value[i-1]) == npos) && | 75 if ((i == 0 || kSeparators.find(value[i-1]) == npos) && |
78 (i+1 < value.length() || kSeparators.find(value[i+1]) == npos)) { | 76 (i+1 < value.length() || kSeparators.find(value[i+1]) == npos)) { |
79 return true; | 77 return true; |
80 } | 78 } |
81 ++i; | 79 ++i; |
82 } | 80 } |
83 } | 81 } |
84 | 82 |
85 return false; | 83 return false; |
86 } | 84 } |
87 | 85 |
| 86 } // namespace |
| 87 |
| 88 ExtensionUnpacker::ExtensionUnpacker(const FilePath& extension_path) |
| 89 : extension_path_(extension_path) { |
| 90 } |
| 91 |
| 92 ExtensionUnpacker::~ExtensionUnpacker() { |
| 93 } |
| 94 |
88 DictionaryValue* ExtensionUnpacker::ReadManifest() { | 95 DictionaryValue* ExtensionUnpacker::ReadManifest() { |
89 FilePath manifest_path = | 96 FilePath manifest_path = |
90 temp_install_dir_.Append(Extension::kManifestFilename); | 97 temp_install_dir_.Append(Extension::kManifestFilename); |
91 if (!file_util::PathExists(manifest_path)) { | 98 if (!file_util::PathExists(manifest_path)) { |
92 SetError(errors::kInvalidManifest); | 99 SetError(errors::kInvalidManifest); |
93 return NULL; | 100 return NULL; |
94 } | 101 } |
95 | 102 |
96 JSONFileValueSerializer serializer(manifest_path); | 103 JSONFileValueSerializer serializer(manifest_path); |
97 std::string error; | 104 std::string error; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 | 323 |
317 parsed_catalogs_->Set(WideToUTF8(relative_path.DirName().ToWStringHack()), | 324 parsed_catalogs_->Set(WideToUTF8(relative_path.DirName().ToWStringHack()), |
318 root.release()); | 325 root.release()); |
319 | 326 |
320 return true; | 327 return true; |
321 } | 328 } |
322 | 329 |
323 void ExtensionUnpacker::SetError(const std::string &error) { | 330 void ExtensionUnpacker::SetError(const std::string &error) { |
324 error_message_ = error; | 331 error_message_ = error; |
325 } | 332 } |
OLD | NEW |