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

Side by Side Diff: components/safe_json/json_sanitizer.cc

Issue 1203083002: Add a JSON sanitizer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: x Created 5 years, 5 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/safe_json/json_sanitizer.h"
6
7 #if defined(OS_ANDROID)
8 #error Build json_sanitizer_android.cc instead of this file on Android.
9 #endif
10
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/json/json_writer.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string_util.h"
16 #include "base/values.h"
17 #include "components/safe_json/safe_json_parser.h"
18
19 namespace safe_json {
20
21 namespace {
22
23 class OopJsonSanitizer : public JsonSanitizer {
24 public:
25 OopJsonSanitizer(const StringCallback& success_callback,
26 const StringCallback& error_callback);
27 ~OopJsonSanitizer() override {}
28
29 // JsonSanitizer implementation:
30 void Start(const std::string& unsafe_json) override;
31
32 private:
33 void OnParseSuccess(scoped_ptr<base::Value> value);
34 void OnParseError(const std::string& error);
35
36 StringCallback success_callback_;
37 StringCallback error_callback_;
38
39 base::WeakPtrFactory<OopJsonSanitizer> weak_ptr_factory_;
40
41 DISALLOW_COPY_AND_ASSIGN(OopJsonSanitizer);
42 };
43
44 OopJsonSanitizer::OopJsonSanitizer(const StringCallback& success_callback,
45 const StringCallback& error_callback)
46 : success_callback_(success_callback),
47 error_callback_(error_callback),
48 weak_ptr_factory_(this) {}
49
50 void OopJsonSanitizer::Start(const std::string& unsafe_json) {
51 SafeJsonParser::Parse(unsafe_json,
52 base::Bind(&OopJsonSanitizer::OnParseSuccess,
53 weak_ptr_factory_.GetWeakPtr()),
54 base::Bind(&OopJsonSanitizer::OnParseError,
55 weak_ptr_factory_.GetWeakPtr()));
56 }
57
58 void OopJsonSanitizer::OnParseSuccess(scoped_ptr<base::Value> value) {
59 // A valid JSON document may only have a dictionary or list as its top-level
60 // type, but the JSON parser also accepts other types, so we filter them out.
61 base::Value::Type type = value->GetType();
62 if (type != base::Value::TYPE_DICTIONARY && type != base::Value::TYPE_LIST) {
63 error_callback_.Run("Invalid top-level type");
64 return;
65 }
66
67 std::string json;
68 if (!base::JSONWriter::Write(*value, &json)) {
69 error_callback_.Run("Encoding error");
70 return;
71 }
72
73 success_callback_.Run(json);
74 }
75
76 void OopJsonSanitizer::OnParseError(const std::string& error) {
77 error_callback_.Run("Parse error: " + error);
78 }
79
80 } // namespace
81
82 // static
83 scoped_ptr<JsonSanitizer> JsonSanitizer::Create(
84 const StringCallback& success_callback,
85 const StringCallback& error_callback) {
86 return make_scoped_ptr(
87 new OopJsonSanitizer(success_callback, error_callback));
88 }
89
90 } // namespace safe_json
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698