1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import android.content.SharedPreferences;
9 import java.util.Collections;
10 import java.util.HashMap;
15 * An implementation of SharedPreferences that can be used to create XWalkBrowserContext.
17 * It keeps all state in memory, and there is no difference between apply() and commit().
19 public class InMemorySharedPreferences implements SharedPreferences {
21 // Guarded on its own monitor.
22 private final Map<String, Object> mData;
24 public InMemorySharedPreferences() {
25 mData = new HashMap<String, Object>();
28 public InMemorySharedPreferences(Map<String, Object> data) {
33 public Map<String, ?> getAll() {
34 synchronized (mData) {
35 return Collections.unmodifiableMap(mData);
40 public String getString(String key, String defValue) {
41 synchronized (mData) {
42 if (mData.containsKey(key)) {
43 return (String) mData.get(key);
49 @SuppressWarnings("unchecked")
51 public Set<String> getStringSet(String key, Set<String> defValues) {
52 synchronized (mData) {
53 if (mData.containsKey(key)) {
54 return Collections.unmodifiableSet((Set<String>) mData.get(key));
61 public int getInt(String key, int defValue) {
62 synchronized (mData) {
63 if (mData.containsKey(key)) {
64 return (Integer) mData.get(key);
71 public long getLong(String key, long defValue) {
72 synchronized (mData) {
73 if (mData.containsKey(key)) {
74 return (Long) mData.get(key);
81 public float getFloat(String key, float defValue) {
82 synchronized (mData) {
83 if (mData.containsKey(key)) {
84 return (Float) mData.get(key);
91 public boolean getBoolean(String key, boolean defValue) {
92 synchronized (mData) {
93 if (mData.containsKey(key)) {
94 return (Boolean) mData.get(key);
101 public boolean contains(String key) {
102 synchronized (mData) {
103 return mData.containsKey(key);
108 public SharedPreferences.Editor edit() {
109 return new InMemoryEditor();
113 public void registerOnSharedPreferenceChangeListener(
114 SharedPreferences.OnSharedPreferenceChangeListener
116 throw new UnsupportedOperationException();
120 public void unregisterOnSharedPreferenceChangeListener(
121 SharedPreferences.OnSharedPreferenceChangeListener listener) {
122 throw new UnsupportedOperationException();
125 private class InMemoryEditor implements SharedPreferences.Editor {
127 // All guarded by |mChanges|.
128 private boolean mClearCalled;
129 private volatile boolean mApplyCalled;
130 private final Map<String, Object> mChanges = new HashMap<String, Object>();
133 public SharedPreferences.Editor putString(String key, String value) {
134 synchronized (mChanges) {
135 if (mApplyCalled) throw new IllegalStateException();
136 mChanges.put(key, value);
142 public SharedPreferences.Editor putStringSet(String key, Set<String> values) {
143 synchronized (mChanges) {
144 if (mApplyCalled) throw new IllegalStateException();
145 mChanges.put(key, values);
151 public SharedPreferences.Editor putInt(String key, int value) {
152 synchronized (mChanges) {
153 if (mApplyCalled) throw new IllegalStateException();
154 mChanges.put(key, value);
160 public SharedPreferences.Editor putLong(String key, long value) {
161 synchronized (mChanges) {
162 if (mApplyCalled) throw new IllegalStateException();
163 mChanges.put(key, value);
169 public SharedPreferences.Editor putFloat(String key, float value) {
170 synchronized (mChanges) {
171 if (mApplyCalled) throw new IllegalStateException();
172 mChanges.put(key, value);
178 public SharedPreferences.Editor putBoolean(String key, boolean value) {
179 synchronized (mChanges) {
180 if (mApplyCalled) throw new IllegalStateException();
181 mChanges.put(key, value);
187 public SharedPreferences.Editor remove(String key) {
188 synchronized (mChanges) {
189 if (mApplyCalled) throw new IllegalStateException();
190 // Magic value for removes
191 mChanges.put(key, this);
197 public SharedPreferences.Editor clear() {
198 synchronized (mChanges) {
199 if (mApplyCalled) throw new IllegalStateException();
206 public boolean commit() {
212 public void apply() {
213 synchronized (mData) {
214 synchronized (mChanges) {
215 if (mApplyCalled) throw new IllegalStateException();
219 for (Map.Entry<String, Object> entry : mChanges.entrySet()) {
220 String key = entry.getKey();
221 Object value = entry.getValue();
223 // Special value for removal
226 mData.put(key, value);
229 // The real shared prefs clears out the temporaries allowing the caller to
230 // reuse the Editor instance, however this is undocumented behavior and subtle
231 // to read, so instead we just ban any future use of this instance.