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

Side by Side Diff: extensions/browser/computed_hashes.cc

Issue 2899743002: Remove raw base::DictionaryValue::Set in //extensions (Closed)
Patch Set: Addressed nit Created 3 years, 6 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 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 <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/memory/ptr_util.h"
15 #include "base/stl_util.h" 16 #include "base/stl_util.h"
16 #include "base/values.h" 17 #include "base/values.h"
17 #include "crypto/secure_hash.h" 18 #include "crypto/secure_hash.h"
18 #include "crypto/sha2.h" 19 #include "crypto/sha2.h"
19 20
20 namespace { 21 namespace {
21 const char kBlockHashesKey[] = "block_hashes"; 22 const char kBlockHashesKey[] = "block_hashes";
22 const char kBlockSizeKey[] = "block_size"; 23 const char kBlockSizeKey[] = "block_size";
23 const char kFileHashesKey[] = "file_hashes"; 24 const char kFileHashesKey[] = "file_hashes";
24 const char kPathKey[] = "path"; 25 const char kPathKey[] = "path";
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 130
130 ComputedHashes::Writer::Writer() : file_list_(new base::ListValue) { 131 ComputedHashes::Writer::Writer() : file_list_(new base::ListValue) {
131 } 132 }
132 133
133 ComputedHashes::Writer::~Writer() { 134 ComputedHashes::Writer::~Writer() {
134 } 135 }
135 136
136 void ComputedHashes::Writer::AddHashes(const base::FilePath& relative_path, 137 void ComputedHashes::Writer::AddHashes(const base::FilePath& relative_path,
137 int block_size, 138 int block_size,
138 const std::vector<std::string>& hashes) { 139 const std::vector<std::string>& hashes) {
139 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); 140 auto block_hashes = base::MakeUnique<base::ListValue>();
140 base::ListValue* block_hashes = new base::ListValue(); 141 block_hashes->GetList().reserve(hashes.size());
142 for (const auto& hash : hashes) {
143 std::string encoded;
144 base::Base64Encode(hash, &encoded);
145 block_hashes->GetList().emplace_back(std::move(encoded));
146 }
147
148 auto dict = base::MakeUnique<base::DictionaryValue>();
141 dict->SetString(kPathKey, 149 dict->SetString(kPathKey,
142 relative_path.NormalizePathSeparatorsTo('/').AsUTF8Unsafe()); 150 relative_path.NormalizePathSeparatorsTo('/').AsUTF8Unsafe());
143 dict->SetInteger(kBlockSizeKey, block_size); 151 dict->SetInteger(kBlockSizeKey, block_size);
144 dict->Set(kBlockHashesKey, block_hashes); 152 dict->Set(kBlockHashesKey, std::move(block_hashes));
145 file_list_->Append(std::move(dict)); 153 file_list_->Append(std::move(dict));
146
147 for (std::vector<std::string>::const_iterator i = hashes.begin();
148 i != hashes.end();
149 ++i) {
150 std::string encoded;
151 base::Base64Encode(*i, &encoded);
152 block_hashes->AppendString(encoded);
153 }
154 } 154 }
155 155
156 bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) { 156 bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) {
157 std::string json; 157 std::string json;
158 base::DictionaryValue top_dictionary; 158 base::DictionaryValue top_dictionary;
159 top_dictionary.SetInteger(kVersionKey, kVersion); 159 top_dictionary.SetInteger(kVersionKey, kVersion);
160 top_dictionary.Set(kFileHashesKey, file_list_.release()); 160 top_dictionary.Set(kFileHashesKey, std::move(file_list_));
161 161
162 if (!base::JSONWriter::Write(top_dictionary, &json)) 162 if (!base::JSONWriter::Write(top_dictionary, &json))
163 return false; 163 return false;
164 int written = base::WriteFile(path, json.data(), json.size()); 164 int written = base::WriteFile(path, json.data(), json.size());
165 if (static_cast<unsigned>(written) != json.size()) { 165 if (static_cast<unsigned>(written) != json.size()) {
166 LOG(ERROR) << "Error writing " << path.AsUTF8Unsafe() 166 LOG(ERROR) << "Error writing " << path.AsUTF8Unsafe()
167 << " ; write result:" << written << " expected:" << json.size(); 167 << " ; write result:" << written << " expected:" << json.size();
168 return false; 168 return false;
169 } 169 }
170 return true; 170 return true;
(...skipping 20 matching lines...) Expand all
191 191
192 // If |contents| is empty, then we want to just exit here. 192 // If |contents| is empty, then we want to just exit here.
193 if (bytes_to_read == 0) 193 if (bytes_to_read == 0)
194 break; 194 break;
195 195
196 offset += bytes_to_read; 196 offset += bytes_to_read;
197 } while (offset < contents.size()); 197 } while (offset < contents.size());
198 } 198 }
199 199
200 } // namespace extensions 200 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/app_window/app_window.cc ('k') | extensions/browser/event_listener_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698