File: memory_options.cc

package info (click to toggle)
google-gadgets 0.11.2-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 16,820 kB
  • ctags: 20,323
  • sloc: cpp: 116,722; ansic: 18,000; sh: 9,269; makefile: 2,676; xml: 2,138; lex: 459
file content (258 lines) | stat: -rw-r--r-- 7,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
  Copyright 2008 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include <climits>
#include <set>
#include "memory_options.h"
#include "logger.h"
#include "scriptable_holder.h"
#include "string_utils.h"
#include "small_object.h"

namespace ggadget {

class OptionsItem {
 public:
  OptionsItem() {
  }

  explicit OptionsItem(const Variant &value) {
    SetValue(value);
  }

  void SetValue(const Variant &value) {
    value_ = value;
    if (value.type() == Variant::TYPE_SCRIPTABLE)
      holder_.Reset(VariantValue<ScriptableInterface *>()(value));
    else
      holder_.Reset(NULL);
  }

  Variant GetValue() const {
    return value_.type() == Variant::TYPE_SCRIPTABLE ?
           Variant(holder_.Get()) : value_;
  }

 private:
  Variant value_;
  ScriptableHolder<ScriptableInterface> holder_;
};

class MemoryOptions::Impl : public SmallObject<> {
 public:
  Impl(size_t size_limit)
      : size_limit_(size_limit), total_size_(0) {
  }

  void FireChangedEvent(const char *name, const Variant &value) {
    DLOG("option %s changed to %s", name, value.Print().c_str());
    onoptionchanged_signal_(name);
  }

