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

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: add Pass*() 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_; }
86 scoped_ptr<base::DictionaryValue> PassSettings() {
87 return settings_.Pass();
88 }
31 89
32 // Gets whether the operation failed. 90 // Only call if HasError is true.
33 bool HasError() const; 91 const Error& error() const { return *error_; }
34 92 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 93
39 private: 94 private:
40 scoped_ptr<base::DictionaryValue> settings_; 95 scoped_ptr<base::DictionaryValue> settings_;
41 const std::string error_; 96 scoped_ptr<Error> error_;
42 97
43 DISALLOW_COPY_AND_ASSIGN(ReadResultType); 98 DISALLOW_COPY_AND_ASSIGN(ReadResultType);
44 }; 99 };
45 typedef scoped_ptr<ReadResultType> ReadResult; 100 typedef scoped_ptr<ReadResultType> ReadResult;
46 101
47 // The result of a write operation (Set/Remove/Clear). 102 // The result of a write operation (Set/Remove/Clear).
48 class WriteResultType { 103 class WriteResultType {
49 public: 104 public:
50 // Ownership of |changes| taken. 105 explicit WriteResultType(scoped_ptr<ValueStoreChangeList> changes);
51 explicit WriteResultType(ValueStoreChangeList* changes); 106 explicit WriteResultType(scoped_ptr<Error> error);
52 explicit WriteResultType(const std::string& error);
53 ~WriteResultType(); 107 ~WriteResultType();
54 108
109 bool HasError() const { return error_; }
110
55 // Gets the list of changes to the settings which resulted from the write. 111 // Gets the list of changes to the settings which resulted from the write.
56 // Must only be called if HasError() is false. 112 // Won't be present if the NO_GENERATE_CHANGES WriteOptions was given.
57 const ValueStoreChangeList& changes() const; 113 // Only call if HasError is false.
114 ValueStoreChangeList& changes() { return *changes_; }
115 scoped_ptr<ValueStoreChangeList> PassChanges() { return changes_.Pass(); }
58 116
59 // Gets whether the operation failed. 117 // Only call if HasError is true.
60 bool HasError() const; 118 const Error& error() const { return *error_; }
61 119 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 120
66 private: 121 private:
67 const scoped_ptr<ValueStoreChangeList> changes_; 122 scoped_ptr<ValueStoreChangeList> changes_;
68 const std::string error_; 123 scoped_ptr<Error> error_;
69 124
70 DISALLOW_COPY_AND_ASSIGN(WriteResultType); 125 DISALLOW_COPY_AND_ASSIGN(WriteResultType);
71 }; 126 };
72 typedef scoped_ptr<WriteResultType> WriteResult; 127 typedef scoped_ptr<WriteResultType> WriteResult;
73 128
74 // Options for write operations. 129 // Options for write operations.
75 enum WriteOptionsValues { 130 enum WriteOptionsValues {
76 // Callers should usually use this. 131 // Callers should usually use this.
77 DEFAULTS = 0, 132 DEFAULTS = 0,
78 133
79 // Ignore any quota restrictions. 134 // Ignore any quota restrictions.
80 IGNORE_QUOTA = 1<<1, 135 IGNORE_QUOTA = 1<<1,
81 136
82 // Don't generate the changes for a WriteResult. 137 // Don't generate the changes for a WriteResult.
83 NO_GENERATE_CHANGES = 1<<2, 138 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 }; 139 };
89 typedef int WriteOptions; 140 typedef int WriteOptions;
90 141
91 virtual ~ValueStore() {} 142 virtual ~ValueStore() {}
92 143
93 // Helpers for making a Read/WriteResult. 144 // Helpers for making a Read/WriteResult.
94 template<typename T> 145 template<typename T>
95 static ReadResult MakeReadResult(T arg) { 146 static ReadResult MakeReadResult(scoped_ptr<T> arg) {
96 return ReadResult(new ReadResultType(arg)); 147 return ReadResult(new ReadResultType(arg.Pass()));
97 } 148 }
98 149
99 template<typename T> 150 template<typename T>
100 static WriteResult MakeWriteResult(T arg) { 151 static WriteResult MakeWriteResult(scoped_ptr<T> arg) {
101 return WriteResult(new WriteResultType(arg)); 152 return WriteResult(new WriteResultType(arg.Pass()));
102 } 153 }
103 154
104 // Gets the amount of space being used by a single value, in bytes. 155 // 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 156 // Note: The GetBytesInUse methods are only used by extension settings at the
106 // moment. If these become more generally useful, the 157 // moment. If these become more generally useful, the
107 // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes 158 // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes
108 // should be moved to the value_store directory. 159 // should be moved to the value_store directory.
109 virtual size_t GetBytesInUse(const std::string& key) = 0; 160 virtual size_t GetBytesInUse(const std::string& key) = 0;
110 161
111 // Gets the total amount of space being used by multiple values, in bytes. 162 // 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; 187 virtual WriteResult Remove(const std::string& key) = 0;
137 188
138 // Removes multiple keys from the storage. 189 // Removes multiple keys from the storage.
139 virtual WriteResult Remove(const std::vector<std::string>& keys) = 0; 190 virtual WriteResult Remove(const std::vector<std::string>& keys) = 0;
140 191
141 // Clears the storage. 192 // Clears the storage.
142 virtual WriteResult Clear() = 0; 193 virtual WriteResult Clear() = 0;
143 }; 194 };
144 195
145 #endif // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_ 196 #endif // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_H_
OLDNEW
« no previous file with comments | « chrome/browser/value_store/testing_value_store.cc ('k') | chrome/browser/value_store/value_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698