| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 14 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
| 17 #include "chrome/common/extensions/features/feature.h" | 18 #include "chrome/common/extensions/features/feature.h" |
| 18 #include "net/base/file_stream.h" | 19 #include "net/base/file_stream.h" |
| 19 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const int kExitTimeoutMS = 5000; | 26 const int kExitTimeoutMS = 5000; |
| 26 const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; | 27 const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; |
| 27 | 28 |
| 28 // Message header contains 4-byte integer size of the message. | 29 // Message header contains 4-byte integer size of the message. |
| 29 const size_t kMessageHeaderSize = 4; | 30 const size_t kMessageHeaderSize = 4; |
| 30 | 31 |
| 31 // Size of the buffer to be allocated for each read. | 32 // Size of the buffer to be allocated for each read. |
| 32 const size_t kReadBufferSize = 4096; | 33 const size_t kReadBufferSize = 4096; |
| 33 | 34 |
| 34 } // namespace | 35 } // namespace |
| 35 | 36 |
| 36 namespace extensions { | 37 namespace extensions { |
| 37 | 38 |
| 38 NativeMessageProcessHost::NativeMessageProcessHost( | 39 NativeMessageProcessHost::NativeMessageProcessHost( |
| 39 base::WeakPtr<Client> weak_client_ui, | 40 base::WeakPtr<Client> weak_client_ui, |
| 41 const std::string& source_extension_id, |
| 40 const std::string& native_host_name, | 42 const std::string& native_host_name, |
| 41 int destination_port, | 43 int destination_port, |
| 42 scoped_ptr<NativeProcessLauncher> launcher) | 44 scoped_ptr<NativeProcessLauncher> launcher) |
| 43 : weak_client_ui_(weak_client_ui), | 45 : weak_client_ui_(weak_client_ui), |
| 46 source_extension_id_(source_extension_id), |
| 44 native_host_name_(native_host_name), | 47 native_host_name_(native_host_name), |
| 45 destination_port_(destination_port), | 48 destination_port_(destination_port), |
| 46 launcher_(launcher.Pass()), | 49 launcher_(launcher.Pass()), |
| 47 closed_(false), | 50 closed_(false), |
| 48 native_process_handle_(base::kNullProcessHandle), | 51 native_process_handle_(base::kNullProcessHandle), |
| 49 read_pending_(false), | 52 read_pending_(false), |
| 50 read_eof_(false), | 53 read_eof_(false), |
| 51 write_pending_(false) { | 54 write_pending_(false) { |
| 52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 53 | 56 |
| 54 // It's safe to use base::Unretained() here because NativeMessagePort always | 57 // It's safe to use base::Unretained() here because NativeMessagePort always |
| 55 // deletes us on the IO thread. | 58 // deletes us on the IO thread. |
| 56 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 59 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 57 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | 60 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
| 58 base::Unretained(this))); | 61 base::Unretained(this))); |
| 59 } | 62 } |
| 60 | 63 |
| 61 NativeMessageProcessHost::~NativeMessageProcessHost() { | 64 NativeMessageProcessHost::~NativeMessageProcessHost() { |
| 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 65 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 63 Close(); | 66 Close(); |
| 64 } | 67 } |
| 65 | 68 |
| 66 // static | 69 // static |
| 67 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( | 70 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( |
| 68 base::WeakPtr<Client> weak_client_ui, | 71 base::WeakPtr<Client> weak_client_ui, |
| 72 const std::string& source_extension_id, |
| 69 const std::string& native_host_name, | 73 const std::string& native_host_name, |
| 70 int destination_port) { | 74 int destination_port) { |
| 71 return CreateWithLauncher(weak_client_ui, native_host_name, destination_port, | 75 return CreateWithLauncher(weak_client_ui, source_extension_id, |
| 76 native_host_name, destination_port, |
| 72 NativeProcessLauncher::CreateDefault()); | 77 NativeProcessLauncher::CreateDefault()); |
| 73 } | 78 } |
| 74 | 79 |
| 75 // static | 80 // static |
| 76 scoped_ptr<NativeMessageProcessHost> | 81 scoped_ptr<NativeMessageProcessHost> |
| 77 NativeMessageProcessHost::CreateWithLauncher( | 82 NativeMessageProcessHost::CreateWithLauncher( |
| 78 base::WeakPtr<Client> weak_client_ui, | 83 base::WeakPtr<Client> weak_client_ui, |
| 84 const std::string& source_extension_id, |
| 79 const std::string& native_host_name, | 85 const std::string& native_host_name, |
| 80 int destination_port, | 86 int destination_port, |
| 81 scoped_ptr<NativeProcessLauncher> launcher) { | 87 scoped_ptr<NativeProcessLauncher> launcher) { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 83 | 89 |
| 84 scoped_ptr<NativeMessageProcessHost> process; | 90 scoped_ptr<NativeMessageProcessHost> process; |
| 85 if (Feature::GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV || | 91 if (Feature::GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV || |
| 86 !CommandLine::ForCurrentProcess()->HasSwitch( | 92 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 87 switches::kEnableNativeMessaging)) { | 93 switches::kEnableNativeMessaging)) { |
| 88 return process.Pass(); | 94 return process.Pass(); |
| 89 } | 95 } |
| 90 | 96 |
| 91 process.reset(new NativeMessageProcessHost( | 97 process.reset(new NativeMessageProcessHost( |
| 92 weak_client_ui, native_host_name, destination_port, launcher.Pass())); | 98 weak_client_ui, source_extension_id, native_host_name, |
| 99 destination_port, launcher.Pass())); |
| 93 | 100 |
| 94 return process.Pass(); | 101 return process.Pass(); |
| 95 } | 102 } |
| 96 | 103 |
| 97 void NativeMessageProcessHost::LaunchHostProcess() { | 104 void NativeMessageProcessHost::LaunchHostProcess() { |
| 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 99 | 106 |
| 100 launcher_->Launch( | 107 launcher_->Launch( |
| 101 native_host_name_, base::Bind( | 108 source_extension_id_, native_host_name_, base::Bind( |
| 102 &NativeMessageProcessHost::OnHostProcessLaunched, | 109 &NativeMessageProcessHost::OnHostProcessLaunched, |
| 103 base::Unretained(this))); | 110 base::Unretained(this))); |
| 104 } | 111 } |
| 105 | 112 |
| 106 void NativeMessageProcessHost::OnHostProcessLaunched( | 113 void NativeMessageProcessHost::OnHostProcessLaunched( |
| 107 base::ProcessHandle native_process_handle, | 114 base::ProcessHandle native_process_handle, |
| 108 base::PlatformFile read_file, | 115 base::PlatformFile read_file, |
| 109 base::PlatformFile write_file) { | 116 base::PlatformFile write_file) { |
| 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 117 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 111 | 118 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 content::BrowserThread::IO, FROM_HERE, | 325 content::BrowserThread::IO, FROM_HERE, |
| 319 base::Bind(base::IgnoreResult(&base::KillProcess), | 326 base::Bind(base::IgnoreResult(&base::KillProcess), |
| 320 native_process_handle_, 0, | 327 native_process_handle_, 0, |
| 321 false /* don't wait for exit */), | 328 false /* don't wait for exit */), |
| 322 base::TimeDelta::FromMilliseconds(kExitTimeoutMS)); | 329 base::TimeDelta::FromMilliseconds(kExitTimeoutMS)); |
| 323 native_process_handle_ = base::kNullProcessHandle; | 330 native_process_handle_ = base::kNullProcessHandle; |
| 324 } | 331 } |
| 325 } | 332 } |
| 326 | 333 |
| 327 } // namespace extensions | 334 } // namespace extensions |
| OLD | NEW |