OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/profile_resetter/jtl_interpreter.h" | 5 #include "chrome/browser/profile_resetter/jtl_interpreter.h" |
6 | 6 |
7 #include <numeric> | 7 #include <numeric> |
8 | 8 |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 base::DictionaryValue* working_memory_; | 105 base::DictionaryValue* working_memory_; |
106 // Whether a runtime error occurred. | 106 // Whether a runtime error occurred. |
107 bool error_; | 107 bool error_; |
108 DISALLOW_COPY_AND_ASSIGN(ExecutionContext); | 108 DISALLOW_COPY_AND_ASSIGN(ExecutionContext); |
109 }; | 109 }; |
110 | 110 |
111 class NavigateOperation : public Operation { | 111 class NavigateOperation : public Operation { |
112 public: | 112 public: |
113 explicit NavigateOperation(const std::string& hashed_key) | 113 explicit NavigateOperation(const std::string& hashed_key) |
114 : hashed_key_(hashed_key) {} | 114 : hashed_key_(hashed_key) {} |
115 virtual ~NavigateOperation() {} | 115 ~NavigateOperation() override {} |
116 virtual bool Execute(ExecutionContext* context) override { | 116 bool Execute(ExecutionContext* context) override { |
117 const base::DictionaryValue* dict = NULL; | 117 const base::DictionaryValue* dict = NULL; |
118 if (!context->current_node()->GetAsDictionary(&dict)) { | 118 if (!context->current_node()->GetAsDictionary(&dict)) { |
119 // Just ignore this node gracefully as this navigation is a dead end. | 119 // Just ignore this node gracefully as this navigation is a dead end. |
120 // If this NavigateOperation occurred after a NavigateAny operation, those | 120 // If this NavigateOperation occurred after a NavigateAny operation, those |
121 // may still be fulfillable, so we allow continuing the execution of the | 121 // may still be fulfillable, so we allow continuing the execution of the |
122 // sentence on other nodes. | 122 // sentence on other nodes. |
123 return true; | 123 return true; |
124 } | 124 } |
125 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { | 125 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { |
126 if (context->GetHash(i.key()) != hashed_key_) | 126 if (context->GetHash(i.key()) != hashed_key_) |
127 continue; | 127 continue; |
128 context->stack()->push_back(&i.value()); | 128 context->stack()->push_back(&i.value()); |
129 bool continue_traversal = context->ContinueExecution(); | 129 bool continue_traversal = context->ContinueExecution(); |
130 context->stack()->pop_back(); | 130 context->stack()->pop_back(); |
131 if (!continue_traversal) | 131 if (!continue_traversal) |
132 return false; | 132 return false; |
133 } | 133 } |
134 return true; | 134 return true; |
135 } | 135 } |
136 | 136 |
137 private: | 137 private: |
138 std::string hashed_key_; | 138 std::string hashed_key_; |
139 DISALLOW_COPY_AND_ASSIGN(NavigateOperation); | 139 DISALLOW_COPY_AND_ASSIGN(NavigateOperation); |
140 }; | 140 }; |
141 | 141 |
142 class NavigateAnyOperation : public Operation { | 142 class NavigateAnyOperation : public Operation { |
143 public: | 143 public: |
144 NavigateAnyOperation() {} | 144 NavigateAnyOperation() {} |
145 virtual ~NavigateAnyOperation() {} | 145 ~NavigateAnyOperation() override {} |
146 virtual bool Execute(ExecutionContext* context) override { | 146 bool Execute(ExecutionContext* context) override { |
147 const base::DictionaryValue* dict = NULL; | 147 const base::DictionaryValue* dict = NULL; |
148 const base::ListValue* list = NULL; | 148 const base::ListValue* list = NULL; |
149 if (context->current_node()->GetAsDictionary(&dict)) { | 149 if (context->current_node()->GetAsDictionary(&dict)) { |
150 for (base::DictionaryValue::Iterator i(*dict); | 150 for (base::DictionaryValue::Iterator i(*dict); |
151 !i.IsAtEnd(); i.Advance()) { | 151 !i.IsAtEnd(); i.Advance()) { |
152 context->stack()->push_back(&i.value()); | 152 context->stack()->push_back(&i.value()); |
153 bool continue_traversal = context->ContinueExecution(); | 153 bool continue_traversal = context->ContinueExecution(); |
154 context->stack()->pop_back(); | 154 context->stack()->pop_back(); |
155 if (!continue_traversal) | 155 if (!continue_traversal) |
156 return false; | 156 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
170 return true; | 170 return true; |
171 } | 171 } |
172 | 172 |
173 private: | 173 private: |
174 DISALLOW_COPY_AND_ASSIGN(NavigateAnyOperation); | 174 DISALLOW_COPY_AND_ASSIGN(NavigateAnyOperation); |
175 }; | 175 }; |
176 | 176 |
177 class NavigateBackOperation : public Operation { | 177 class NavigateBackOperation : public Operation { |
178 public: | 178 public: |
179 NavigateBackOperation() {} | 179 NavigateBackOperation() {} |
180 virtual ~NavigateBackOperation() {} | 180 ~NavigateBackOperation() override {} |
181 virtual bool Execute(ExecutionContext* context) override { | 181 bool Execute(ExecutionContext* context) override { |
182 const base::Value* current_node = context->current_node(); | 182 const base::Value* current_node = context->current_node(); |
183 context->stack()->pop_back(); | 183 context->stack()->pop_back(); |
184 bool continue_traversal = context->ContinueExecution(); | 184 bool continue_traversal = context->ContinueExecution(); |
185 context->stack()->push_back(current_node); | 185 context->stack()->push_back(current_node); |
186 return continue_traversal; | 186 return continue_traversal; |
187 } | 187 } |
188 | 188 |
189 private: | 189 private: |
190 DISALLOW_COPY_AND_ASSIGN(NavigateBackOperation); | 190 DISALLOW_COPY_AND_ASSIGN(NavigateBackOperation); |
191 }; | 191 }; |
192 | 192 |
193 class StoreValue : public Operation { | 193 class StoreValue : public Operation { |
194 public: | 194 public: |
195 StoreValue(const std::string& hashed_name, scoped_ptr<base::Value> value) | 195 StoreValue(const std::string& hashed_name, scoped_ptr<base::Value> value) |
196 : hashed_name_(hashed_name), | 196 : hashed_name_(hashed_name), |
197 value_(value.Pass()) { | 197 value_(value.Pass()) { |
198 DCHECK(base::IsStringUTF8(hashed_name)); | 198 DCHECK(base::IsStringUTF8(hashed_name)); |
199 DCHECK(value_); | 199 DCHECK(value_); |
200 } | 200 } |
201 virtual ~StoreValue() {} | 201 ~StoreValue() override {} |
202 virtual bool Execute(ExecutionContext* context) override { | 202 bool Execute(ExecutionContext* context) override { |
203 context->working_memory()->Set(hashed_name_, value_->DeepCopy()); | 203 context->working_memory()->Set(hashed_name_, value_->DeepCopy()); |
204 return context->ContinueExecution(); | 204 return context->ContinueExecution(); |
205 } | 205 } |
206 | 206 |
207 private: | 207 private: |
208 std::string hashed_name_; | 208 std::string hashed_name_; |
209 scoped_ptr<base::Value> value_; | 209 scoped_ptr<base::Value> value_; |
210 DISALLOW_COPY_AND_ASSIGN(StoreValue); | 210 DISALLOW_COPY_AND_ASSIGN(StoreValue); |
211 }; | 211 }; |
212 | 212 |
213 class CompareStoredValue : public Operation { | 213 class CompareStoredValue : public Operation { |
214 public: | 214 public: |
215 CompareStoredValue(const std::string& hashed_name, | 215 CompareStoredValue(const std::string& hashed_name, |
216 scoped_ptr<base::Value> value, | 216 scoped_ptr<base::Value> value, |
217 scoped_ptr<base::Value> default_value) | 217 scoped_ptr<base::Value> default_value) |
218 : hashed_name_(hashed_name), | 218 : hashed_name_(hashed_name), |
219 value_(value.Pass()), | 219 value_(value.Pass()), |
220 default_value_(default_value.Pass()) { | 220 default_value_(default_value.Pass()) { |
221 DCHECK(base::IsStringUTF8(hashed_name)); | 221 DCHECK(base::IsStringUTF8(hashed_name)); |
222 DCHECK(value_); | 222 DCHECK(value_); |
223 DCHECK(default_value_); | 223 DCHECK(default_value_); |
224 } | 224 } |
225 virtual ~CompareStoredValue() {} | 225 ~CompareStoredValue() override {} |
226 virtual bool Execute(ExecutionContext* context) override { | 226 bool Execute(ExecutionContext* context) override { |
227 const base::Value* actual_value = NULL; | 227 const base::Value* actual_value = NULL; |
228 if (!context->working_memory()->Get(hashed_name_, &actual_value)) | 228 if (!context->working_memory()->Get(hashed_name_, &actual_value)) |
229 actual_value = default_value_.get(); | 229 actual_value = default_value_.get(); |
230 if (!value_->Equals(actual_value)) | 230 if (!value_->Equals(actual_value)) |
231 return true; | 231 return true; |
232 return context->ContinueExecution(); | 232 return context->ContinueExecution(); |
233 } | 233 } |
234 | 234 |
235 private: | 235 private: |
236 std::string hashed_name_; | 236 std::string hashed_name_; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 270 |
271 // Stores the hash of the registerable domain name -- as in, the portion of the | 271 // Stores the hash of the registerable domain name -- as in, the portion of the |
272 // domain that is registerable, as opposed to controlled by a registrar; without | 272 // domain that is registerable, as opposed to controlled by a registrar; without |
273 // subdomains -- of the URL represented by the current node into working memory. | 273 // subdomains -- of the URL represented by the current node into working memory. |
274 class StoreNodeRegisterableDomain : public Operation { | 274 class StoreNodeRegisterableDomain : public Operation { |
275 public: | 275 public: |
276 explicit StoreNodeRegisterableDomain(const std::string& hashed_name) | 276 explicit StoreNodeRegisterableDomain(const std::string& hashed_name) |
277 : hashed_name_(hashed_name) { | 277 : hashed_name_(hashed_name) { |
278 DCHECK(base::IsStringUTF8(hashed_name)); | 278 DCHECK(base::IsStringUTF8(hashed_name)); |
279 } | 279 } |
280 virtual ~StoreNodeRegisterableDomain() {} | 280 ~StoreNodeRegisterableDomain() override {} |
281 virtual bool Execute(ExecutionContext* context) override { | 281 bool Execute(ExecutionContext* context) override { |
282 std::string possibly_invalid_url; | 282 std::string possibly_invalid_url; |
283 std::string domain; | 283 std::string domain; |
284 if (!context->current_node()->GetAsString(&possibly_invalid_url) || | 284 if (!context->current_node()->GetAsString(&possibly_invalid_url) || |
285 !GetRegisterableDomain(possibly_invalid_url, &domain)) | 285 !GetRegisterableDomain(possibly_invalid_url, &domain)) |
286 return true; | 286 return true; |
287 context->working_memory()->Set( | 287 context->working_memory()->Set( |
288 hashed_name_, new base::StringValue(context->GetHash(domain))); | 288 hashed_name_, new base::StringValue(context->GetHash(domain))); |
289 return context->ContinueExecution(); | 289 return context->ContinueExecution(); |
290 } | 290 } |
291 | 291 |
(...skipping 27 matching lines...) Expand all Loading... |
319 return true; | 319 return true; |
320 } | 320 } |
321 | 321 |
322 std::string hashed_name_; | 322 std::string hashed_name_; |
323 DISALLOW_COPY_AND_ASSIGN(StoreNodeRegisterableDomain); | 323 DISALLOW_COPY_AND_ASSIGN(StoreNodeRegisterableDomain); |
324 }; | 324 }; |
325 | 325 |
326 class CompareNodeBool : public Operation { | 326 class CompareNodeBool : public Operation { |
327 public: | 327 public: |
328 explicit CompareNodeBool(bool value) : value_(value) {} | 328 explicit CompareNodeBool(bool value) : value_(value) {} |
329 virtual ~CompareNodeBool() {} | 329 ~CompareNodeBool() override {} |
330 virtual bool Execute(ExecutionContext* context) override { | 330 bool Execute(ExecutionContext* context) override { |
331 bool actual_value = false; | 331 bool actual_value = false; |
332 if (!context->current_node()->GetAsBoolean(&actual_value)) | 332 if (!context->current_node()->GetAsBoolean(&actual_value)) |
333 return true; | 333 return true; |
334 if (actual_value != value_) | 334 if (actual_value != value_) |
335 return true; | 335 return true; |
336 return context->ContinueExecution(); | 336 return context->ContinueExecution(); |
337 } | 337 } |
338 | 338 |
339 private: | 339 private: |
340 bool value_; | 340 bool value_; |
341 DISALLOW_COPY_AND_ASSIGN(CompareNodeBool); | 341 DISALLOW_COPY_AND_ASSIGN(CompareNodeBool); |
342 }; | 342 }; |
343 | 343 |
344 class CompareNodeHash : public Operation { | 344 class CompareNodeHash : public Operation { |
345 public: | 345 public: |
346 explicit CompareNodeHash(const std::string& hashed_value) | 346 explicit CompareNodeHash(const std::string& hashed_value) |
347 : hashed_value_(hashed_value) {} | 347 : hashed_value_(hashed_value) {} |
348 virtual ~CompareNodeHash() {} | 348 ~CompareNodeHash() override {} |
349 virtual bool Execute(ExecutionContext* context) override { | 349 bool Execute(ExecutionContext* context) override { |
350 std::string actual_hash; | 350 std::string actual_hash; |
351 if (!context->GetValueHash(*context->current_node(), &actual_hash) || | 351 if (!context->GetValueHash(*context->current_node(), &actual_hash) || |
352 actual_hash != hashed_value_) | 352 actual_hash != hashed_value_) |
353 return true; | 353 return true; |
354 return context->ContinueExecution(); | 354 return context->ContinueExecution(); |
355 } | 355 } |
356 | 356 |
357 private: | 357 private: |
358 std::string hashed_value_; | 358 std::string hashed_value_; |
359 DISALLOW_COPY_AND_ASSIGN(CompareNodeHash); | 359 DISALLOW_COPY_AND_ASSIGN(CompareNodeHash); |
360 }; | 360 }; |
361 | 361 |
362 class CompareNodeHashNot : public Operation { | 362 class CompareNodeHashNot : public Operation { |
363 public: | 363 public: |
364 explicit CompareNodeHashNot(const std::string& hashed_value) | 364 explicit CompareNodeHashNot(const std::string& hashed_value) |
365 : hashed_value_(hashed_value) {} | 365 : hashed_value_(hashed_value) {} |
366 virtual ~CompareNodeHashNot() {} | 366 ~CompareNodeHashNot() override {} |
367 virtual bool Execute(ExecutionContext* context) override { | 367 bool Execute(ExecutionContext* context) override { |
368 std::string actual_hash; | 368 std::string actual_hash; |
369 if (context->GetValueHash(*context->current_node(), &actual_hash) && | 369 if (context->GetValueHash(*context->current_node(), &actual_hash) && |
370 actual_hash == hashed_value_) | 370 actual_hash == hashed_value_) |
371 return true; | 371 return true; |
372 return context->ContinueExecution(); | 372 return context->ContinueExecution(); |
373 } | 373 } |
374 | 374 |
375 private: | 375 private: |
376 std::string hashed_value_; | 376 std::string hashed_value_; |
377 DISALLOW_COPY_AND_ASSIGN(CompareNodeHashNot); | 377 DISALLOW_COPY_AND_ASSIGN(CompareNodeHashNot); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 class CompareNodeSubstring : public Operation { | 410 class CompareNodeSubstring : public Operation { |
411 public: | 411 public: |
412 explicit CompareNodeSubstring(const std::string& hashed_pattern, | 412 explicit CompareNodeSubstring(const std::string& hashed_pattern, |
413 size_t pattern_length, | 413 size_t pattern_length, |
414 uint32 pattern_sum) | 414 uint32 pattern_sum) |
415 : hashed_pattern_(hashed_pattern), | 415 : hashed_pattern_(hashed_pattern), |
416 pattern_length_(pattern_length), | 416 pattern_length_(pattern_length), |
417 pattern_sum_(pattern_sum) { | 417 pattern_sum_(pattern_sum) { |
418 DCHECK(pattern_length_); | 418 DCHECK(pattern_length_); |
419 } | 419 } |
420 virtual ~CompareNodeSubstring() {} | 420 ~CompareNodeSubstring() override {} |
421 virtual bool Execute(ExecutionContext* context) override { | 421 bool Execute(ExecutionContext* context) override { |
422 std::string value_as_string; | 422 std::string value_as_string; |
423 if (!context->current_node()->GetAsString(&value_as_string) || | 423 if (!context->current_node()->GetAsString(&value_as_string) || |
424 !pattern_length_ || value_as_string.size() < pattern_length_) | 424 !pattern_length_ || value_as_string.size() < pattern_length_) |
425 return true; | 425 return true; |
426 // Go over the string with a sliding window. Meanwhile, maintain the sum in | 426 // Go over the string with a sliding window. Meanwhile, maintain the sum in |
427 // an incremental fashion, and only calculate the SHA-256 hash when the sum | 427 // an incremental fashion, and only calculate the SHA-256 hash when the sum |
428 // checks out so as to improve performance. | 428 // checks out so as to improve performance. |
429 std::string::const_iterator window_begin = value_as_string.begin(); | 429 std::string::const_iterator window_begin = value_as_string.begin(); |
430 std::string::const_iterator window_end = window_begin + pattern_length_ - 1; | 430 std::string::const_iterator window_end = window_begin + pattern_length_ - 1; |
431 uint32 window_sum = | 431 uint32 window_sum = |
(...skipping 11 matching lines...) Expand all Loading... |
443 private: | 443 private: |
444 std::string hashed_pattern_; | 444 std::string hashed_pattern_; |
445 size_t pattern_length_; | 445 size_t pattern_length_; |
446 uint32 pattern_sum_; | 446 uint32 pattern_sum_; |
447 DISALLOW_COPY_AND_ASSIGN(CompareNodeSubstring); | 447 DISALLOW_COPY_AND_ASSIGN(CompareNodeSubstring); |
448 }; | 448 }; |
449 | 449 |
450 class StopExecutingSentenceOperation : public Operation { | 450 class StopExecutingSentenceOperation : public Operation { |
451 public: | 451 public: |
452 StopExecutingSentenceOperation() {} | 452 StopExecutingSentenceOperation() {} |
453 virtual ~StopExecutingSentenceOperation() {} | 453 ~StopExecutingSentenceOperation() override {} |
454 virtual bool Execute(ExecutionContext* context) override { | 454 bool Execute(ExecutionContext* context) override { return false; } |
455 return false; | |
456 } | |
457 | 455 |
458 private: | 456 private: |
459 DISALLOW_COPY_AND_ASSIGN(StopExecutingSentenceOperation); | 457 DISALLOW_COPY_AND_ASSIGN(StopExecutingSentenceOperation); |
460 }; | 458 }; |
461 | 459 |
462 class Parser { | 460 class Parser { |
463 public: | 461 public: |
464 explicit Parser(const std::string& program) | 462 explicit Parser(const std::string& program) |
465 : program_(program), | 463 : program_(program), |
466 next_instruction_index_(0u) {} | 464 next_instruction_index_(0u) {} |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 return working_memory_->GetString(hashed_key, output); | 737 return working_memory_->GetString(hashed_key, output); |
740 } | 738 } |
741 | 739 |
742 int JtlInterpreter::CalculateProgramChecksum() const { | 740 int JtlInterpreter::CalculateProgramChecksum() const { |
743 uint8 digest[3] = {}; | 741 uint8 digest[3] = {}; |
744 crypto::SHA256HashString(program_, digest, arraysize(digest)); | 742 crypto::SHA256HashString(program_, digest, arraysize(digest)); |
745 return static_cast<uint32>(digest[0]) << 16 | | 743 return static_cast<uint32>(digest[0]) << 16 | |
746 static_cast<uint32>(digest[1]) << 8 | | 744 static_cast<uint32>(digest[1]) << 8 | |
747 static_cast<uint32>(digest[2]); | 745 static_cast<uint32>(digest[2]); |
748 } | 746 } |
OLD | NEW |