1// Copyright 2014 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.
4
5#ifndef EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_CHANGE_H_
6#define EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_CHANGE_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/values.h"
14
15class ValueStoreChange;
16typedef std::vector<ValueStoreChange> ValueStoreChangeList;
17
18// A change to a setting.  Safe/efficient to copy.
19class ValueStoreChange {
20 public:
21  // Converts an ValueStoreChangeList into JSON of the form:
22  // { "foo": { "key": "foo", "oldValue": "bar", "newValue": "baz" } }
23  static std::string ToJson(const ValueStoreChangeList& changes);
24
25  // Ownership of |old_value| and |new_value| taken.
26  ValueStoreChange(
27      const std::string& key, base::Value* old_value, base::Value* new_value);
28
29  ~ValueStoreChange();
30
31  // Gets the key of the setting which changed.
32  const std::string& key() const;
33
34  // Gets the value of the setting before the change, or NULL if there was no
35  // old value.
36  const base::Value* old_value() const;
37
38  // Gets the value of the setting after the change, or NULL if there is no new
39  // value.
40  const base::Value* new_value() const;
41
42 private:
43  class Inner : public base::RefCountedThreadSafe<Inner> {
44   public:
45    Inner(
46        const std::string& key, base::Value* old_value, base::Value* new_value);
47
48    const std::string key_;
49    const scoped_ptr<base::Value> old_value_;
50    const scoped_ptr<base::Value> new_value_;
51
52   private:
53    friend class base::RefCountedThreadSafe<Inner>;
54    virtual ~Inner();
55  };
56
57  scoped_refptr<Inner> inner_;
58};
59
60#endif  // EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_CHANGE_H_
61