OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
8 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/json/json_reader.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
11 #include "base/process_util.h" | 13 #include "base/process_util.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 15 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
14 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
15 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
16 #include "chrome/common/extensions/features/feature.h" | 18 #include "chrome/common/extensions/features/feature.h" |
17 #include "extensions/common/constants.h" | 19 #include "extensions/common/constants.h" |
18 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
(...skipping 16 matching lines...) Expand all Loading... |
35 const size_t kReadBufferSize = 4096; | 37 const size_t kReadBufferSize = 4096; |
36 | 38 |
37 const char kFailedToStartError[] = "Failed to start native messaging host."; | 39 const char kFailedToStartError[] = "Failed to start native messaging host."; |
38 const char kInvalidNameError[] = | 40 const char kInvalidNameError[] = |
39 "Invalid native messaging host name specified."; | 41 "Invalid native messaging host name specified."; |
40 const char kNotFoundError[] = "Specified native messaging host not found."; | 42 const char kNotFoundError[] = "Specified native messaging host not found."; |
41 const char kForbiddenError[] = | 43 const char kForbiddenError[] = |
42 "Access to the specified native messaging host is forbidden."; | 44 "Access to the specified native messaging host is forbidden."; |
43 const char kHostInputOuputError[] = | 45 const char kHostInputOuputError[] = |
44 "Error when communicating with the native messaging host."; | 46 "Error when communicating with the native messaging host."; |
| 47 const char kInvalidJsonError[] = |
| 48 "Message must be valid JSON"; |
45 | 49 |
46 } // namespace | 50 } // namespace |
47 | 51 |
48 namespace extensions { | 52 namespace extensions { |
49 | 53 |
50 NativeMessageProcessHost::NativeMessageProcessHost( | 54 NativeMessageProcessHost::NativeMessageProcessHost( |
51 base::WeakPtr<Client> weak_client_ui, | 55 base::WeakPtr<Client> weak_client_ui, |
52 const std::string& source_extension_id, | 56 const std::string& source_extension_id, |
53 const std::string& native_host_name, | 57 const std::string& native_host_name, |
54 int destination_port, | 58 int destination_port, |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 if (message_size > kMaximumMessageSize) { | 265 if (message_size > kMaximumMessageSize) { |
262 LOG(ERROR) << "Native Messaging host tried sending a message that is " | 266 LOG(ERROR) << "Native Messaging host tried sending a message that is " |
263 << message_size << " bytes long."; | 267 << message_size << " bytes long."; |
264 Close(kHostInputOuputError); | 268 Close(kHostInputOuputError); |
265 return; | 269 return; |
266 } | 270 } |
267 | 271 |
268 if (incoming_data_.size() < message_size + kMessageHeaderSize) | 272 if (incoming_data_.size() < message_size + kMessageHeaderSize) |
269 return; | 273 return; |
270 | 274 |
| 275 scoped_ptr<base::ListValue> message(new base::ListValue()); |
| 276 { |
| 277 std::string message_as_json = |
| 278 incoming_data_.substr(kMessageHeaderSize, message_size); |
| 279 int error_code; |
| 280 std::string error_message; |
| 281 scoped_ptr<base::Value> message_as_value( |
| 282 base::JSONReader::ReadAndReturnError(message_as_json, |
| 283 0, // no flags |
| 284 &error_code, |
| 285 &error_message)); |
| 286 if (!message_as_value) { |
| 287 base::JSONReader::JsonParseError parse_error = |
| 288 static_cast<base::JSONReader::JsonParseError>(error_code); |
| 289 LOG(ERROR) << "Native Messaging host sent message with invalid JSON \"" |
| 290 << message_as_json << "\": " << error_message << " (" |
| 291 << base::JSONReader::ErrorCodeToString(parse_error) << "), " |
| 292 << "message size " << message_size << ", " |
| 293 << "incoming data size " << incoming_data_.size() << "."; |
| 294 Close(kInvalidJsonError); |
| 295 return; |
| 296 } |
| 297 message->Append(message_as_value.release()); |
| 298 } |
| 299 |
271 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 300 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
272 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, | 301 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, |
273 destination_port_, | 302 destination_port_, |
274 incoming_data_.substr(kMessageHeaderSize, message_size))); | 303 base::Passed(&message))); |
275 | 304 |
276 incoming_data_.erase(0, kMessageHeaderSize + message_size); | 305 incoming_data_.erase(0, kMessageHeaderSize + message_size); |
277 } | 306 } |
278 } | 307 } |
279 | 308 |
280 void NativeMessageProcessHost::DoWrite() { | 309 void NativeMessageProcessHost::DoWrite() { |
281 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
282 | 311 |
283 while (!write_pending_ && !closed_) { | 312 while (!write_pending_ && !closed_) { |
284 if (!current_write_buffer_.get() || | 313 if (!current_write_buffer_.get() || |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 closed_ = true; | 361 closed_ = true; |
333 read_stream_.reset(); | 362 read_stream_.reset(); |
334 write_stream_.reset(); | 363 write_stream_.reset(); |
335 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 364 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
336 base::Bind(&Client::CloseChannel, weak_client_ui_, | 365 base::Bind(&Client::CloseChannel, weak_client_ui_, |
337 destination_port_, error_message)); | 366 destination_port_, error_message)); |
338 } | 367 } |
339 } | 368 } |
340 | 369 |
341 } // namespace extensions | 370 } // namespace extensions |
OLD | NEW |