Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1096)

Side by Side Diff: chrome/browser/value_store/value_store.h

Issue 24021002: Propagate more information about ValueStore errors to callers, notably an (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_ 5 #ifndef CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
6 #define CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_ 6 #define CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/value_store/value_store_change.h" 13 #include "chrome/browser/value_store/value_store_change.h"
14 14
15 // Interface for a storage area for Value objects. 15 // Interface for a storage area for Value objects.
16 class ValueStore { 16 class ValueStore {
17 public: 17 public:
18 // Error codes returned from storage methods.
19 enum ErrorCode {
20 OK,
21
22 // The failure was due to some kind of database corruption. Depending on
23 // what is corrupted, some part of the database may be recoverable.
24 //
25 // For example, if the on-disk representation of leveldb is corrupted, it's
26 // likely the whole database will need to be wiped and started again.
27 //
28 // If a single key has been committed with an invalid JSON representation,
29 // just that key can be deleted without affecting the rest of the database.
30 CORRUPTION,
31
32 // The failure was due to the store being read-only (for example, policy).
33 READ_ONLY,
34
35 // The failure was due to the store running out of space.
36 QUOTA_EXCEEDED,
37
38 // Any other error.
39 OTHER_ERROR,
40 };
41
42 // Bundles an ErrorCode with further metadata.
43 struct Error {
44 Error(ErrorCode code,
45 const std::string& message,
46 scoped_ptr<std::string> key);
47 ~Error();
48
49 static scoped_ptr<Error> Create(ErrorCode code,
50 const std::string& message,
51 scoped_ptr<std::string> key) {
52 return make_scoped_ptr(new Error(code, message, key.Pass()));
53 }
54
55 // The error code.
56 const ErrorCode code;
57
58 // Message associated with the error.
59 const std::string message;
60
61 // The key associated with the error, if any. Use a scoped_ptr here
62 // because empty-string is a valid key.
63 //
64 // TODO(kalman): add test(s) for an empty key.
65 const scoped_ptr<std::string> key;
66
67 private:
68 DISALLOW_COPY_AND_ASSIGN(Error);
69 };
70
18 // The result of a read operation (Get). 71 // The result of a read operation (Get).
19 class ReadResultType { 72 class ReadResultType {
20 public: 73 public:
21 // Ownership of |settings| taken. 74 explicit ReadResultType(scoped_ptr<base::DictionaryValue> settings);
22 explicit ReadResultType(base::DictionaryValue* settings); 75 explicit ReadResultType(scoped_ptr<Error> error);
23 explicit ReadResultType(const std::string& error);
24 ~ReadResultType(); 76 ~ReadResultType();
25 77
78 bool HasError() const { return error_; }
79
26 // Gets the settings read from the storage. Note that this represents 80 // Gets the settings read from the storage. Note that this represents
27 // the root object. If you request the value for key "foo", that value will 81 // the root object. If you request the value for key "foo", that value will
28 // be in |settings.foo|. 82 // be in |settings|.|foo|.
29 // Must only be called if HasError() is false. 83 //
30 scoped_ptr<base::DictionaryValue>& settings(); 84 // Must only be called if there is no error.
85 base::DictionaryValue& settings() { return *settings_; }
31 86
32 // Gets whether the operation failed. 87 // Only call if HasError is true.
33 bool HasError() const; 88 const Error& error() const { return *error_; }
34 89 scoped_ptr<Error> PassError() { return error_.Pass(); }
35 // Gets the error message describing the failure.
36 // Must only be called if HasError() is true.
37 const std::string& error() const;
38 90
39 private: 91 private:
40 scoped_ptr<base::DictionaryValue> settings_; 92 scoped_ptr<base::DictionaryValue> settings_;
41 const std::string error_; 93 scoped_ptr<Error> error_;
42 94
43 DISALLOW_COPY_AND_ASSIGN(ReadResultType); 95 DISALLOW_COPY_AND_ASSIGN(ReadResultType);
44 }; 96 };
45 typedef scoped_ptr<ReadResultType> ReadResult; 97 typedef scoped_ptr<ReadResultType> ReadResult;
46 98
47 // The result of a write operation (Set/Remove/Clear). 99 // The result of a write operation (Set/Remove/Clear).
48 class WriteResultType { 100 class WriteResultType {
49 public: 101 public:
50 // Ownership of |changes| taken. 102 explicit WriteResultType(scoped_ptr<ValueStoreChangeList> changes);
51 explicit WriteResultType(ValueStoreChangeList* changes); 103 explicit WriteResultType(scoped_ptr<Error> error);
52 explicit WriteResultType(const std::string& error);
53 ~WriteResultType(); 104 ~WriteResultType();
54 105
106 bool HasError() const { return error_; }
107
55 // Gets the list of changes to the settings which resulted from the write. 108 // Gets the list of changes to the settings which resulted from the write.
56 // Must only be called if HasError() is false. 109 // Won't be present if the NO_GENERATE_CHANGES WriteOptions was given.
57 const ValueStoreChangeList& changes() const; 110 // Only call if HasError is false.
111 ValueStoreChangeList& changes() { return *changes_; }
58 112
59 // Gets whether the operation failed. 113 // Only call if HasError is true.
60 bool HasError() const; 114 const Error& error() const { return *error_; }
61 115 scoped_ptr<Error> PassError() { return error_.Pass(); }
62 // Gets the error message describing the failure.
63 // Must only be called if HasError() is true.
64 const std::string& error() const;
65 116
66 private: 117 private:
67 const scoped_ptr<ValueStoreChangeList> changes_; 118 scoped_ptr<ValueStoreChangeList> changes_;
68 const std::string error_; 119 scoped_ptr<Error> error_;
69 120
70 DISALLOW_COPY_AND_ASSIGN(WriteResultType); 121 DISALLOW_COPY_AND_ASSIGN(WriteResultType);
71 }; 122 };
72 typedef scoped_ptr<WriteResultType> WriteResult; 123 typedef scoped_ptr<WriteResultType> WriteResult;
73 124
74 // Options for write operations. 125 // Options for write operations.
75 enum WriteOptionsValues { 126 enum WriteOptionsValues {
76 // Callers should usually use this. 127 // Callers should usually use this.
77 DEFAULTS = 0, 128 DEFAULTS = 0,
78 129
79 // Ignore any quota restrictions. 130 // Ignore any quota restrictions.
80 IGNORE_QUOTA = 1<<1, 131 IGNORE_QUOTA = 1<<1,
81 132
82 // Don't generate the changes for a WriteResult. 133 // Don't generate the changes for a WriteResult.
83 NO_GENERATE_CHANGES = 1<<2, 134 NO_GENERATE_CHANGES = 1<<2,
84
85 // Don't check the old value before writing a new value. This will also
86 // result in an empty |old_value| in the WriteResult::changes list.
87 NO_CHECK_OLD_VALUE = 1<<3
88 }; 135 };
89 typedef int WriteOptions; 136 typedef int WriteOptions;
90 137
91 virtual ~ValueStore() {} 138 virtual ~ValueStore() {}
92 139
93 // Helpers for making a Read/WriteResult. 140 // Helpers for making a Read/WriteResult.
94 template<typename T> 141 template<typename T>
95 static ReadResult MakeReadResult(T arg) { 142 static ReadResult MakeReadResult(scoped_ptr<T> arg) {
96 return ReadResult(new ReadResultType(arg)); 143 return ReadResult(new ReadResultType(arg.Pass()));
97 } 144 }
98 145
99 template<typename T> 146 template<typename T>
100 static WriteResult MakeWriteResult(T arg) { 147 static WriteResult MakeWriteResult(scoped_ptr<T> arg) {
101 return WriteResult(new WriteResultType(arg)); 148 return WriteResult(new WriteResultType(arg.Pass()));
102 } 149 }
103 150
104 // Gets the amount of space being used by a single value, in bytes. 151 // Gets the amount of space being used by a single value, in bytes.
105 // Note: The GetBytesInUse methods are only used by extension settings at the 152 // Note: The GetBytesInUse methods are only used by extension settings at the
106 // moment. If these become more generally useful, the 153 // moment. If these become more generally useful, the
107 // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes 154 // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes
108 // should be moved to the value_store directory. 155 // should be moved to the value_store directory.
109 virtual size_t GetBytesInUse(const std::string& key) = 0; 156 virtual size_t GetBytesInUse(const std::string& key) = 0;
110 157
111 // Gets the total amount of space being used by multiple values, in bytes. 158 // Gets the total amount of space being used by multiple values, in bytes.
(...skipping 24 matching lines...) Expand all
136 virtual WriteResult Remove(const std::string& key) = 0; 183 virtual WriteResult Remove(const std::string& key) = 0;
137 184
138 // Removes multiple keys from the storage. 185 // Removes multiple keys from the storage.
139 virtual WriteResult Remove(const std::vector<std::string>& keys) = 0; 186 virtual WriteResult Remove(const std::vector<std::string>& keys) = 0;
140 187
141 // Clears the storage. 188 // Clears the storage.
142 virtual WriteResult Clear() = 0; 189 virtual WriteResult Clear() = 0;
143 }; 190 };
144 191
145 #endif // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_ 192 #endif // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698