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 scoped_ptr<Storage::Callback> data_ready, | |
41 const Storage& wrapped_storage) | |
42 : data_ready_(data_ready.Pass()) { | |
43 wrapped_storage.Get(key, BuildCallback(this, &Helper::OnWrappedDataReady)); | |
44 } | |
45 | |
46 private: | |
47 ~Helper() {} | |
48 | |
49 void OnWrappedDataReady(bool success, | |
50 const std::string& key, | |
51 const std::string& wrapped_data) { | |
52 std::string data(wrapped_data); | |
53 if (!success || | |
54 !ValidatingUtil::UnwrapTimestamp(&data, time(NULL)) || | |
55 !ValidatingUtil::UnwrapChecksum(&data)) { | |
56 // TODO(estade): Pretty unsafe. |this| owns |data_ready_|, but | |
57 // |data_ready_->instance_| might be stale. | |
58 (*data_ready_)(false, key, std::string()); | |
59 } else { | |
60 (*data_ready_)(true, key, data); | |
61 } | |
62 // TODO(estade): |this| will leak if the callback is never run. Make this | |
63 // safer. | |
64 delete this; | |
65 } | |
66 | |
67 scoped_ptr<Storage::Callback> data_ready_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(Helper); | |
70 }; | |
71 | |
72 } // namespace | |
73 | |
74 ValidatingStorage::ValidatingStorage(scoped_ptr<Storage> storage) | |
75 : wrapped_storage_(storage.Pass()) { | |
76 assert(wrapped_storage_ != NULL); | |
77 } | |
78 | |
79 ValidatingStorage::~ValidatingStorage() {} | |
80 | |
81 void ValidatingStorage::Put(const std::string& key, const std::string& data) { | |
82 wrapped_storage_->Put(key, ValidatingUtil::Wrap(data, time(NULL))); | |
83 } | |
84 | |
85 void ValidatingStorage::Get(const std::string& key, | |
86 scoped_ptr<Callback> data_ready) const { | |
87 new Helper(key, data_ready.Pass(), *wrapped_storage_); | |
88 } | |
89 | |
90 } // namespace addressinput | |
91 } // namespace i18n | |
OLD | NEW |