OLD | NEW |
---|---|
(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 #ifndef COMPONENTS_SAFE_JSON_JSON_SANITIZER_H_ | |
6 #define COMPONENTS_SAFE_JSON_JSON_SANITIZER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 | |
14 #if defined(OS_ANDROID) | |
15 #include <jni.h> | |
16 #endif | |
17 | |
18 namespace safe_json { | |
19 | |
20 // Sanitizes and normalizes JSON by parsing it in a safe environment and | |
21 // re-serializing it. Parsing the sanitized JSON should result in a value | |
22 // identical to parsing the original JSON. | |
23 // This allows parsing the sanitized JSON with the regular JSONParser while | |
24 // reducing the risk versus parsing completely untrusted JSON. It also minifies | |
25 // the resulting JSON, which might save some space. | |
26 class JsonSanitizer { | |
Robert Sesek
2015/07/07 21:54:16
Does this class need a Create/Start split, or can
Bernhard Bauer
2015/07/08 11:54:29
As it is right now, Create() returns a scoped_ptr
Robert Sesek
2015/07/08 19:24:07
What is the use for controlling this pointer lifet
Bernhard Bauer
2015/07/08 21:56:47
The use is... it's easier to reason about the life
Robert Sesek
2015/07/08 22:00:43
Unless there were a reason for multiple Sanitize()
Bernhard Bauer
2015/07/09 12:34:49
OK, done.
| |
27 public: | |
28 using StringCallback = base::Callback<void(const std::string&)>; | |
29 | |
30 // Creates a new instance of this class that will call back either the passed | |
31 // |success_callback| or the |error_callback| with the result of the | |
32 // sanitization or an error message, respectively. If this object is destroyed | |
33 // before that, no callback will be called. | |
34 static scoped_ptr<JsonSanitizer> Create( | |
35 const StringCallback& success_callback, | |
36 const StringCallback& error_callback); | |
37 virtual ~JsonSanitizer() {} | |
38 | |
39 // Starts sanitizing the passed in unsafe JSON string. The error or success | |
40 // callback is guaranteed not to be called before the method returns. This | |
41 // method can be called arbitrarily often (even before a callback for an | |
42 // earlier call has been run, but there is no guarantee about the order in | |
43 // which the callbacks are run, and the result or error passed into the | |
44 // callback does not indicate what the input was). | |
45 virtual void Start(const std::string& unsafe_json) = 0; | |
46 | |
47 #if defined(OS_ANDROID) | |
48 static bool Register(JNIEnv* env); | |
49 #endif | |
50 | |
51 protected: | |
52 JsonSanitizer() {} | |
53 | |
54 private: | |
55 DISALLOW_COPY_AND_ASSIGN(JsonSanitizer); | |
56 }; | |
57 | |
58 } // namespace safe_json | |
59 | |
60 #endif // COMPONENTS_SAFE_JSON_JSON_SANITIZER_H_ | |
OLD | NEW |