1 // Copyright 2010-2018, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 #ifndef MOZC_STORAGE_REGISTRY_H_ 31 #define MOZC_STORAGE_REGISTRY_H_ 32 33 #include <cstring> 34 #include <string> 35 36 #include "base/port.h" 37 #include "base/logging.h" 38 39 namespace mozc { 40 namespace storage { 41 42 class StorageInterface; 43 44 // The idea of Registry module is the same as Windows Registry. 45 // You can use it for saving small data like timestamp, auth_token. 46 // DO NOT USE it to save big data or data which are frequently updated. 47 // Lookup() and Insert() do process-wide global lock and it may be slow. 48 // All methods are thread-safe. 49 // 50 // TODO(taku): Currently, Registry won't guarantee that two processes 51 // can con-currently share the same data. We will replace the backend 52 // of Registry (Storage Interface) with sqlite 53 // 54 // Example: 55 // uint64 timestamp = 0; 56 // CHECK(Registry::Lookup<uint64>("timestamp", ×tamp)); 57 // 58 // string value = "hello world"; 59 // CHECK(Registry::Insert<string>("hello", value)); 60 class Registry { 61 public: 62 template <typename T> Lookup(const string & key,T * value)63 static bool Lookup(const string &key, T *value) { 64 DCHECK(value); 65 string tmp; 66 if (!LookupInternal(key, &tmp)) { 67 return false; 68 } 69 if (sizeof(*value) != tmp.size()) { 70 return false; 71 } 72 memcpy(reinterpret_cast<char *>(value), 73 tmp.data(), tmp.size()); 74 return true; 75 } 76 Lookup(const string & key,string * value)77 static bool Lookup(const string &key, string *value) { 78 return LookupInternal(key, value); 79 } 80 Lookup(const string & key,bool * value)81 static bool Lookup(const string &key, bool *value) { 82 uint8 v = 0; 83 const bool result = Lookup<uint8>(key, &v); 84 *value = (v != 0); 85 return result; 86 } 87 88 // insert key and data 89 // It is not guaranteed that the data is synced to the disk 90 template <typename T> Insert(const string & key,const T & value)91 static bool Insert(const string &key, const T &value) { 92 string tmp(reinterpret_cast<const char *>(&value), sizeof(value)); 93 return InsertInternal(key, tmp); 94 } 95 96 // Insert key and string value 97 // It is not guaranteed that the data is synced to the disk Insert(const string & key,const string & value)98 static bool Insert(const string &key, const string &value) { 99 return InsertInternal(key, value); 100 } 101 102 // Insert key and bool value 103 // It is not guaranteed that the data is synced to the disk Insert(const string & key,const bool & value)104 static bool Insert(const string &key, const bool &value) { 105 const uint8 tmp = static_cast<uint8>(value); 106 return Insert<uint8>(key, tmp); 107 } 108 109 // Syncing the data into disk 110 static bool Sync(); 111 112 // Erase key 113 static bool Erase(const string &key); 114 115 // clear internal keys and values 116 static bool Clear(); 117 118 // Inject internal storage for unittesting. 119 // TinyStorage is used by default 120 // TODO(taku) replace it with SQLITE 121 static void SetStorage(StorageInterface *handler); 122 123 private: 124 static bool LookupInternal(const string &key, string *value); 125 static bool InsertInternal(const string &key, const string &value); 126 127 DISALLOW_IMPLICIT_CONSTRUCTORS(Registry); 128 }; 129 } // namespace storage 130 } // namespace mozc 131 #endif // MOZC_STORAGE_REGISTRY_H_ 132