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