| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 state_base64.c_str()); | 71 state_base64.c_str()); |
| 72 } | 72 } |
| 73 | 73 |
| 74 bool V4Store::Reset() { | 74 bool V4Store::Reset() { |
| 75 // TODO(vakh): Implement skeleton. | 75 // TODO(vakh): Implement skeleton. |
| 76 state_ = ""; | 76 state_ = ""; |
| 77 return true; | 77 return true; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void V4Store::ApplyUpdate( | 80 void V4Store::ApplyUpdate( |
| 81 const ListUpdateResponse& response, | 81 std::unique_ptr<ListUpdateResponse> response, |
| 82 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 82 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 83 UpdatedStoreReadyCallback callback) { | 83 UpdatedStoreReadyCallback callback) { |
| 84 std::unique_ptr<V4Store> new_store( | 84 std::unique_ptr<V4Store> new_store( |
| 85 new V4Store(this->task_runner_, this->store_path_)); | 85 new V4Store(this->task_runner_, this->store_path_)); |
| 86 | 86 |
| 87 // TODO(vakh): The new store currently only picks up the new state. Do more. | 87 new_store->state_ = response->new_client_state(); |
| 88 new_store->state_ = response.new_client_state(); | 88 // TODO(vakh): |
| 89 | 89 // 1. Merge the old store and the new update in new_store. |
| 90 // TODO(vakh): Merge the old store and the new update in new_store. | 90 // 2. Create a ListUpdateResponse containing RICE encoded hash-prefixes and |
| 91 // Then, create a ListUpdateResponse containing RICE encoded hash-prefixes and | |
| 92 // response_type as FULL_UPDATE, and write that to disk. | 91 // response_type as FULL_UPDATE, and write that to disk. |
| 93 StoreWriteResult result = new_store->WriteToDisk(response); | 92 // 3. Remove this if condition after completing 1. and 2. |
| 94 RecordStoreWriteResult(result); | 93 if (response->has_response_type() && |
| 94 response->response_type() == ListUpdateResponse::FULL_UPDATE) { |
| 95 StoreWriteResult result = new_store->WriteToDisk(std::move(response)); |
| 96 RecordStoreWriteResult(result); |
| 97 } |
| 95 | 98 |
| 96 // new_store is done updating, pass it to the callback. | 99 // new_store is done updating, pass it to the callback. |
| 97 callback_task_runner->PostTask( | 100 callback_task_runner->PostTask( |
| 98 FROM_HERE, base::Bind(callback, base::Passed(&new_store))); | 101 FROM_HERE, base::Bind(callback, base::Passed(&new_store))); |
| 99 } | 102 } |
| 100 | 103 |
| 101 StoreReadResult V4Store::ReadFromDisk() { | 104 StoreReadResult V4Store::ReadFromDisk() { |
| 102 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 105 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 103 | 106 |
| 104 std::string contents; | 107 std::string contents; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 135 } | 138 } |
| 136 | 139 |
| 137 ListUpdateResponse list_update_response = file_format.list_update_response(); | 140 ListUpdateResponse list_update_response = file_format.list_update_response(); |
| 138 state_ = list_update_response.new_client_state(); | 141 state_ = list_update_response.new_client_state(); |
| 139 // TODO(vakh): Do more with what's read from the disk. | 142 // TODO(vakh): Do more with what's read from the disk. |
| 140 | 143 |
| 141 return READ_SUCCESS; | 144 return READ_SUCCESS; |
| 142 } | 145 } |
| 143 | 146 |
| 144 StoreWriteResult V4Store::WriteToDisk( | 147 StoreWriteResult V4Store::WriteToDisk( |
| 145 const ListUpdateResponse& response) const { | 148 std::unique_ptr<ListUpdateResponse> response) const { |
| 146 // Do not write partial updates to the disk. | 149 // Do not write partial updates to the disk. |
| 147 // After merging the updates, the ListUpdateResponse passed to this method | 150 // After merging the updates, the ListUpdateResponse passed to this method |
| 148 // should be a FULL_UPDATE. | 151 // should be a FULL_UPDATE. |
| 149 if (!response.has_response_type() || | 152 if (!response->has_response_type() || |
| 150 response.response_type() != ListUpdateResponse::FULL_UPDATE) { | 153 response->response_type() != ListUpdateResponse::FULL_UPDATE) { |
| 151 DVLOG(1) << "response.has_response_type(): " | 154 DVLOG(1) << "response->has_response_type(): " |
| 152 << response.has_response_type(); | 155 << response->has_response_type(); |
| 153 DVLOG(1) << "response.response_type(): " << response.response_type(); | 156 DVLOG(1) << "response->response_type(): " << response->response_type(); |
| 154 return INVALID_RESPONSE_TYPE_FAILURE; | 157 return INVALID_RESPONSE_TYPE_FAILURE; |
| 155 } | 158 } |
| 156 | 159 |
| 157 // Attempt writing to a temporary file first and at the end, swap the files. | 160 // Attempt writing to a temporary file first and at the end, swap the files. |
| 158 const base::FilePath new_filename = TemporaryFileForFilename(store_path_); | 161 const base::FilePath new_filename = TemporaryFileForFilename(store_path_); |
| 159 | 162 |
| 160 V4StoreFileFormat file_format; | 163 V4StoreFileFormat file_format; |
| 161 file_format.set_magic_number(kFileMagic); | 164 file_format.set_magic_number(kFileMagic); |
| 162 file_format.set_version_number(kFileVersion); | 165 file_format.set_version_number(kFileVersion); |
| 163 ListUpdateResponse* response_to_write = | 166 ListUpdateResponse* response_to_write = |
| 164 file_format.mutable_list_update_response(); | 167 file_format.mutable_list_update_response(); |
| 165 *response_to_write = response; | 168 response_to_write->Swap(response.get()); |
| 166 std::string file_format_string; | 169 std::string file_format_string; |
| 167 file_format.SerializeToString(&file_format_string); | 170 file_format.SerializeToString(&file_format_string); |
| 168 size_t written = base::WriteFile(new_filename, file_format_string.data(), | 171 size_t written = base::WriteFile(new_filename, file_format_string.data(), |
| 169 file_format_string.size()); | 172 file_format_string.size()); |
| 170 DCHECK_EQ(file_format_string.size(), written); | 173 DCHECK_EQ(file_format_string.size(), written); |
| 171 | 174 |
| 172 if (!base::Move(new_filename, store_path_)) { | 175 if (!base::Move(new_filename, store_path_)) { |
| 173 DVLOG(1) << "store_path_: " << store_path_.value(); | 176 DVLOG(1) << "store_path_: " << store_path_.value(); |
| 174 return UNABLE_TO_RENAME_FAILURE; | 177 return UNABLE_TO_RENAME_FAILURE; |
| 175 } | 178 } |
| 176 | 179 |
| 177 return WRITE_SUCCESS; | 180 return WRITE_SUCCESS; |
| 178 } | 181 } |
| 179 | 182 |
| 180 } // namespace safe_browsing | 183 } // namespace safe_browsing |
| OLD | NEW |