OLD | NEW |
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 #include "base/json/json_parser.h" | 5 #include "base/json/json_parser.h" |
6 | 6 |
7 #include "base/float_util.h" | 7 #include "base/float_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // optimization avoids about 2/3rds of string memory copies. The constructor | 30 // optimization avoids about 2/3rds of string memory copies. The constructor |
31 // takes ownership of the input string. The real root value is Swap()ed into | 31 // takes ownership of the input string. The real root value is Swap()ed into |
32 // the new instance. | 32 // the new instance. |
33 class DictionaryHiddenRootValue : public base::DictionaryValue { | 33 class DictionaryHiddenRootValue : public base::DictionaryValue { |
34 public: | 34 public: |
35 DictionaryHiddenRootValue(std::string* json, Value* root) : json_(json) { | 35 DictionaryHiddenRootValue(std::string* json, Value* root) : json_(json) { |
36 DCHECK(root->IsType(Value::TYPE_DICTIONARY)); | 36 DCHECK(root->IsType(Value::TYPE_DICTIONARY)); |
37 DictionaryValue::Swap(static_cast<DictionaryValue*>(root)); | 37 DictionaryValue::Swap(static_cast<DictionaryValue*>(root)); |
38 } | 38 } |
39 | 39 |
40 virtual void Swap(DictionaryValue* other) OVERRIDE { | 40 void Swap(DictionaryValue* other) override { |
41 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently."; | 41 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently."; |
42 | 42 |
43 // First deep copy to convert JSONStringValue to std::string and swap that | 43 // First deep copy to convert JSONStringValue to std::string and swap that |
44 // copy with |other|, which contains the new contents of |this|. | 44 // copy with |other|, which contains the new contents of |this|. |
45 scoped_ptr<base::DictionaryValue> copy(DeepCopy()); | 45 scoped_ptr<base::DictionaryValue> copy(DeepCopy()); |
46 copy->Swap(other); | 46 copy->Swap(other); |
47 | 47 |
48 // Then erase the contents of the current dictionary and swap in the | 48 // Then erase the contents of the current dictionary and swap in the |
49 // new contents, originally from |other|. | 49 // new contents, originally from |other|. |
50 Clear(); | 50 Clear(); |
51 json_.reset(); | 51 json_.reset(); |
52 DictionaryValue::Swap(copy.get()); | 52 DictionaryValue::Swap(copy.get()); |
53 } | 53 } |
54 | 54 |
55 // Not overriding DictionaryValue::Remove because it just calls through to | 55 // Not overriding DictionaryValue::Remove because it just calls through to |
56 // the method below. | 56 // the method below. |
57 | 57 |
58 virtual bool RemoveWithoutPathExpansion(const std::string& key, | 58 bool RemoveWithoutPathExpansion(const std::string& key, |
59 scoped_ptr<Value>* out) OVERRIDE { | 59 scoped_ptr<Value>* out) override { |
60 // If the caller won't take ownership of the removed value, just call up. | 60 // If the caller won't take ownership of the removed value, just call up. |
61 if (!out) | 61 if (!out) |
62 return DictionaryValue::RemoveWithoutPathExpansion(key, out); | 62 return DictionaryValue::RemoveWithoutPathExpansion(key, out); |
63 | 63 |
64 DVLOG(1) << "Remove()ing from a DictionaryValue inefficiently."; | 64 DVLOG(1) << "Remove()ing from a DictionaryValue inefficiently."; |
65 | 65 |
66 // Otherwise, remove the value while its still "owned" by this and copy it | 66 // Otherwise, remove the value while its still "owned" by this and copy it |
67 // to convert any JSONStringValues to std::string. | 67 // to convert any JSONStringValues to std::string. |
68 scoped_ptr<Value> out_owned; | 68 scoped_ptr<Value> out_owned; |
69 if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned)) | 69 if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned)) |
(...skipping 10 matching lines...) Expand all Loading... |
80 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue); | 80 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue); |
81 }; | 81 }; |
82 | 82 |
83 class ListHiddenRootValue : public base::ListValue { | 83 class ListHiddenRootValue : public base::ListValue { |
84 public: | 84 public: |
85 ListHiddenRootValue(std::string* json, Value* root) : json_(json) { | 85 ListHiddenRootValue(std::string* json, Value* root) : json_(json) { |
86 DCHECK(root->IsType(Value::TYPE_LIST)); | 86 DCHECK(root->IsType(Value::TYPE_LIST)); |
87 ListValue::Swap(static_cast<ListValue*>(root)); | 87 ListValue::Swap(static_cast<ListValue*>(root)); |
88 } | 88 } |
89 | 89 |
90 virtual void Swap(ListValue* other) OVERRIDE { | 90 void Swap(ListValue* other) override { |
91 DVLOG(1) << "Swap()ing a ListValue inefficiently."; | 91 DVLOG(1) << "Swap()ing a ListValue inefficiently."; |
92 | 92 |
93 // First deep copy to convert JSONStringValue to std::string and swap that | 93 // First deep copy to convert JSONStringValue to std::string and swap that |
94 // copy with |other|, which contains the new contents of |this|. | 94 // copy with |other|, which contains the new contents of |this|. |
95 scoped_ptr<base::ListValue> copy(DeepCopy()); | 95 scoped_ptr<base::ListValue> copy(DeepCopy()); |
96 copy->Swap(other); | 96 copy->Swap(other); |
97 | 97 |
98 // Then erase the contents of the current list and swap in the new contents, | 98 // Then erase the contents of the current list and swap in the new contents, |
99 // originally from |other|. | 99 // originally from |other|. |
100 Clear(); | 100 Clear(); |
101 json_.reset(); | 101 json_.reset(); |
102 ListValue::Swap(copy.get()); | 102 ListValue::Swap(copy.get()); |
103 } | 103 } |
104 | 104 |
105 virtual bool Remove(size_t index, scoped_ptr<Value>* out) OVERRIDE { | 105 bool Remove(size_t index, scoped_ptr<Value>* out) override { |
106 // If the caller won't take ownership of the removed value, just call up. | 106 // If the caller won't take ownership of the removed value, just call up. |
107 if (!out) | 107 if (!out) |
108 return ListValue::Remove(index, out); | 108 return ListValue::Remove(index, out); |
109 | 109 |
110 DVLOG(1) << "Remove()ing from a ListValue inefficiently."; | 110 DVLOG(1) << "Remove()ing from a ListValue inefficiently."; |
111 | 111 |
112 // Otherwise, remove the value while its still "owned" by this and copy it | 112 // Otherwise, remove the value while its still "owned" by this and copy it |
113 // to convert any JSONStringValues to std::string. | 113 // to convert any JSONStringValues to std::string. |
114 scoped_ptr<Value> out_owned; | 114 scoped_ptr<Value> out_owned; |
115 if (!ListValue::Remove(index, &out_owned)) | 115 if (!ListValue::Remove(index, &out_owned)) |
(...skipping 14 matching lines...) Expand all Loading... |
130 // into the Value. This can only be stored in a child of hidden root (above), | 130 // into the Value. This can only be stored in a child of hidden root (above), |
131 // otherwise the referenced string will not be guaranteed to outlive it. | 131 // otherwise the referenced string will not be guaranteed to outlive it. |
132 class JSONStringValue : public base::Value { | 132 class JSONStringValue : public base::Value { |
133 public: | 133 public: |
134 explicit JSONStringValue(const base::StringPiece& piece) | 134 explicit JSONStringValue(const base::StringPiece& piece) |
135 : Value(TYPE_STRING), | 135 : Value(TYPE_STRING), |
136 string_piece_(piece) { | 136 string_piece_(piece) { |
137 } | 137 } |
138 | 138 |
139 // Overridden from base::Value: | 139 // Overridden from base::Value: |
140 virtual bool GetAsString(std::string* out_value) const OVERRIDE { | 140 bool GetAsString(std::string* out_value) const override { |
141 string_piece_.CopyToString(out_value); | 141 string_piece_.CopyToString(out_value); |
142 return true; | 142 return true; |
143 } | 143 } |
144 virtual bool GetAsString(string16* out_value) const OVERRIDE { | 144 bool GetAsString(string16* out_value) const override { |
145 *out_value = UTF8ToUTF16(string_piece_); | 145 *out_value = UTF8ToUTF16(string_piece_); |
146 return true; | 146 return true; |
147 } | 147 } |
148 virtual Value* DeepCopy() const OVERRIDE { | 148 Value* DeepCopy() const override { |
149 return new StringValue(string_piece_.as_string()); | 149 return new StringValue(string_piece_.as_string()); |
150 } | 150 } |
151 virtual bool Equals(const Value* other) const OVERRIDE { | 151 bool Equals(const Value* other) const override { |
152 std::string other_string; | 152 std::string other_string; |
153 return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) && | 153 return other->IsType(TYPE_STRING) && other->GetAsString(&other_string) && |
154 StringPiece(other_string) == string_piece_; | 154 StringPiece(other_string) == string_piece_; |
155 } | 155 } |
156 | 156 |
157 private: | 157 private: |
158 // The location in the original input stream. | 158 // The location in the original input stream. |
159 base::StringPiece string_piece_; | 159 base::StringPiece string_piece_; |
160 | 160 |
161 DISALLOW_COPY_AND_ASSIGN(JSONStringValue); | 161 DISALLOW_COPY_AND_ASSIGN(JSONStringValue); |
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
956 const std::string& description) { | 956 const std::string& description) { |
957 if (line || column) { | 957 if (line || column) { |
958 return StringPrintf("Line: %i, column: %i, %s", | 958 return StringPrintf("Line: %i, column: %i, %s", |
959 line, column, description.c_str()); | 959 line, column, description.c_str()); |
960 } | 960 } |
961 return description; | 961 return description; |
962 } | 962 } |
963 | 963 |
964 } // namespace internal | 964 } // namespace internal |
965 } // namespace base | 965 } // namespace base |
OLD | NEW |