| 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 scoped_ptr<base::Value> message_as_value( |
| 280 base::JSONReader::Read(message_as_json)); |
| 281 if (!message_as_value) { |
| 282 LOG(ERROR) << "Native Messaging host sent a message with invalid JSON"; |
| 283 Close(kInvalidJsonError); |
| 284 return; |
| 285 } |
| 286 message->Append(message_as_value.release()); |
| 287 } |
| 288 |
| 271 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 289 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 272 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, | 290 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, |
| 273 destination_port_, | 291 destination_port_, |
| 274 incoming_data_.substr(kMessageHeaderSize, message_size))); | 292 base::Passed(&message))); |
| 275 | 293 |
| 276 incoming_data_.erase(0, kMessageHeaderSize + message_size); | 294 incoming_data_.erase(0, kMessageHeaderSize + message_size); |
| 277 } | 295 } |
| 278 } | 296 } |
| 279 | 297 |
| 280 void NativeMessageProcessHost::DoWrite() { | 298 void NativeMessageProcessHost::DoWrite() { |
| 281 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 282 | 300 |
| 283 while (!write_pending_ && !closed_) { | 301 while (!write_pending_ && !closed_) { |
| 284 if (!current_write_buffer_ || !current_write_buffer_->BytesRemaining()) { | 302 if (!current_write_buffer_ || !current_write_buffer_->BytesRemaining()) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 closed_ = true; | 348 closed_ = true; |
| 331 read_stream_.reset(); | 349 read_stream_.reset(); |
| 332 write_stream_.reset(); | 350 write_stream_.reset(); |
| 333 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 351 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 334 base::Bind(&Client::CloseChannel, weak_client_ui_, | 352 base::Bind(&Client::CloseChannel, weak_client_ui_, |
| 335 destination_port_, error_message)); | 353 destination_port_, error_message)); |
| 336 } | 354 } |
| 337 } | 355 } |
| 338 | 356 |
| 339 } // namespace extensions | 357 } // namespace extensions |
| OLD | NEW |