Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 5 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
| 6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/threading/thread_checker.h" | |
|
Anand Mistry (off Chromium)
2016/06/10 11:13:54
thread_checker.h is still used.
Patrick Monette
2016/06/10 18:13:23
Done.
| |
| 15 #include "components/safe_json/public/interfaces/safe_json.mojom.h" | 14 #include "components/safe_json/public/interfaces/safe_json.mojom.h" |
| 16 #include "components/safe_json/safe_json_parser.h" | 15 #include "components/safe_json/safe_json_parser.h" |
| 17 #include "content/public/browser/utility_process_host_client.h" | 16 #include "content/public/browser/utility_process_mojo_client.h" |
| 18 | 17 |
| 19 namespace base { | 18 namespace base { |
| 20 class ListValue; | 19 class ListValue; |
| 21 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 22 class Value; | 21 class Value; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace content { | |
| 26 class UtilityProcessHost; | |
| 27 } | |
| 28 | |
| 29 namespace IPC { | |
| 30 class Message; | |
| 31 } | |
| 32 | |
| 33 namespace safe_json { | 24 namespace safe_json { |
| 34 | 25 |
| 35 class SafeJsonParserImpl : public content::UtilityProcessHostClient, | 26 class SafeJsonParserImpl |
| 36 public SafeJsonParser { | 27 : public base::RefCountedThreadSafe<SafeJsonParserImpl>, |
|
Bernhard Bauer
2016/06/09 15:16:50
Ooh, if this class now doesn't inherit from a refc
Patrick Monette
2016/06/10 18:13:23
Done.
| |
| 28 public SafeJsonParser { | |
| 37 public: | 29 public: |
| 38 SafeJsonParserImpl(const std::string& unsafe_json, | 30 SafeJsonParserImpl(const std::string& unsafe_json, |
| 39 const SuccessCallback& success_callback, | 31 const SuccessCallback& success_callback, |
| 40 const ErrorCallback& error_callback); | 32 const ErrorCallback& error_callback); |
| 41 | 33 |
| 42 private: | 34 private: |
| 35 friend class base::RefCountedThreadSafe<SafeJsonParserImpl>; | |
| 36 | |
| 43 ~SafeJsonParserImpl() override; | 37 ~SafeJsonParserImpl() override; |
| 44 | 38 |
| 45 void StartWorkOnIOThread(); | |
| 46 | |
| 47 void ReportResults(); | |
| 48 void ReportResultsOnOriginThread(); | |
| 49 | |
| 50 // Implementing pieces of the UtilityProcessHostClient interface. | |
| 51 bool OnMessageReceived(const IPC::Message& message) override; | |
| 52 | |
| 53 // SafeJsonParser implementation. | 39 // SafeJsonParser implementation. |
| 54 void Start() override; | 40 void Start() override; |
| 55 | 41 |
| 42 void StartOnIoThread(); | |
|
grt (UTC plus 2)
2016/06/09 14:48:58
nit: OnIOThread is far more prevalent in the codeb
Patrick Monette
2016/06/10 18:13:23
Done.
| |
| 43 | |
|
grt (UTC plus 2)
2016/06/09 14:48:58
nit: either remove ths blank lines separating thes
Patrick Monette
2016/06/10 18:13:23
Done.
| |
| 44 void OnConnectionError(); | |
| 45 | |
| 56 // mojom::SafeJsonParser::Parse callback. | 46 // mojom::SafeJsonParser::Parse callback. |
| 57 void OnParseDone(const base::ListValue& wrapper, mojo::String error); | 47 void OnParseDone(const base::ListValue& wrapper, const mojo::String& error); |
| 48 | |
| 49 // Reports the result on the calling task runner via the |success_callback_| | |
| 50 // or the |error_callback_|. | |
| 51 void ReportResults(std::unique_ptr<base::Value> parsed_json, | |
| 52 const std::string& error); | |
| 58 | 53 |
| 59 const std::string unsafe_json_; | 54 const std::string unsafe_json_; |
| 60 SuccessCallback success_callback_; | 55 SuccessCallback success_callback_; |
| 61 ErrorCallback error_callback_; | 56 ErrorCallback error_callback_; |
| 62 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; | 57 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; |
| 63 | 58 |
| 64 std::unique_ptr<base::Value> parsed_json_; | 59 std::unique_ptr<content::UtilityProcessMojoClient<mojom::SafeJsonParser>> |
| 65 std::string error_; | 60 mojo_json_parser_; |
| 66 | 61 |
| 67 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 62 // Used instead of DCHECK_CURRENTLY_ON(BrowserThread::UI) because it's |
|
Bernhard Bauer
2016/06/09 15:16:50
BrowserThread::IO?
Patrick Monette
2016/06/10 18:13:23
Right!Fixed.
| |
| 68 | 63 // posssible that it fails when the IO thread message loop is shutting down. |
| 69 mojom::SafeJsonParserPtr service_; | 64 // This happens after the IO thread has unregistered from the BrowserThread |
| 70 | 65 // list. |
| 71 // To ensure the UtilityProcessHost and Mojo service are only accessed on the | |
| 72 // IO thread. | |
| 73 base::ThreadChecker io_thread_checker_; | 66 base::ThreadChecker io_thread_checker_; |
| 74 | 67 |
| 75 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); | 68 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); |
| 76 }; | 69 }; |
| 77 | 70 |
| 78 } // namespace safe_json | 71 } // namespace safe_json |
| 79 | 72 |
| 80 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 73 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
| OLD | NEW |