| 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 "extensions/browser/computed_hashes.h" | 5 #include "extensions/browser/computed_hashes.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 } | 29 } |
| 30 | 30 |
| 31 ComputedHashes::Reader::~Reader() { | 31 ComputedHashes::Reader::~Reader() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 bool ComputedHashes::Reader::InitFromFile(const base::FilePath& path) { | 34 bool ComputedHashes::Reader::InitFromFile(const base::FilePath& path) { |
| 35 std::string contents; | 35 std::string contents; |
| 36 if (!base::ReadFileToString(path, &contents)) | 36 if (!base::ReadFileToString(path, &contents)) |
| 37 return false; | 37 return false; |
| 38 | 38 |
| 39 base::DictionaryValue* top_dictionary = NULL; | 39 base::DictionaryValue* top_dictionary = nullptr; |
| 40 scoped_ptr<base::Value> value(base::JSONReader::Read(contents)); | 40 scoped_ptr<base::Value> value(base::JSONReader::Read(contents)); |
| 41 if (!value.get() || !value->GetAsDictionary(&top_dictionary)) | 41 if (!value.get() || !value->GetAsDictionary(&top_dictionary)) |
| 42 return false; | 42 return false; |
| 43 | 43 |
| 44 // For now we don't support forwards or backwards compatability in the | 44 // For now we don't support forwards or backwards compatability in the |
| 45 // format, so we return false on version mismatch. | 45 // format, so we return false on version mismatch. |
| 46 int version = 0; | 46 int version = 0; |
| 47 if (!top_dictionary->GetInteger(kVersionKey, &version) || version != kVersion) | 47 if (!top_dictionary->GetInteger(kVersionKey, &version) || version != kVersion) |
| 48 return false; | 48 return false; |
| 49 | 49 |
| 50 base::ListValue* all_hashes = NULL; | 50 base::ListValue* all_hashes = nullptr; |
| 51 if (!top_dictionary->GetList(kFileHashesKey, &all_hashes)) | 51 if (!top_dictionary->GetList(kFileHashesKey, &all_hashes)) |
| 52 return false; | 52 return false; |
| 53 | 53 |
| 54 for (size_t i = 0; i < all_hashes->GetSize(); i++) { | 54 for (size_t i = 0; i < all_hashes->GetSize(); i++) { |
| 55 base::DictionaryValue* dictionary = NULL; | 55 base::DictionaryValue* dictionary = nullptr; |
| 56 if (!all_hashes->GetDictionary(i, &dictionary)) | 56 if (!all_hashes->GetDictionary(i, &dictionary)) |
| 57 return false; | 57 return false; |
| 58 | 58 |
| 59 std::string relative_path_utf8; | 59 std::string relative_path_utf8; |
| 60 if (!dictionary->GetString(kPathKey, &relative_path_utf8)) | 60 if (!dictionary->GetString(kPathKey, &relative_path_utf8)) |
| 61 return false; | 61 return false; |
| 62 | 62 |
| 63 int block_size; | 63 int block_size; |
| 64 if (!dictionary->GetInteger(kBlockSizeKey, &block_size)) | 64 if (!dictionary->GetInteger(kBlockSizeKey, &block_size)) |
| 65 return false; | 65 return false; |
| 66 if (block_size <= 0 || ((block_size % 1024) != 0)) { | 66 if (block_size <= 0 || ((block_size % 1024) != 0)) { |
| 67 LOG(ERROR) << "Invalid block size: " << block_size; | 67 LOG(ERROR) << "Invalid block size: " << block_size; |
| 68 block_size = 0; | 68 block_size = 0; |
| 69 return false; | 69 return false; |
| 70 } | 70 } |
| 71 | 71 |
| 72 base::ListValue* hashes_list = NULL; | 72 base::ListValue* hashes_list = nullptr; |
| 73 if (!dictionary->GetList(kBlockHashesKey, &hashes_list)) | 73 if (!dictionary->GetList(kBlockHashesKey, &hashes_list)) |
| 74 return false; | 74 return false; |
| 75 | 75 |
| 76 base::FilePath relative_path = | 76 base::FilePath relative_path = |
| 77 base::FilePath::FromUTF8Unsafe(relative_path_utf8); | 77 base::FilePath::FromUTF8Unsafe(relative_path_utf8); |
| 78 relative_path = relative_path.NormalizePathSeparatorsTo('/'); | 78 relative_path = relative_path.NormalizePathSeparatorsTo('/'); |
| 79 | 79 |
| 80 data_[relative_path] = HashInfo(block_size, std::vector<std::string>()); | 80 data_[relative_path] = HashInfo(block_size, std::vector<std::string>()); |
| 81 std::vector<std::string>* hashes = &(data_[relative_path].second); | 81 std::vector<std::string>* hashes = &(data_[relative_path].second); |
| 82 | 82 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 173 |
| 174 // If |contents| is empty, then we want to just exit here. | 174 // If |contents| is empty, then we want to just exit here. |
| 175 if (bytes_to_read == 0) | 175 if (bytes_to_read == 0) |
| 176 break; | 176 break; |
| 177 | 177 |
| 178 offset += bytes_to_read; | 178 offset += bytes_to_read; |
| 179 } while (offset < contents.size()); | 179 } while (offset < contents.size()); |
| 180 } | 180 } |
| 181 | 181 |
| 182 } // namespace extensions | 182 } // namespace extensions |
| OLD | NEW |