summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_settings_storage.h
blob: c13c5d0f6b511d0a8befa2abd946888735b8f69c (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_STORAGE_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_STORAGE_H_
#pragma once

#include <set>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_setting_change.h"

// Interface for extension settings storage classes.
//
// All methods *must* be run on the FILE thread, including construction and
// destruction.
class ExtensionSettingsStorage {
 public:
  // The result of a read operation (Get). Safe/efficient to copy.
  class ReadResult {
   public:
    // Ownership of |settings| taken.
    explicit ReadResult(DictionaryValue* settings);
    explicit ReadResult(const std::string& error);
    ~ReadResult();

    // Gets the settings read from the storage.
    // Must only be called if HasError() is false.
    const DictionaryValue& settings() const;

    // Gets whether the operation failed.
    bool HasError() const;

    // Gets the error message describing the failure.
    // Must only be called if HasError() is true.
    const std::string& error() const;

   private:
    class Inner : public base::RefCountedThreadSafe<Inner> {
     public:
      Inner(DictionaryValue* settings, const std::string& error);
      const scoped_ptr<DictionaryValue> settings_;
      const std::string error_;

     private:
      friend class base::RefCountedThreadSafe<Inner>;
      virtual ~Inner();
    };

    scoped_refptr<Inner> inner_;
  };

  // The result of a write operation (Set/Remove/Clear). Safe/efficient to copy.
  class WriteResult {
   public:
    // Ownership of |changes| taken.
    explicit WriteResult(ExtensionSettingChangeList* changes);
    explicit WriteResult(const std::string& error);
    ~WriteResult();

    // Gets the list of changes to the settings which resulted from the write.
    // Must only be called if HasError() is false.
    const ExtensionSettingChangeList& changes() const;

    // Gets whether the operation failed.
    bool HasError() const;

    // Gets the error message describing the failure.
    // Must only be called if HasError() is true.
    const std::string& error() const;

   private:
    class Inner : public base::RefCountedThreadSafe<Inner> {
     public:
      Inner(ExtensionSettingChangeList* changes, const std::string& error);
      const scoped_ptr<ExtensionSettingChangeList> changes_;
      const std::string error_;

     private:
      friend class base::RefCountedThreadSafe<Inner>;
      virtual ~Inner();
    };

    scoped_refptr<Inner> inner_;
  };

  virtual ~ExtensionSettingsStorage() {}

  // Gets a single value from storage.
  virtual ReadResult Get(const std::string& key) = 0;

  // Gets multiple values from storage.
  virtual ReadResult Get(const std::vector<std::string>& keys) = 0;

  // Gets all values from storage.
  virtual ReadResult Get() = 0;

  // Sets a single key to a new value.
  virtual WriteResult Set(const std::string& key, const Value& value) = 0;

  // Sets multiple keys to new values.
  virtual WriteResult Set(const DictionaryValue& values) = 0;

  // Removes a key from the storage.
  virtual WriteResult Remove(const std::string& key) = 0;

  // Removes multiple keys from the storage.
  virtual WriteResult Remove(const std::vector<std::string>& keys) = 0;

  // Clears the storage.
  virtual WriteResult Clear() = 0;
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SETTINGS_STORAGE_H_