| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 block_size = 0; | 50 block_size = 0; |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| 53 | 53 |
| 54 base::ListValue* hashes_list = NULL; | 54 base::ListValue* hashes_list = NULL; |
| 55 if (!dictionary->GetList(kBlockHashesKey, &hashes_list)) | 55 if (!dictionary->GetList(kBlockHashesKey, &hashes_list)) |
| 56 return false; | 56 return false; |
| 57 | 57 |
| 58 base::FilePath relative_path = | 58 base::FilePath relative_path = |
| 59 base::FilePath::FromUTF8Unsafe(relative_path_utf8); | 59 base::FilePath::FromUTF8Unsafe(relative_path_utf8); |
| 60 relative_path = relative_path.NormalizePathSeparatorsTo('/'); |
| 60 | 61 |
| 61 data_[relative_path] = HashInfo(block_size, std::vector<std::string>()); | 62 data_[relative_path] = HashInfo(block_size, std::vector<std::string>()); |
| 62 std::vector<std::string>* hashes = &(data_[relative_path].second); | 63 std::vector<std::string>* hashes = &(data_[relative_path].second); |
| 63 | 64 |
| 64 for (size_t j = 0; j < hashes_list->GetSize(); j++) { | 65 for (size_t j = 0; j < hashes_list->GetSize(); j++) { |
| 65 std::string encoded; | 66 std::string encoded; |
| 66 if (!hashes_list->GetString(j, &encoded)) | 67 if (!hashes_list->GetString(j, &encoded)) |
| 67 return false; | 68 return false; |
| 68 | 69 |
| 69 hashes->push_back(std::string()); | 70 hashes->push_back(std::string()); |
| 70 std::string* decoded = &hashes->back(); | 71 std::string* decoded = &hashes->back(); |
| 71 if (!base::Base64Decode(encoded, decoded)) { | 72 if (!base::Base64Decode(encoded, decoded)) { |
| 72 hashes->clear(); | 73 hashes->clear(); |
| 73 return false; | 74 return false; |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 } | 77 } |
| 77 return true; | 78 return true; |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool ComputedHashes::Reader::GetHashes(const base::FilePath& relative_path, | 81 bool ComputedHashes::Reader::GetHashes(const base::FilePath& relative_path, |
| 81 int* block_size, | 82 int* block_size, |
| 82 std::vector<std::string>* hashes) { | 83 std::vector<std::string>* hashes) { |
| 83 std::map<base::FilePath, HashInfo>::iterator i = data_.find(relative_path); | 84 base::FilePath path = relative_path.NormalizePathSeparatorsTo('/'); |
| 85 std::map<base::FilePath, HashInfo>::iterator i = data_.find(path); |
| 84 if (i == data_.end()) | 86 if (i == data_.end()) |
| 85 return false; | 87 return false; |
| 86 HashInfo& info = i->second; | 88 HashInfo& info = i->second; |
| 87 *block_size = info.first; | 89 *block_size = info.first; |
| 88 *hashes = info.second; | 90 *hashes = info.second; |
| 89 return true; | 91 return true; |
| 90 } | 92 } |
| 91 | 93 |
| 92 ComputedHashes::Writer::Writer() { | 94 ComputedHashes::Writer::Writer() { |
| 93 } | 95 } |
| 94 ComputedHashes::Writer::~Writer() { | 96 ComputedHashes::Writer::~Writer() { |
| 95 } | 97 } |
| 96 | 98 |
| 97 void ComputedHashes::Writer::AddHashes(const base::FilePath& relative_path, | 99 void ComputedHashes::Writer::AddHashes(const base::FilePath& relative_path, |
| 98 int block_size, | 100 int block_size, |
| 99 const std::vector<std::string>& hashes) { | 101 const std::vector<std::string>& hashes) { |
| 100 base::DictionaryValue* dict = new base::DictionaryValue(); | 102 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 101 base::ListValue* block_hashes = new base::ListValue(); | 103 base::ListValue* block_hashes = new base::ListValue(); |
| 102 file_list_.Append(dict); | 104 file_list_.Append(dict); |
| 103 dict->SetString(kPathKey, relative_path.AsUTF8Unsafe()); | 105 dict->SetString(kPathKey, |
| 106 relative_path.NormalizePathSeparatorsTo('/').AsUTF8Unsafe()); |
| 104 dict->SetInteger(kBlockSizeKey, block_size); | 107 dict->SetInteger(kBlockSizeKey, block_size); |
| 105 dict->Set(kBlockHashesKey, block_hashes); | 108 dict->Set(kBlockHashesKey, block_hashes); |
| 106 | 109 |
| 107 for (std::vector<std::string>::const_iterator i = hashes.begin(); | 110 for (std::vector<std::string>::const_iterator i = hashes.begin(); |
| 108 i != hashes.end(); | 111 i != hashes.end(); |
| 109 ++i) { | 112 ++i) { |
| 110 std::string encoded; | 113 std::string encoded; |
| 111 base::Base64Encode(*i, &encoded); | 114 base::Base64Encode(*i, &encoded); |
| 112 block_hashes->AppendString(encoded); | 115 block_hashes->AppendString(encoded); |
| 113 } | 116 } |
| 114 } | 117 } |
| 115 | 118 |
| 116 bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) { | 119 bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) { |
| 117 std::string json; | 120 std::string json; |
| 118 if (!base::JSONWriter::Write(&file_list_, &json)) | 121 if (!base::JSONWriter::Write(&file_list_, &json)) |
| 119 return false; | 122 return false; |
| 120 int written = base::WriteFile(path, json.data(), json.size()); | 123 int written = base::WriteFile(path, json.data(), json.size()); |
| 121 if (static_cast<unsigned>(written) != json.size()) { | 124 if (static_cast<unsigned>(written) != json.size()) { |
| 122 LOG(ERROR) << "Error writing " << path.MaybeAsASCII() | 125 LOG(ERROR) << "Error writing " << path.MaybeAsASCII() |
| 123 << " ; write result:" << written << " expected:" << json.size(); | 126 << " ; write result:" << written << " expected:" << json.size(); |
| 124 return false; | 127 return false; |
| 125 } | 128 } |
| 126 return true; | 129 return true; |
| 127 } | 130 } |
| 128 | 131 |
| 129 } // namespace extensions | 132 } // namespace extensions |
| OLD | NEW |