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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_
6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
20 // SafeJsonParser parses a given JSON safely via a platform-dependent mechanism
21 // (like parsing it in a utility process or in a memory-safe environment).
22 // Internally, an instance of this class is created when Parse() is called and
23 // is kept alive until one of the two callbacks is called, after which it
25 class SafeJsonParser
{
27 using SuccessCallback
= base::Callback
<void(scoped_ptr
<base::Value
>)>;
28 using ErrorCallback
= base::Callback
<void(const std::string
&)>;
30 using Factory
= SafeJsonParser
* (*)(const std::string
& unsafe_json
,
31 const SuccessCallback
& success_callback
,
32 const ErrorCallback
& error_callback
);
34 // Starts parsing the passed in |unsafe_json| and calls either
35 // |success_callback| or |error_callback| when finished.
36 static void Parse(const std::string
& unsafe_json
,
37 const SuccessCallback
& success_callback
,
38 const ErrorCallback
& error_callback
);
40 static void SetFactoryForTesting(Factory factory
);
43 virtual ~SafeJsonParser() {}
46 virtual void Start() = 0;
49 } // namespace safe_json
51 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_