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