  typedef LightMap<std::string, OptionsItem, GadgetStringComparator> OptionsMap;
  typedef LightSet<std::string, GadgetStringComparator> EncryptedSet;
  OptionsMap values_;
  OptionsMap defaults_;
  OptionsMap internal_values_;
  EncryptedSet encrypted_;
  Signal1<void, const char *> onoptionchanged_signal_;
  size_t size_limit_, total_size_;
};

MemoryOptions::MemoryOptions()
    // Though INT_MAX is much smaller than the maximum value of size_t on
    // some platforms, it is still big enough to be treated as "unlimited".
    : impl_(new Impl(INT_MAX)) {
}

MemoryOptions::MemoryOptions(size_t size_limit)
    : impl_(new Impl(size_limit)) {
}

MemoryOptions::~MemoryOptions() {
  delete impl_;
}

// Returns the approximate size of a variant.
static size_t GetVariantSize(const Variant& v) {
  switch (v.type()) {
    case Variant::TYPE_VOID:
      // It's important to return 0 for TYPE_VOID because sometimes
      // non-existance values are treated as void.
      return 0;
    case Variant::TYPE_STRING:
      return VariantValue<std::string>()(v).size();
    case Variant::TYPE_JSON:
      return VariantValue<JSONString>()(v).value.size();
    case Variant::TYPE_UTF16STRING:
      return VariantValue<UTF16String>()(v).size() * 2;
    default:
      // Value of other types only counted approximately.
      return sizeof(Variant);
  }
}

Connection *MemoryOptions::ConnectOnOptionChanged(
    Slot1<void, const char *> *handler) {
  return impl_->onoptionchanged_signal_.Connect(handler);
}

size_t MemoryOptions::GetCount() {
  return impl_->values_.size();
}

void MemoryOptions::Add(const char *name, const Variant &value) {
  std::string name_str(name); // Avoid multiple std::string() construction.
  if (impl_->values_.find(name_str) == impl_->values_.end()) {
    size_t new_total_size = impl_->total_size_ + name_str.size() +
                            GetVariantSize(value);
    if (new_total_size > impl_->size_limit_) {
      LOG("Options exceeds size limit %zu.", impl_->size_limit_);
    } else {
      impl_->total_size_ = new_total_size;
      impl_->values_[name_str].SetValue(value);
      impl_->FireChangedEvent(name, value);
    }
  }
}

bool MemoryOptions::Exists(const char *name) {
  return impl_->values_.find(name) != impl_->values_.end();
}

Variant MemoryOptions::GetDefaultValue(const char *name) {
  Impl::OptionsMap::const_iterator it = impl_->defaults_.find(name);
  return it == impl_->defaults_.end() ? Variant() : it->second.GetValue();
}

void MemoryOptions::PutDefaultValue(const char *name, const Variant &value) {
  impl_->defaults_[name].SetValue(value);
}

Variant MemoryOptions::GetValue(const char *name) {
  Impl::OptionsMap::const_iterator it = impl_->values_.find(name);
  return it == impl_->values_.end() ?
         GetDefaultValue(name) : it->second.GetValue();
}

void MemoryOptions::PutValue(const char *name, const Variant &value) {
  std::string name_str(name); // Avoid multiple std::string construction.
  Impl::OptionsMap::iterator it = impl_->values_.find(name_str);
  if (it == impl_->values_.end()) {
    Add(name, value);
  } else {
    Variant last_value = it->second.GetValue();
    if (last_value != value) {
      ASSERT(impl_->total_size_ >= GetVariantSize(last_value));
      size_t new_total_size = impl_->total_size_ + GetVariantSize(value) -
                              GetVariantSize(last_value);
      if (new_total_size > impl_->size_limit_) {
        LOG("Options exceeds size limit %zu.", impl_->size_limit_);
      } else {
        impl_->total_size_ = new_total_size;
        it->second.SetValue(value);
        impl_->FireChangedEvent(name, value);
      }
    }
    // Putting a value automatically removes the encrypted state.
    impl_->encrypted_.erase(name_str);
  }
}

void MemoryOptions::Remove(const char *name) {
  std::string name_str(name); // Avoid multiple std::string construction.
  Impl::OptionsMap::iterator it = impl_->values_.find(name_str);
  if (it != impl_->values_.end()) {
    size_t last_value_size = GetVariantSize(it->second.GetValue());
    ASSERT(impl_->total_size_ >= name_str.size() + last_value_size);
    impl_->total_size_ -= name_str.size() + last_value_size;
    impl_->values_.erase(it);
    impl_->encrypted_.erase(name_str);
    impl_->FireChangedEvent(name, Variant());
  }
}

void MemoryOptions::RemoveAll() {
  while (!impl_->values_.empty()) {
    Impl::OptionsMap::iterator it = impl_->values_.begin();
    std::string name(it->first);
    impl_->values_.erase(it);
    impl_->encrypted_.erase(name);
    impl_->FireChangedEvent(name.c_str(), Variant());
  }
  impl_->total_size_ = 0;
}

void MemoryOptions::EncryptValue(const char *name) {
  impl_->encrypted_.insert(name);
}

bool MemoryOptions::IsEncrypted(const char *name) {
  return impl_->encrypted_.find(name) != impl_->encrypted_.end();
}

Variant MemoryOptions::GetInternalValue(const char *name) {
  Impl::OptionsMap::const_iterator it = impl_->internal_values_.find(name);
  return it == impl_->internal_values_.end() ?
         Variant() : it->second.GetValue();
}

void MemoryOptions::PutInternalValue(const char *name, const Variant &value) {
  // Internal values are not counted in total_size_.
  impl_->internal_values_[name].SetValue(value);
}

bool MemoryOptions::Flush() {
  return true;
}

void MemoryOptions::DeleteStorage() {
  impl_->values_.clear();
  impl_->internal_values_.clear();
  impl_->encrypted_.clear();
  impl_->total_size_ = 0;
}

bool MemoryOptions::EnumerateItems(
    Slot3<bool, const char *, const Variant &, bool> *callback) {
  ASSERT(callback);
  for (Impl::OptionsMap::const_iterator it = impl_->values_.begin();
       it != impl_->values_.end(); ++it) {
    const char *name = it->first.c_str();
    if (!(*callback)(name, it->second.GetValue(), IsEncrypted(name))) {
      delete callback;
      return false;
    }
  }
  delete callback;
  return true;
}

bool MemoryOptions::EnumerateInternalItems(
    Slot2<bool, const char *, const Variant &> *callback) {
  ASSERT(callback);
  for (Impl::OptionsMap::const_iterator it = impl_->internal_values_.begin();
       it != impl_->internal_values_.end(); ++it) {
    if (!(*callback)(it->first.c_str(), it->second.GetValue())) {
      delete callback;
      return false;
    }
  }
  delete callback;
  return true;
}

} // namespace ggadget