OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 Google Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 // |
| 15 // ValidatingStorage saves data with checksum and timestamp using |
| 16 // ValidatingUtil. |
| 17 |
| 18 #include "validating_storage.h" |
| 19 |
| 20 #include <libaddressinput/callback.h> |
| 21 #include <libaddressinput/storage.h> |
| 22 #include <libaddressinput/util/basictypes.h> |
| 23 #include <libaddressinput/util/scoped_ptr.h> |
| 24 |
| 25 #include <cassert> |
| 26 #include <cstddef> |
| 27 #include <ctime> |
| 28 #include <string> |
| 29 |
| 30 #include "validating_util.h" |
| 31 |
| 32 namespace i18n { |
| 33 namespace addressinput { |
| 34 |
| 35 namespace { |
| 36 |
| 37 class Helper { |
| 38 public: |
| 39 Helper(const std::string& key, |
| 40 const Storage::Callback& data_ready, |
| 41 const Storage& wrapped_storage) |
| 42 : data_ready_(data_ready), |
| 43 wrapped_data_ready_(BuildCallback(this, &Helper::OnWrappedDataReady)) { |
| 44 wrapped_storage.Get(key, *wrapped_data_ready_); |
| 45 } |
| 46 |
| 47 private: |
| 48 ~Helper() {} |
| 49 |
| 50 void OnWrappedDataReady(bool success, |
| 51 const std::string& key, |
| 52 const std::string& wrapped_data) { |
| 53 std::string data(wrapped_data); |
| 54 if (!success || |
| 55 !ValidatingUtil::UnwrapTimestamp(&data, time(NULL)) || |
| 56 !ValidatingUtil::UnwrapChecksum(&data)) { |
| 57 data_ready_(false, key, std::string()); |
| 58 } else { |
| 59 data_ready_(true, key, data); |
| 60 } |
| 61 delete this; |
| 62 } |
| 63 |
| 64 const Storage::Callback& data_ready_; |
| 65 scoped_ptr<Storage::Callback> wrapped_data_ready_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 72 ValidatingStorage::ValidatingStorage(Storage* storage) |
| 73 : wrapped_storage_(storage) { |
| 74 assert(wrapped_storage_ != NULL); |
| 75 } |
| 76 |
| 77 ValidatingStorage::~ValidatingStorage() {} |
| 78 |
| 79 void ValidatingStorage::Put(const std::string& key, const std::string& data) { |
| 80 wrapped_storage_->Put(key, ValidatingUtil::Wrap(data, time(NULL))); |
| 81 } |
| 82 |
| 83 void ValidatingStorage::Get(const std::string& key, |
| 84 const Callback& data_ready) const { |
| 85 new Helper(key, data_ready, *wrapped_storage_); |
| 86 } |
| 87 |
| 88 } // namespace addressinput |
| 89 } // namespace i18n |
OLD | NEW |