OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_settings_storage_cache.h" | 5 #include "chrome/browser/extensions/extension_settings_storage_cache.h" |
6 | 6 |
7 ExtensionSettingsStorageCache::ExtensionSettingsStorageCache( | 7 ExtensionSettingsStorageCache::ExtensionSettingsStorageCache( |
8 ExtensionSettingsStorage* delegate) : delegate_(delegate) {} | 8 ExtensionSettingsStorage* delegate) : delegate_(delegate) {} |
9 | 9 |
10 ExtensionSettingsStorageCache::~ExtensionSettingsStorageCache() {} | 10 ExtensionSettingsStorageCache::~ExtensionSettingsStorageCache() {} |
11 | 11 |
12 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get( | 12 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get( |
13 const std::string& key) { | 13 const std::string& key) { |
14 Value *value; | 14 Value *value; |
15 if (GetFromCache(key, &value)) { | 15 if (GetFromCache(key, &value)) { |
16 DictionaryValue* settings = new DictionaryValue(); | 16 DictionaryValue* settings = new DictionaryValue(); |
17 settings->SetWithoutPathExpansion(key, value); | 17 settings->SetWithoutPathExpansion(key, value); |
18 return Result(settings, NULL); | 18 return Result(settings, NULL, NULL); |
19 } | 19 } |
20 | 20 |
21 Result result = delegate_->Get(key); | 21 Result result = delegate_->Get(key); |
22 if (result.HasError()) { | 22 if (result.HasError()) { |
23 return result; | 23 return result; |
24 } | 24 } |
25 | 25 |
26 cache_.MergeDictionary(result.GetSettings()); | 26 cache_.MergeDictionary(result.GetSettings()); |
27 return result; | 27 return result; |
28 } | 28 } |
29 | 29 |
30 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get( | 30 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get( |
31 const std::vector<std::string>& keys) { | 31 const std::vector<std::string>& keys) { |
32 scoped_ptr<DictionaryValue> from_cache(new DictionaryValue()); | 32 scoped_ptr<DictionaryValue> from_cache(new DictionaryValue()); |
33 std::vector<std::string> missing_keys; | 33 std::vector<std::string> missing_keys; |
34 | 34 |
35 for (std::vector<std::string>::const_iterator it = keys.begin(); | 35 for (std::vector<std::string>::const_iterator it = keys.begin(); |
36 it != keys.end(); ++it) { | 36 it != keys.end(); ++it) { |
37 Value *value; | 37 Value *value; |
38 if (GetFromCache(*it, &value)) { | 38 if (GetFromCache(*it, &value)) { |
39 from_cache->SetWithoutPathExpansion(*it, value); | 39 from_cache->SetWithoutPathExpansion(*it, value); |
40 } else { | 40 } else { |
41 missing_keys.push_back(*it); | 41 missing_keys.push_back(*it); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 if (missing_keys.empty()) { | 45 if (missing_keys.empty()) { |
46 return Result(from_cache.release(), NULL); | 46 return Result(from_cache.release(), NULL, NULL); |
47 } | 47 } |
48 | 48 |
49 Result result = delegate_->Get(keys); | 49 Result result = delegate_->Get(keys); |
50 if (result.HasError()) { | 50 if (result.HasError()) { |
51 return result; | 51 return result; |
52 } | 52 } |
53 | 53 |
54 cache_.MergeDictionary(result.GetSettings()); | 54 cache_.MergeDictionary(result.GetSettings()); |
55 result.GetSettings()->MergeDictionary(from_cache.get()); | 55 |
56 return result; | 56 DictionaryValue* settings_with_cache = result.GetSettings()->DeepCopy(); |
| 57 settings_with_cache->MergeDictionary(from_cache.get()); |
| 58 return Result(settings_with_cache, NULL, NULL); |
57 } | 59 } |
58 | 60 |
59 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get() { | 61 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Get() { |
60 Result result = delegate_->Get(); | 62 Result result = delegate_->Get(); |
61 if (!result.HasError()) { | 63 if (!result.HasError()) { |
62 cache_.MergeDictionary(result.GetSettings()); | 64 cache_.MergeDictionary(result.GetSettings()); |
63 } | 65 } |
64 return result; | 66 return result; |
65 } | 67 } |
66 | 68 |
67 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Set( | 69 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Set( |
68 const std::string& key, const Value& value) { | 70 const std::string& key, const Value& value) { |
69 Result result = delegate_->Set(key, value); | 71 Result result = delegate_->Set(key, value); |
70 if (!result.HasError()) { | 72 if (!result.HasError()) { |
71 cache_.MergeDictionary(result.GetSettings()); | 73 cache_.MergeDictionary(result.GetSettings()); |
72 } | 74 } |
73 return result; | 75 return result; |
74 } | 76 } |
75 | 77 |
76 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Set( | 78 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Set( |
77 const DictionaryValue& settings) { | 79 const DictionaryValue& settings) { |
78 Result result = delegate_->Set(settings); | 80 Result result = delegate_->Set(settings); |
79 if (result.HasError()) { | 81 if (result.HasError()) { |
80 return result; | 82 return result; |
81 } | 83 } |
82 | 84 |
83 std::set<std::string>* changed_keys = result.GetChangedKeys(); | 85 const std::set<std::string>* changed_keys = result.GetChangedKeys(); |
84 DCHECK(changed_keys); | 86 DCHECK(changed_keys); |
85 for (std::set<std::string>::iterator it = changed_keys->begin(); | 87 for (std::set<std::string>::const_iterator it = changed_keys->begin(); |
86 it != changed_keys->end(); ++it) { | 88 it != changed_keys->end(); ++it) { |
87 Value* new_value = NULL; | 89 Value* new_value = NULL; |
88 result.GetSettings()->GetWithoutPathExpansion(*it, &new_value); | 90 result.GetSettings()->GetWithoutPathExpansion(*it, &new_value); |
89 DCHECK(new_value); | 91 DCHECK(new_value); |
90 cache_.SetWithoutPathExpansion(*it, new_value->DeepCopy()); | 92 cache_.SetWithoutPathExpansion(*it, new_value->DeepCopy()); |
91 } | 93 } |
92 return result; | 94 return result; |
93 } | 95 } |
94 | 96 |
95 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Remove( | 97 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Remove( |
96 const std::string& key) { | 98 const std::string& key) { |
97 Result result = delegate_->Remove(key); | 99 Result result = delegate_->Remove(key); |
98 if (!result.HasError()) { | 100 if (!result.HasError()) { |
99 cache_.RemoveWithoutPathExpansion(key, NULL); | 101 cache_.RemoveWithoutPathExpansion(key, NULL); |
100 } | 102 } |
101 return result; | 103 return result; |
102 } | 104 } |
103 | 105 |
104 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Remove( | 106 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Remove( |
105 const std::vector<std::string>& keys) { | 107 const std::vector<std::string>& keys) { |
106 Result result = delegate_->Remove(keys); | 108 Result result = delegate_->Remove(keys); |
107 if (result.HasError()) { | 109 if (result.HasError()) { |
108 return result; | 110 return result; |
109 } | 111 } |
110 | 112 |
111 std::set<std::string>* changed_keys = result.GetChangedKeys(); | 113 const std::set<std::string>* changed_keys = result.GetChangedKeys(); |
112 DCHECK(changed_keys); | 114 DCHECK(changed_keys); |
113 for (std::set<std::string>::iterator it = changed_keys->begin(); | 115 for (std::set<std::string>::const_iterator it = changed_keys->begin(); |
114 it != changed_keys->end(); ++it) { | 116 it != changed_keys->end(); ++it) { |
115 cache_.RemoveWithoutPathExpansion(*it, NULL); | 117 cache_.RemoveWithoutPathExpansion(*it, NULL); |
116 } | 118 } |
117 return result; | 119 return result; |
118 } | 120 } |
119 | 121 |
120 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Clear() { | 122 ExtensionSettingsStorage::Result ExtensionSettingsStorageCache::Clear() { |
121 Result result = delegate_->Clear(); | 123 Result result = delegate_->Clear(); |
122 if (!result.HasError()) { | 124 if (!result.HasError()) { |
123 cache_.Clear(); | 125 cache_.Clear(); |
124 } | 126 } |
125 return result; | 127 return result; |
126 } | 128 } |
127 | 129 |
128 bool ExtensionSettingsStorageCache::GetFromCache( | 130 bool ExtensionSettingsStorageCache::GetFromCache( |
129 const std::string& key, Value** value) { | 131 const std::string& key, Value** value) { |
130 Value* cached_value; | 132 Value* cached_value; |
131 if (!cache_.GetWithoutPathExpansion(key, &cached_value)) { | 133 if (!cache_.GetWithoutPathExpansion(key, &cached_value)) { |
132 return false; | 134 return false; |
133 } | 135 } |
134 | 136 |
135 *value = cached_value->DeepCopy(); | 137 *value = cached_value->DeepCopy(); |
136 return true; | 138 return true; |
137 } | 139 } |
OLD | NEW |