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 #include "components/safe_json/safe_json_parser.h" | 5 #include "components/safe_json/safe_json_parser.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/tuple.h" | 12 #include "base/tuple.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "components/safe_json/safe_json_parser_messages.h" | 14 #include "components/safe_json/safe_json_parser_messages.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/utility_process_host.h" | 16 #include "content/public/browser/utility_process_host.h" |
| 17 #include "content/public/browser/utility_process_host_client.h" |
17 #include "grit/components_strings.h" | 18 #include "grit/components_strings.h" |
18 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 using content::UtilityProcessHost; | 23 using content::UtilityProcessHost; |
23 | 24 |
24 namespace safe_json { | 25 namespace safe_json { |
25 | 26 |
26 SafeJsonParser::SafeJsonParser(const std::string& unsafe_json, | 27 namespace { |
27 const SuccessCallback& success_callback, | 28 |
28 const ErrorCallback& error_callback) | 29 SafeJsonParser::Factory g_factory = nullptr; |
| 30 |
| 31 class SafeJsonParserImpl : public content::UtilityProcessHostClient, |
| 32 public SafeJsonParser { |
| 33 public: |
| 34 SafeJsonParserImpl(const std::string& unsafe_json, |
| 35 const SuccessCallback& success_callback, |
| 36 const ErrorCallback& error_callback); |
| 37 |
| 38 void Start() override; |
| 39 |
| 40 private: |
| 41 ~SafeJsonParserImpl() override; |
| 42 |
| 43 void StartWorkOnIOThread(); |
| 44 |
| 45 void OnJSONParseSucceeded(const base::ListValue& wrapper); |
| 46 void OnJSONParseFailed(const std::string& error_message); |
| 47 |
| 48 void ReportResults(); |
| 49 void ReportResultsOnOriginThread(); |
| 50 |
| 51 // Implementing pieces of the UtilityProcessHostClient interface. |
| 52 bool OnMessageReceived(const IPC::Message& message) override; |
| 53 |
| 54 const std::string unsafe_json_; |
| 55 SuccessCallback success_callback_; |
| 56 ErrorCallback error_callback_; |
| 57 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; |
| 58 |
| 59 scoped_ptr<base::Value> parsed_json_; |
| 60 std::string error_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); |
| 63 }; |
| 64 |
| 65 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, |
| 66 const SuccessCallback& success_callback, |
| 67 const ErrorCallback& error_callback) |
29 : unsafe_json_(unsafe_json), | 68 : unsafe_json_(unsafe_json), |
30 success_callback_(success_callback), | 69 success_callback_(success_callback), |
31 error_callback_(error_callback) { | 70 error_callback_(error_callback) {} |
32 } | |
33 | 71 |
34 void SafeJsonParser::Start() { | 72 void SafeJsonParserImpl::Start() { |
| 73 // Current callers of this class are known to either come from |
| 74 // the UI or IO threads. This DCHECK can be removed/further enhanced |
| 75 // in case this class is useable outside of these contexts. |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 77 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 78 |
35 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 79 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
36 | 80 |
37 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
38 BrowserThread::IO, FROM_HERE, | 82 BrowserThread::IO, FROM_HERE, |
39 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this)); | 83 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); |
40 } | 84 } |
41 | 85 |
42 SafeJsonParser::~SafeJsonParser() { | 86 SafeJsonParserImpl::~SafeJsonParserImpl() { |
43 } | 87 } |
44 | 88 |
45 void SafeJsonParser::StartWorkOnIOThread() { | 89 void SafeJsonParserImpl::StartWorkOnIOThread() { |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
47 UtilityProcessHost* host = UtilityProcessHost::Create( | 91 UtilityProcessHost* host = UtilityProcessHost::Create( |
48 this, base::ThreadTaskRunnerHandle::Get().get()); | 92 this, base::ThreadTaskRunnerHandle::Get().get()); |
49 host->SetName( | 93 host->SetName( |
50 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | 94 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); |
51 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); | 95 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); |
52 } | 96 } |
53 | 97 |
54 void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) { | 98 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) { |
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
56 const base::Value* value = NULL; | 100 const base::Value* value = NULL; |
57 CHECK(wrapper.Get(0, &value)); | 101 CHECK(wrapper.Get(0, &value)); |
58 | 102 |
59 parsed_json_.reset(value->DeepCopy()); | 103 parsed_json_.reset(value->DeepCopy()); |
60 ReportResults(); | 104 ReportResults(); |
61 } | 105 } |
62 | 106 |
63 void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) { | 107 void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) { |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
65 error_ = error_message; | 109 error_ = error_message; |
66 ReportResults(); | 110 ReportResults(); |
67 } | 111 } |
68 | 112 |
69 void SafeJsonParser::ReportResults() { | 113 void SafeJsonParserImpl::ReportResults() { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
71 | 115 |
72 caller_task_runner_->PostTask( | 116 caller_task_runner_->PostTask( |
73 FROM_HERE, | 117 FROM_HERE, |
74 base::Bind(&SafeJsonParser::ReportResultsOnOriginThread, this)); | 118 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); |
75 } | 119 } |
76 | 120 |
77 void SafeJsonParser::ReportResultsOnOriginThread() { | 121 void SafeJsonParserImpl::ReportResultsOnOriginThread() { |
78 // Current callers of this class are known to either come from | 122 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
79 // the UI or IO threads. This DCHECK can be removed/further enhanced | |
80 // in case this class is useable outside of these contexts. | |
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
82 BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
83 if (error_.empty() && parsed_json_) { | 123 if (error_.empty() && parsed_json_) { |
84 if (!success_callback_.is_null()) | 124 if (!success_callback_.is_null()) |
85 success_callback_.Run(parsed_json_.Pass()); | 125 success_callback_.Run(parsed_json_.Pass()); |
86 } else { | 126 } else { |
87 if (!error_callback_.is_null()) | 127 if (!error_callback_.is_null()) |
88 error_callback_.Run(error_); | 128 error_callback_.Run(error_); |
89 } | 129 } |
90 } | 130 } |
91 | 131 |
92 bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) { | 132 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { |
93 bool handled = true; | 133 bool handled = true; |
94 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message) | 134 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message) |
95 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, | 135 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, |
96 OnJSONParseSucceeded) | 136 OnJSONParseSucceeded) |
97 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, | 137 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, |
98 OnJSONParseFailed) | 138 OnJSONParseFailed) |
99 IPC_MESSAGE_UNHANDLED(handled = false) | 139 IPC_MESSAGE_UNHANDLED(handled = false) |
100 IPC_END_MESSAGE_MAP() | 140 IPC_END_MESSAGE_MAP() |
101 return handled; | 141 return handled; |
102 } | 142 } |
103 | 143 |
| 144 } // namespace |
| 145 |
| 146 // static |
| 147 void SafeJsonParser::SetFactoryForTesting(Factory factory) { |
| 148 g_factory = factory; |
| 149 } |
| 150 |
| 151 // static |
| 152 SafeJsonParser* SafeJsonParser::Create( |
| 153 const std::string& unsafe_json, |
| 154 const SuccessCallback& success_callback, |
| 155 const ErrorCallback& error_callback) { |
| 156 if (g_factory) |
| 157 return g_factory(unsafe_json, success_callback, error_callback); |
| 158 |
| 159 return new SafeJsonParserImpl(unsafe_json, success_callback, error_callback); |
| 160 } |
| 161 |
104 } // namespace safe_json | 162 } // namespace safe_json |
OLD | NEW |