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

Unified Diff: components/safe_json/testing_json_parser.cc

Issue 1214903010: Make JSONParser a pure interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: review Created 5 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 side-by-side diff with in-line comments
Download patch
Index: components/safe_json/testing_json_parser.cc
diff --git a/components/safe_json/testing_json_parser.cc b/components/safe_json/testing_json_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ea7e9c5469b45cc7378fa0cffe6df8ee75dce7ad
--- /dev/null
+++ b/components/safe_json/testing_json_parser.cc
@@ -0,0 +1,58 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/safe_json/testing_json_parser.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/json/json_reader.h"
+#include "base/location.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/values.h"
+
+namespace safe_json {
+
+namespace {
+
+SafeJsonParser* CreateTestingJsonParser(
+ const std::string& unsafe_json,
+ const SafeJsonParser::SuccessCallback& success_callback,
+ const SafeJsonParser::ErrorCallback& error_callback) {
+ return new TestingJsonParser(unsafe_json, success_callback, error_callback);
+}
+
+} // namespace
+
+TestingJsonParser::ScopedFactoryOverride::ScopedFactoryOverride() {
+ SafeJsonParser::SetFactoryForTesting(&CreateTestingJsonParser);
+}
+
+TestingJsonParser::ScopedFactoryOverride::~ScopedFactoryOverride() {
+ SafeJsonParser::SetFactoryForTesting(nullptr);
+}
+
+TestingJsonParser::TestingJsonParser(const std::string& unsafe_json,
+ const SuccessCallback& success_callback,
+ const ErrorCallback& error_callback)
+ : unsafe_json_(unsafe_json),
+ success_callback_(success_callback),
+ error_callback_(error_callback) {}
+
+TestingJsonParser::~TestingJsonParser() {}
+
+void TestingJsonParser::Start() {
+ int error_code;
+ std::string error;
+ scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError(
+ unsafe_json_, base::JSON_PARSE_RFC, &error_code, &error);
+
+ // Run the callback asynchronously.
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE, value ? base::Bind(success_callback_, base::Passed(&value))
+ : base::Bind(error_callback_, error));
+ base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
+}
+
+} // namespace
« components/safe_json/BUILD.gn ('K') | « components/safe_json/testing_json_parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698