bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
| 6 | #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
| 7 | |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 8 | #include <memory> |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 9 | #include <string> |
| 10 | |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 11 | #include "base/callback.h" |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 12 | #include "base/macros.h" |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
amistry | ab6f6ae | 2016-05-11 06:44:18 | [diff] [blame] | 14 | #include "base/threading/thread_checker.h" |
| 15 | #include "components/safe_json/public/interfaces/safe_json.mojom.h" |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 16 | #include "components/safe_json/safe_json_parser.h" |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 17 | #include "content/public/browser/utility_process_mojo_client.h" |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | class ListValue; |
| 21 | class SequencedTaskRunner; |
| 22 | class Value; |
| 23 | } |
| 24 | |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 25 | namespace safe_json { |
| 26 | |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 27 | class SafeJsonParserImpl : public SafeJsonParser { |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 28 | 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 | |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 36 | // SafeJsonParser implementation. |
| 37 | void Start() override; |
| 38 | |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 39 | void StartOnIOThread(); |
| 40 | void OnConnectionError(); |
| 41 | |
amistry | ab6f6ae | 2016-05-11 06:44:18 | [diff] [blame] | 42 | // mojom::SafeJsonParser::Parse callback. |
rockot | c383dde8 | 2016-06-18 20:15:51 | [diff] [blame] | 43 | void OnParseDone(const base::ListValue& wrapper, mojo::String error); |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 44 | |
| 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); |
amistry | ab6f6ae | 2016-05-11 06:44:18 | [diff] [blame] | 49 | |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 50 | const std::string unsafe_json_; |
| 51 | SuccessCallback success_callback_; |
| 52 | ErrorCallback error_callback_; |
| 53 | scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; |
| 54 | |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 55 | std::unique_ptr<content::UtilityProcessMojoClient<mojom::SafeJsonParser>> |
| 56 | mojo_json_parser_; |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 57 | |
pmonette | e6f1ea1 | 2016-06-17 22:14:21 | [diff] [blame] | 58 | // 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. |
amistry | ab6f6ae | 2016-05-11 06:44:18 | [diff] [blame] | 62 | base::ThreadChecker io_thread_checker_; |
| 63 | |
bauerb | 64c5a388 | 2015-07-06 20:08:54 | [diff] [blame] | 64 | DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); |
| 65 | }; |
| 66 | |
| 67 | } // namespace safe_json |
| 68 | |
| 69 | #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |