blob: 625e52f96b8b2418c76aa5d2558d0a322050a21c [file] [log] [blame]
bauerb64c5a3882015-07-06 20:08:541// 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.
4
5#ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_
6#define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_
7
dcheng82beb4f2016-04-26 00:35:028#include <memory>
bauerb64c5a3882015-07-06 20:08:549#include <string>
10
pmonettee6f1ea12016-06-17 22:14:2111#include "base/callback.h"
avif57136c12015-12-25 23:27:4512#include "base/macros.h"
bauerb64c5a3882015-07-06 20:08:5413#include "base/memory/ref_counted.h"
amistryab6f6ae2016-05-11 06:44:1814#include "base/threading/thread_checker.h"
15#include "components/safe_json/public/interfaces/safe_json.mojom.h"
bauerb64c5a3882015-07-06 20:08:5416#include "components/safe_json/safe_json_parser.h"
pmonettee6f1ea12016-06-17 22:14:2117#include "content/public/browser/utility_process_mojo_client.h"
bauerb64c5a3882015-07-06 20:08:5418
19namespace base {
20class ListValue;
21class SequencedTaskRunner;
22class Value;
23}
24
bauerb64c5a3882015-07-06 20:08:5425namespace safe_json {
26
pmonettee6f1ea12016-06-17 22:14:2127class SafeJsonParserImpl : public SafeJsonParser {
bauerb64c5a3882015-07-06 20:08:5428 public:
29 SafeJsonParserImpl(const std::string& unsafe_json,
30 const SuccessCallback& success_callback,
31 const ErrorCallback& error_callback);
32
33 private:
34 ~SafeJsonParserImpl() override;
35
bauerb64c5a3882015-07-06 20:08:5436 // SafeJsonParser implementation.
37 void Start() override;
38
pmonettee6f1ea12016-06-17 22:14:2139 void StartOnIOThread();
40 void OnConnectionError();
41
amistryab6f6ae2016-05-11 06:44:1842 // mojom::SafeJsonParser::Parse callback.
rockotc383dde82016-06-18 20:15:5143 void OnParseDone(const base::ListValue& wrapper, mojo::String error);
pmonettee6f1ea12016-06-17 22:14:2144
45 // Reports the result on the calling task runner via the |success_callback_|
46 // or the |error_callback_|.
47 void ReportResults(std::unique_ptr<base::Value> parsed_json,
48 const std::string& error);
amistryab6f6ae2016-05-11 06:44:1849
bauerb64c5a3882015-07-06 20:08:5450 const std::string unsafe_json_;
51 SuccessCallback success_callback_;
52 ErrorCallback error_callback_;
53 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_;
54
pmonettee6f1ea12016-06-17 22:14:2155 std::unique_ptr<content::UtilityProcessMojoClient<mojom::SafeJsonParser>>
56 mojo_json_parser_;
bauerb64c5a3882015-07-06 20:08:5457
pmonettee6f1ea12016-06-17 22:14:2158 // Used instead of DCHECK_CURRENTLY_ON(BrowserThread::IO) because it's
59 // posssible that it fails when the IO thread message loop is shutting down.
60 // This happens after the IO thread has unregistered from the BrowserThread
61 // list.
amistryab6f6ae2016-05-11 06:44:1862 base::ThreadChecker io_thread_checker_;
63
bauerb64c5a3882015-07-06 20:08:5464 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl);
65};
66
67} // namespace safe_json
68
69#endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_