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

Side by Side Diff: base/json/json_parser.cc

Issue 1927753002: Convert callers of base::DeepCopy() to base::CreateDeepCopy() in //base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: With a comment Created 4 years, 7 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
« no previous file with comments | « base/json/json_parser.h ('k') | base/json/json_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/json/json_parser.h" 5 #include "base/json/json_parser.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <memory> 8 #include <utility>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversion_utils.h" 17 #include "base/strings/utf_string_conversion_utils.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/third_party/icu/icu_utf.h" 19 #include "base/third_party/icu/icu_utf.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 21
21 namespace base { 22 namespace base {
22 namespace internal { 23 namespace internal {
23 24
24 namespace { 25 namespace {
25 26
26 const int kStackMaxDepth = 100; 27 const int kStackMaxDepth = 100;
27 28
28 const int32_t kExtendedASCIIStart = 0x80; 29 const int32_t kExtendedASCIIStart = 0x80;
29 30
30 // This and the class below are used to own the JSON input string for when 31 // DictionaryHiddenRootValue and ListHiddenRootValue are used in conjunction
31 // string tokens are stored as StringPiece instead of std::string. This 32 // with JSONStringValue as an optimization for reducing the number of string
32 // optimization avoids about 2/3rds of string memory copies. The constructor 33 // copies. When this optimization is active, the parser uses a hidden root to
33 // takes ownership of the input string. The real root value is Swap()ed into 34 // keep the original JSON input string live and creates JSONStringValue children
34 // the new instance. 35 // holding StringPiece references to the input string, avoiding about 2/3rds of
36 // string memory copies. The real root value is Swap()ed into the new instance.
35 class DictionaryHiddenRootValue : public DictionaryValue { 37 class DictionaryHiddenRootValue : public DictionaryValue {
36 public: 38 public:
37 DictionaryHiddenRootValue(std::string* json, Value* root) : json_(json) { 39 DictionaryHiddenRootValue(std::unique_ptr<std::string> json, Value* root)
jbroman 2016/04/28 16:46:33 nit: you might also consider making the second arg
dcheng 2016/04/28 17:03:13 Done.
40 : json_(std::move(json)) {
38 DCHECK(root->IsType(Value::TYPE_DICTIONARY)); 41 DCHECK(root->IsType(Value::TYPE_DICTIONARY));
39 DictionaryValue::Swap(static_cast<DictionaryValue*>(root)); 42 DictionaryValue::Swap(static_cast<DictionaryValue*>(root));
40 } 43 }
41 44
42 void Swap(DictionaryValue* other) override { 45 void Swap(DictionaryValue* other) override {
43 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently."; 46 DVLOG(1) << "Swap()ing a DictionaryValue inefficiently.";
44 47
45 // First deep copy to convert JSONStringValue to std::string and swap that 48 // First deep copy to convert JSONStringValue to std::string and swap that
46 // copy with |other|, which contains the new contents of |this|. 49 // copy with |other|, which contains the new contents of |this|.
47 std::unique_ptr<DictionaryValue> copy(DeepCopy()); 50 std::unique_ptr<DictionaryValue> copy(CreateDeepCopy());
48 copy->Swap(other); 51 copy->Swap(other);
49 52
50 // Then erase the contents of the current dictionary and swap in the 53 // Then erase the contents of the current dictionary and swap in the
51 // new contents, originally from |other|. 54 // new contents, originally from |other|.
52 Clear(); 55 Clear();
53 json_.reset(); 56 json_.reset();
54 DictionaryValue::Swap(copy.get()); 57 DictionaryValue::Swap(copy.get());
55 } 58 }
56 59
57 // Not overriding DictionaryValue::Remove because it just calls through to 60 // Not overriding DictionaryValue::Remove because it just calls through to
58 // the method below. 61 // the method below.
59 62
60 bool RemoveWithoutPathExpansion(const std::string& key, 63 bool RemoveWithoutPathExpansion(const std::string& key,
61 std::unique_ptr<Value>* out) override { 64 std::unique_ptr<Value>* out) override {
62 // If the caller won't take ownership of the removed value, just call up. 65 // If the caller won't take ownership of the removed value, just call up.
63 if (!out) 66 if (!out)
64 return DictionaryValue::RemoveWithoutPathExpansion(key, out); 67 return DictionaryValue::RemoveWithoutPathExpansion(key, out);
65 68
66 DVLOG(1) << "Remove()ing from a DictionaryValue inefficiently."; 69 DVLOG(1) << "Remove()ing from a DictionaryValue inefficiently.";
67 70
68 // Otherwise, remove the value while its still "owned" by this and copy it 71 // Otherwise, remove the value while its still "owned" by this and copy it
69 // to convert any JSONStringValues to std::string. 72 // to convert any JSONStringValues to std::string.
70 std::unique_ptr<Value> out_owned; 73 std::unique_ptr<Value> out_owned;
71 if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned)) 74 if (!DictionaryValue::RemoveWithoutPathExpansion(key, &out_owned))
72 return false; 75 return false;
73 76
74 out->reset(out_owned->DeepCopy()); 77 *out = out_owned->CreateDeepCopy();
75 78
76 return true; 79 return true;
77 } 80 }
78 81
79 private: 82 private:
80 std::unique_ptr<std::string> json_; 83 std::unique_ptr<std::string> json_;
81 84
82 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue); 85 DISALLOW_COPY_AND_ASSIGN(DictionaryHiddenRootValue);
83 }; 86 };
84 87
85 class ListHiddenRootValue : public ListValue { 88 class ListHiddenRootValue : public ListValue {
86 public: 89 public:
87 ListHiddenRootValue(std::string* json, Value* root) : json_(json) { 90 ListHiddenRootValue(std::unique_ptr<std::string> json, Value* root)
91 : json_(std::move(json)) {
88 DCHECK(root->IsType(Value::TYPE_LIST)); 92 DCHECK(root->IsType(Value::TYPE_LIST));
89 ListValue::Swap(static_cast<ListValue*>(root)); 93 ListValue::Swap(static_cast<ListValue*>(root));
90 } 94 }
91 95
92 void Swap(ListValue* other) override { 96 void Swap(ListValue* other) override {
93 DVLOG(1) << "Swap()ing a ListValue inefficiently."; 97 DVLOG(1) << "Swap()ing a ListValue inefficiently.";
94 98
95 // First deep copy to convert JSONStringValue to std::string and swap that 99 // First deep copy to convert JSONStringValue to std::string and swap that
96 // copy with |other|, which contains the new contents of |this|. 100 // copy with |other|, which contains the new contents of |this|.
97 std::unique_ptr<ListValue> copy(DeepCopy()); 101 std::unique_ptr<ListValue> copy(CreateDeepCopy());
98 copy->Swap(other); 102 copy->Swap(other);
99 103
100 // Then erase the contents of the current list and swap in the new contents, 104 // Then erase the contents of the current list and swap in the new contents,
101 // originally from |other|. 105 // originally from |other|.
102 Clear(); 106 Clear();
103 json_.reset(); 107 json_.reset();
104 ListValue::Swap(copy.get()); 108 ListValue::Swap(copy.get());
105 } 109 }
106 110
107 bool Remove(size_t index, std::unique_ptr<Value>* out) override { 111 bool Remove(size_t index, std::unique_ptr<Value>* out) override {
108 // If the caller won't take ownership of the removed value, just call up. 112 // If the caller won't take ownership of the removed value, just call up.
109 if (!out) 113 if (!out)
110 return ListValue::Remove(index, out); 114 return ListValue::Remove(index, out);
111 115
112 DVLOG(1) << "Remove()ing from a ListValue inefficiently."; 116 DVLOG(1) << "Remove()ing from a ListValue inefficiently.";
113 117
114 // Otherwise, remove the value while its still "owned" by this and copy it 118 // Otherwise, remove the value while its still "owned" by this and copy it
115 // to convert any JSONStringValues to std::string. 119 // to convert any JSONStringValues to std::string.
116 std::unique_ptr<Value> out_owned; 120 std::unique_ptr<Value> out_owned;
117 if (!ListValue::Remove(index, &out_owned)) 121 if (!ListValue::Remove(index, &out_owned))
118 return false; 122 return false;
119 123
120 out->reset(out_owned->DeepCopy()); 124 *out = out_owned->CreateDeepCopy();
121 125
122 return true; 126 return true;
123 } 127 }
124 128
125 private: 129 private:
126 std::unique_ptr<std::string> json_; 130 std::unique_ptr<std::string> json_;
127 131
128 DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue); 132 DISALLOW_COPY_AND_ASSIGN(ListHiddenRootValue);
129 }; 133 };
130 134
131 // A variant on StringValue that uses StringPiece instead of copying the string 135 // A variant on StringValue that uses StringPiece instead of copying the string
132 // into the Value. This can only be stored in a child of hidden root (above), 136 // into the Value. This can only be stored in a child of hidden root (above),
133 // otherwise the referenced string will not be guaranteed to outlive it. 137 // otherwise the referenced string will not be guaranteed to outlive it.
134 class JSONStringValue : public Value { 138 class JSONStringValue : public Value {
135 public: 139 public:
136 explicit JSONStringValue(const StringPiece& piece) 140 explicit JSONStringValue(StringPiece piece)
137 : Value(TYPE_STRING), 141 : Value(TYPE_STRING), string_piece_(piece) {}
138 string_piece_(piece) {
139 }
140 142
141 // Overridden from Value: 143 // Overridden from Value:
142 bool GetAsString(std::string* out_value) const override { 144 bool GetAsString(std::string* out_value) const override {
143 string_piece_.CopyToString(out_value); 145 string_piece_.CopyToString(out_value);
144 return true; 146 return true;
145 } 147 }
146 bool GetAsString(string16* out_value) const override { 148 bool GetAsString(string16* out_value) const override {
147 *out_value = UTF8ToUTF16(string_piece_); 149 *out_value = UTF8ToUTF16(string_piece_);
148 return true; 150 return true;
149 } 151 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 line_number_(0), 198 line_number_(0),
197 index_last_line_(0), 199 index_last_line_(0),
198 error_code_(JSONReader::JSON_NO_ERROR), 200 error_code_(JSONReader::JSON_NO_ERROR),
199 error_line_(0), 201 error_line_(0),
200 error_column_(0) { 202 error_column_(0) {
201 } 203 }
202 204
203 JSONParser::~JSONParser() { 205 JSONParser::~JSONParser() {
204 } 206 }
205 207
206 Value* JSONParser::Parse(const StringPiece& input) { 208 std::unique_ptr<Value> JSONParser::Parse(StringPiece input) {
207 std::unique_ptr<std::string> input_copy; 209 std::unique_ptr<std::string> input_copy;
208 // If the children of a JSON root can be detached, then hidden roots cannot 210 // If the children of a JSON root can be detached, then hidden roots cannot
209 // be used, so do not bother copying the input because StringPiece will not 211 // be used, so do not bother copying the input because StringPiece will not
210 // be used anywhere. 212 // be used anywhere.
211 if (!(options_ & JSON_DETACHABLE_CHILDREN)) { 213 if (!(options_ & JSON_DETACHABLE_CHILDREN)) {
212 input_copy.reset(new std::string(input.as_string())); 214 input_copy = WrapUnique(new std::string(input.as_string()));
213 start_pos_ = input_copy->data(); 215 start_pos_ = input_copy->data();
214 } else { 216 } else {
215 start_pos_ = input.data(); 217 start_pos_ = input.data();
216 } 218 }
217 pos_ = start_pos_; 219 pos_ = start_pos_;
218 end_pos_ = start_pos_ + input.length(); 220 end_pos_ = start_pos_ + input.length();
219 index_ = 0; 221 index_ = 0;
220 line_number_ = 1; 222 line_number_ = 1;
221 index_last_line_ = 0; 223 index_last_line_ = 0;
222 224
(...skipping 21 matching lines...) Expand all
244 if (!CanConsume(1) || (NextChar() && GetNextToken() != T_END_OF_INPUT)) { 246 if (!CanConsume(1) || (NextChar() && GetNextToken() != T_END_OF_INPUT)) {
245 ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1); 247 ReportError(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, 1);
246 return NULL; 248 return NULL;
247 } 249 }
248 } 250 }
249 251
250 // Dictionaries and lists can contain JSONStringValues, so wrap them in a 252 // Dictionaries and lists can contain JSONStringValues, so wrap them in a
251 // hidden root. 253 // hidden root.
252 if (!(options_ & JSON_DETACHABLE_CHILDREN)) { 254 if (!(options_ & JSON_DETACHABLE_CHILDREN)) {
253 if (root->IsType(Value::TYPE_DICTIONARY)) { 255 if (root->IsType(Value::TYPE_DICTIONARY)) {
254 return new DictionaryHiddenRootValue(input_copy.release(), root.get()); 256 return WrapUnique(
257 new DictionaryHiddenRootValue(std::move(input_copy), root.get()));
255 } else if (root->IsType(Value::TYPE_LIST)) { 258 } else if (root->IsType(Value::TYPE_LIST)) {
256 return new ListHiddenRootValue(input_copy.release(), root.get()); 259 return WrapUnique(
260 new ListHiddenRootValue(std::move(input_copy), root.get()));
257 } else if (root->IsType(Value::TYPE_STRING)) { 261 } else if (root->IsType(Value::TYPE_STRING)) {
258 // A string type could be a JSONStringValue, but because there's no 262 // A string type could be a JSONStringValue, but because there's no
259 // corresponding HiddenRootValue, the memory will be lost. Deep copy to 263 // corresponding HiddenRootValue, the memory will be lost. Deep copy to
260 // preserve it. 264 // preserve it.
261 return root->DeepCopy(); 265 return root->CreateDeepCopy();
262 } 266 }
263 } 267 }
264 268
265 // All other values can be returned directly. 269 // All other values can be returned directly.
266 return root.release(); 270 return root;
267 } 271 }
268 272
269 JSONReader::JsonParseError JSONParser::error_code() const { 273 JSONReader::JsonParseError JSONParser::error_code() const {
270 return error_code_; 274 return error_code_;
271 } 275 }
272 276
273 std::string JSONParser::GetErrorMessage() const { 277 std::string JSONParser::GetErrorMessage() const {
274 return FormatErrorMessage(error_line_, error_column_, 278 return FormatErrorMessage(error_line_, error_column_,
275 JSONReader::ErrorCodeToString(error_code_)); 279 JSONReader::ErrorCodeToString(error_code_));
276 } 280 }
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 const std::string& description) { 978 const std::string& description) {
975 if (line || column) { 979 if (line || column) {
976 return StringPrintf("Line: %i, column: %i, %s", 980 return StringPrintf("Line: %i, column: %i, %s",
977 line, column, description.c_str()); 981 line, column, description.c_str());
978 } 982 }
979 return description; 983 return description;
980 } 984 }
981 985
982 } // namespace internal 986 } // namespace internal
983 } // namespace base 987 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_parser.h ('k') | base/json/json_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698