summaryrefslogtreecommitdiff
path: root/chromium/services/data_decoder/public/cpp/safe_json_parser_impl.h
blob: eb04331734dc3d3ffe849434eb850249561ac289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2013 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.

#ifndef SERVICES_DATA_DECODER_PUBLIC_CPP_SAFE_JSON_PARSER_IMPL_H_
#define SERVICES_DATA_DECODER_PUBLIC_CPP_SAFE_JSON_PARSER_IMPL_H_

#include <memory>
#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "base/threading/thread_checker.h"
#include "base/token.h"
#include "base/values.h"
#include "services/data_decoder/public/cpp/safe_json_parser.h"
#include "services/data_decoder/public/mojom/json_parser.mojom.h"

namespace service_manager {
class Connector;
}

namespace data_decoder {

class SafeJsonParserImpl : public SafeJsonParser {
 public:
  SafeJsonParserImpl(service_manager::Connector* connector,
                     const std::string& unsafe_json,
                     SuccessCallback success_callback,
                     ErrorCallback error_callback,
                     const base::Optional<base::Token>& batch_id);

 private:
  ~SafeJsonParserImpl() override;

  // SafeJsonParser implementation.
  void Start() override;

  void StartOnIOThread();
  void OnConnectionError();

  // mojom::SafeJsonParser::Parse callback.
  void OnParseDone(base::Optional<base::Value> result,
                   const base::Optional<std::string>& error);

  // Reports the result on the calling task runner via the |success_callback_|
  // or the |error_callback_|.
  void ReportResults(base::Optional<base::Value> parsed_json,
                     const std::string& error);

  const std::string unsafe_json_;
  SuccessCallback success_callback_;
  ErrorCallback error_callback_;

  mojom::JsonParserPtr json_parser_ptr_;

  SEQUENCE_CHECKER(sequence_checker_);

  DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl);
};

}  // namespace data_decoder

#endif  // SERVICES_DATA_DECODER_PUBLIC_CPP_SAFE_JSON_PARSER_IMPL_H_