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

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/V8StringUtil.cpp

Issue 2004313003: DevTools: migrate from OwnPtr to std::unique_ptr for inspector protocol classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebaselined Created 4 years, 6 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "platform/v8_inspector/V8StringUtil.h" 5 #include "platform/v8_inspector/V8StringUtil.h"
6 6
7 #include "platform/inspector_protocol/String16.h" 7 #include "platform/inspector_protocol/String16.h"
8 #include "platform/v8_inspector/V8DebuggerImpl.h" 8 #include "platform/v8_inspector/V8DebuggerImpl.h"
9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" 9 #include "platform/v8_inspector/V8InspectorSessionImpl.h"
10 #include "platform/v8_inspector/V8Regex.h" 10 #include "platform/v8_inspector/V8Regex.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 for (unsigned i = 0; i < text.length(); i++) { 89 for (unsigned i = 0; i < text.length(); i++) {
90 if (specials.find(text[i]) != kNotFound) 90 if (specials.find(text[i]) != kNotFound)
91 result.append('\\'); 91 result.append('\\');
92 result.append(text[i]); 92 result.append(text[i]);
93 } 93 }
94 94
95 return result.toString(); 95 return result.toString();
96 } 96 }
97 97
98 PassOwnPtr<protocol::Vector<unsigned>> lineEndings(const String16& text) 98 std::unique_ptr<protocol::Vector<unsigned>> lineEndings(const String16& text)
99 { 99 {
100 OwnPtr<protocol::Vector<unsigned>> result(adoptPtr(new protocol::Vector<unsi gned>())); 100 std::unique_ptr<protocol::Vector<unsigned>> result(new protocol::Vector<unsi gned>());
101 101
102 unsigned start = 0; 102 unsigned start = 0;
103 while (start < text.length()) { 103 while (start < text.length()) {
104 size_t lineEnd = text.find('\n', start); 104 size_t lineEnd = text.find('\n', start);
105 if (lineEnd == kNotFound) 105 if (lineEnd == kNotFound)
106 break; 106 break;
107 107
108 result->append(static_cast<unsigned>(lineEnd)); 108 result->append(static_cast<unsigned>(lineEnd));
109 start = lineEnd + 1; 109 start = lineEnd + 1;
110 } 110 }
111 result->append(text.length()); 111 result->append(text.length());
112 112
113 return result; 113 return result;
114 } 114 }
115 115
116 protocol::Vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Re gex& regex, const String16& text) 116 protocol::Vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Re gex& regex, const String16& text)
117 { 117 {
118 protocol::Vector<std::pair<int, String16>> result; 118 protocol::Vector<std::pair<int, String16>> result;
119 if (text.isEmpty()) 119 if (text.isEmpty())
120 return result; 120 return result;
121 121
122 OwnPtr<protocol::Vector<unsigned>> endings(lineEndings(text)); 122 std::unique_ptr<protocol::Vector<unsigned>> endings(lineEndings(text));
123 unsigned size = endings->size(); 123 unsigned size = endings->size();
124 unsigned start = 0; 124 unsigned start = 0;
125 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) { 125 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) {
126 unsigned lineEnd = endings->at(lineNumber); 126 unsigned lineEnd = endings->at(lineNumber);
127 String16 line = text.substring(start, lineEnd - start); 127 String16 line = text.substring(start, lineEnd - start);
128 if (line.endsWith('\r')) 128 if (line.endsWith('\r'))
129 line = line.substring(0, line.length() - 1); 129 line = line.substring(0, line.length() - 1);
130 130
131 int matchLength; 131 int matchLength;
132 if (regex.match(line, 0, &matchLength) != -1) 132 if (regex.match(line, 0, &matchLength) != -1)
133 result.append(std::pair<int, String16>(lineNumber, line)); 133 result.append(std::pair<int, String16>(lineNumber, line));
134 134
135 start = lineEnd + 1; 135 start = lineEnd + 1;
136 } 136 }
137 return result; 137 return result;
138 } 138 }
139 139
140 PassOwnPtr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(int lineNu mber, const String16& lineContent) 140 std::unique_ptr<protocol::Debugger::SearchMatch> buildObjectForSearchMatch(int l ineNumber, const String16& lineContent)
141 { 141 {
142 return protocol::Debugger::SearchMatch::create() 142 return protocol::Debugger::SearchMatch::create()
143 .setLineNumber(lineNumber) 143 .setLineNumber(lineNumber)
144 .setLineContent(lineContent) 144 .setLineContent(lineContent)
145 .build(); 145 .build();
146 } 146 }
147 147
148 PassOwnPtr<V8Regex> createSearchRegex(V8DebuggerImpl* debugger, const String16& query, bool caseSensitive, bool isRegex) 148 std::unique_ptr<V8Regex> createSearchRegex(V8DebuggerImpl* debugger, const Strin g16& query, bool caseSensitive, bool isRegex)
149 { 149 {
150 String16 regexSource = isRegex ? query : createSearchRegexSource(query); 150 String16 regexSource = isRegex ? query : createSearchRegexSource(query);
151 return adoptPtr(new V8Regex(debugger, regexSource, caseSensitive)); 151 return wrapUnique(new V8Regex(debugger, regexSource, caseSensitive));
152 } 152 }
153 153
154 } // namespace 154 } // namespace
155 155
156 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string) 156 v8::Local<v8::String> toV8String(v8::Isolate* isolate, const String16& string)
157 { 157 {
158 if (string.isEmpty()) 158 if (string.isEmpty())
159 return v8::String::Empty(isolate); 159 return v8::String::Empty(isolate);
160 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe cked(); 160 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kNormal, string.length()).ToLocalChe cked();
161 } 161 }
162 162
163 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1 6& string) 163 v8::Local<v8::String> toV8StringInternalized(v8::Isolate* isolate, const String1 6& string)
164 { 164 {
165 if (string.isEmpty()) 165 if (string.isEmpty())
166 return v8::String::Empty(isolate); 166 return v8::String::Empty(isolate);
167 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kInternalized, string.length()).ToLo calChecked(); 167 return v8::String::NewFromTwoByte(isolate, reinterpret_cast<const uint16_t*> (string.characters16()), v8::NewStringType::kInternalized, string.length()).ToLo calChecked();
168 } 168 }
169 169
170 String16 toProtocolString(v8::Local<v8::String> value) 170 String16 toProtocolString(v8::Local<v8::String> value)
171 { 171 {
172 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) 172 if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
173 return String16(); 173 return String16();
174 OwnPtr<UChar[]> buffer = adoptArrayPtr(new UChar[value->Length()]); 174 std::unique_ptr<UChar[]> buffer(new UChar[value->Length()]);
175 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length()); 175 value->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, value->Length());
176 return String16(buffer.get(), value->Length()); 176 return String16(buffer.get(), value->Length());
177 } 177 }
178 178
179 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value) 179 String16 toProtocolStringWithTypeCheck(v8::Local<v8::Value> value)
180 { 180 {
181 if (value.IsEmpty() || !value->IsString()) 181 if (value.IsEmpty() || !value->IsString())
182 return String16(); 182 return String16();
183 return toProtocolString(value.As<v8::String>()); 183 return toProtocolString(value.As<v8::String>());
184 } 184 }
185 185
186 namespace V8ContentSearchUtil { 186 namespace V8ContentSearchUtil {
187 187
188 String16 findSourceURL(const String16& content, bool multiline, bool* deprecated ) 188 String16 findSourceURL(const String16& content, bool multiline, bool* deprecated )
189 { 189 {
190 return findMagicComment(content, "sourceURL", multiline, deprecated); 190 return findMagicComment(content, "sourceURL", multiline, deprecated);
191 } 191 }
192 192
193 String16 findSourceMapURL(const String16& content, bool multiline, bool* depreca ted) 193 String16 findSourceMapURL(const String16& content, bool multiline, bool* depreca ted)
194 { 194 {
195 return findMagicComment(content, "sourceMappingURL", multiline, deprecated); 195 return findMagicComment(content, "sourceMappingURL", multiline, deprecated);
196 } 196 }
197 197
198 PassOwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> searchInTextByLines (V8InspectorSession* session, const String16& text, const String16& query, const bool caseSensitive, const bool isRegex) 198 std::unique_ptr<protocol::Array<protocol::Debugger::SearchMatch>> searchInTextBy Lines(V8InspectorSession* session, const String16& text, const String16& query, const bool caseSensitive, const bool isRegex)
199 { 199 {
200 OwnPtr<protocol::Array<protocol::Debugger::SearchMatch>> result = protocol:: Array<protocol::Debugger::SearchMatch>::create(); 200 std::unique_ptr<protocol::Array<protocol::Debugger::SearchMatch>> result = p rotocol::Array<protocol::Debugger::SearchMatch>::create();
201 OwnPtr<V8Regex> regex = createSearchRegex(static_cast<V8InspectorSessionImpl *>(session)->debugger(), query, caseSensitive, isRegex); 201 std::unique_ptr<V8Regex> regex = createSearchRegex(static_cast<V8InspectorSe ssionImpl*>(session)->debugger(), query, caseSensitive, isRegex);
202 protocol::Vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLi nes(*regex.get(), text); 202 protocol::Vector<std::pair<int, String16>> matches = scriptRegexpMatchesByLi nes(*regex.get(), text);
203 203
204 for (const auto& match : matches) 204 for (const auto& match : matches)
205 result->addItem(buildObjectForSearchMatch(match.first, match.second)); 205 result->addItem(buildObjectForSearchMatch(match.first, match.second));
206 206
207 return result; 207 return result;
208 } 208 }
209 209
210 } // namespace V8ContentSearchUtil 210 } // namespace V8ContentSearchUtil
211 211
212 PassOwnPtr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context, v8:: Local<v8::Value> value, int maxDepth) 212 std::unique_ptr<protocol::Value> toProtocolValue(v8::Local<v8::Context> context, v8::Local<v8::Value> value, int maxDepth)
213 { 213 {
214 if (value.IsEmpty()) { 214 if (value.IsEmpty()) {
215 NOTREACHED(); 215 NOTREACHED();
216 return nullptr; 216 return nullptr;
217 } 217 }
218 218
219 if (!maxDepth) 219 if (!maxDepth)
220 return nullptr; 220 return nullptr;
221 maxDepth--; 221 maxDepth--;
222 222
223 if (value->IsNull() || value->IsUndefined()) 223 if (value->IsNull() || value->IsUndefined())
224 return protocol::Value::null(); 224 return protocol::Value::null();
225 if (value->IsBoolean()) 225 if (value->IsBoolean())
226 return protocol::FundamentalValue::create(value.As<v8::Boolean>()->Value ()); 226 return protocol::FundamentalValue::create(value.As<v8::Boolean>()->Value ());
227 if (value->IsNumber()) 227 if (value->IsNumber())
228 return protocol::FundamentalValue::create(value.As<v8::Number>()->Value( )); 228 return protocol::FundamentalValue::create(value.As<v8::Number>()->Value( ));
229 if (value->IsString()) 229 if (value->IsString())
230 return protocol::StringValue::create(toProtocolString(value.As<v8::Strin g>())); 230 return protocol::StringValue::create(toProtocolString(value.As<v8::Strin g>()));
231 if (value->IsArray()) { 231 if (value->IsArray()) {
232 v8::Local<v8::Array> array = value.As<v8::Array>(); 232 v8::Local<v8::Array> array = value.As<v8::Array>();
233 OwnPtr<protocol::ListValue> inspectorArray = protocol::ListValue::create (); 233 std::unique_ptr<protocol::ListValue> inspectorArray = protocol::ListValu e::create();
234 uint32_t length = array->Length(); 234 uint32_t length = array->Length();
235 for (uint32_t i = 0; i < length; i++) { 235 for (uint32_t i = 0; i < length; i++) {
236 v8::Local<v8::Value> value; 236 v8::Local<v8::Value> value;
237 if (!array->Get(context, i).ToLocal(&value)) 237 if (!array->Get(context, i).ToLocal(&value))
238 return nullptr; 238 return nullptr;
239 OwnPtr<protocol::Value> element = toProtocolValue(context, value, ma xDepth); 239 std::unique_ptr<protocol::Value> element = toProtocolValue(context, value, maxDepth);
240 if (!element) 240 if (!element)
241 return nullptr; 241 return nullptr;
242 inspectorArray->pushValue(std::move(element)); 242 inspectorArray->pushValue(std::move(element));
243 } 243 }
244 return std::move(inspectorArray); 244 return std::move(inspectorArray);
245 } 245 }
246 if (value->IsObject()) { 246 if (value->IsObject()) {
247 OwnPtr<protocol::DictionaryValue> jsonObject = protocol::DictionaryValue ::create(); 247 std::unique_ptr<protocol::DictionaryValue> jsonObject = protocol::Dictio naryValue::create();
248 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); 248 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
249 v8::Local<v8::Array> propertyNames; 249 v8::Local<v8::Array> propertyNames;
250 if (!object->GetPropertyNames(context).ToLocal(&propertyNames)) 250 if (!object->GetPropertyNames(context).ToLocal(&propertyNames))
251 return nullptr; 251 return nullptr;
252 uint32_t length = propertyNames->Length(); 252 uint32_t length = propertyNames->Length();
253 for (uint32_t i = 0; i < length; i++) { 253 for (uint32_t i = 0; i < length; i++) {
254 v8::Local<v8::Value> name; 254 v8::Local<v8::Value> name;
255 if (!propertyNames->Get(context, i).ToLocal(&name)) 255 if (!propertyNames->Get(context, i).ToLocal(&name))
256 return nullptr; 256 return nullptr;
257 // FIXME(yurys): v8::Object should support GetOwnPropertyNames 257 // FIXME(yurys): v8::Object should support GetOwnPropertyNames
258 if (name->IsString()) { 258 if (name->IsString()) {
259 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedPrope rty(context, v8::Local<v8::String>::Cast(name)); 259 v8::Maybe<bool> hasRealNamedProperty = object->HasRealNamedPrope rty(context, v8::Local<v8::String>::Cast(name));
260 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.From Just()) 260 if (!hasRealNamedProperty.IsJust() || !hasRealNamedProperty.From Just())
261 continue; 261 continue;
262 } 262 }
263 v8::Local<v8::String> propertyName; 263 v8::Local<v8::String> propertyName;
264 if (!name->ToString(context).ToLocal(&propertyName)) 264 if (!name->ToString(context).ToLocal(&propertyName))
265 continue; 265 continue;
266 v8::Local<v8::Value> property; 266 v8::Local<v8::Value> property;
267 if (!object->Get(context, name).ToLocal(&property)) 267 if (!object->Get(context, name).ToLocal(&property))
268 return nullptr; 268 return nullptr;
269 OwnPtr<protocol::Value> propertyValue = toProtocolValue(context, pro perty, maxDepth); 269 std::unique_ptr<protocol::Value> propertyValue = toProtocolValue(con text, property, maxDepth);
270 if (!propertyValue) 270 if (!propertyValue)
271 return nullptr; 271 return nullptr;
272 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue)); 272 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue));
273 } 273 }
274 return std::move(jsonObject); 274 return std::move(jsonObject);
275 } 275 }
276 NOTREACHED(); 276 NOTREACHED();
277 return nullptr; 277 return nullptr;
278 } 278 }
279 279
280 } // namespace blink 280 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698