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/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 const size_t kReadBufferSize = 4096; | 35 const size_t kReadBufferSize = 4096; |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 namespace extensions { | 39 namespace extensions { |
40 | 40 |
41 NativeMessageProcessHost::NativeMessageProcessHost( | 41 NativeMessageProcessHost::NativeMessageProcessHost( |
42 const std::string& source_extension_id, | 42 const std::string& source_extension_id, |
43 const std::string& native_host_name, | 43 const std::string& native_host_name, |
44 scoped_ptr<NativeProcessLauncher> launcher) | 44 scoped_ptr<NativeProcessLauncher> launcher) |
45 : source_extension_id_(source_extension_id), | 45 : client_(NULL), |
| 46 source_extension_id_(source_extension_id), |
46 native_host_name_(native_host_name), | 47 native_host_name_(native_host_name), |
47 launcher_(launcher.Pass()), | 48 launcher_(launcher.Pass()), |
48 closed_(false), | 49 closed_(false), |
49 process_handle_(base::kNullProcessHandle), | 50 process_handle_(base::kNullProcessHandle), |
50 #if defined(OS_POSIX) | 51 #if defined(OS_POSIX) |
51 read_file_(-1), | 52 read_file_(-1), |
52 #endif | 53 #endif |
53 read_pending_(false), | 54 read_pending_(false), |
54 write_pending_(false) { | 55 write_pending_(false) { |
55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
56 | 57 |
57 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( | 58 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
58 content::BrowserThread::IO); | 59 content::BrowserThread::IO); |
59 // It's safe to use base::Unretained() here because NativeMessagePort always | |
60 // deletes us on the IO thread. | |
61 task_runner_->PostTask( | |
62 FROM_HERE, | |
63 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | |
64 base::Unretained(this))); | |
65 } | 60 } |
66 | 61 |
67 NativeMessageProcessHost::~NativeMessageProcessHost() { | 62 NativeMessageProcessHost::~NativeMessageProcessHost() { |
68 DCHECK(task_runner_->BelongsToCurrentThread()); | 63 DCHECK(task_runner_->BelongsToCurrentThread()); |
69 } | 64 } |
70 | 65 |
71 // static | 66 // static |
72 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | 67 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
73 gfx::NativeView native_view, | 68 gfx::NativeView native_view, |
74 const std::string& source_extension_id, | 69 const std::string& source_extension_id, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 162 |
168 // Send() may be called before the host process is started. In that case the | 163 // Send() may be called before the host process is started. In that case the |
169 // message will be written when OnHostProcessLaunched() is called. If it's | 164 // message will be written when OnHostProcessLaunched() is called. If it's |
170 // already started then write the message now. | 165 // already started then write the message now. |
171 if (write_stream_) | 166 if (write_stream_) |
172 DoWrite(); | 167 DoWrite(); |
173 } | 168 } |
174 | 169 |
175 void NativeMessageProcessHost::Start(Client* client) { | 170 void NativeMessageProcessHost::Start(Client* client) { |
176 DCHECK(task_runner_->BelongsToCurrentThread()); | 171 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 172 DCHECK(!client_); |
177 client_ = client; | 173 client_ = client; |
| 174 // It's safe to use base::Unretained() here because NativeMessagePort always |
| 175 // deletes us on the IO thread. |
| 176 task_runner_->PostTask( |
| 177 FROM_HERE, |
| 178 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
| 179 base::Unretained(this))); |
178 } | 180 } |
179 | 181 |
180 scoped_refptr<base::SingleThreadTaskRunner> | 182 scoped_refptr<base::SingleThreadTaskRunner> |
181 NativeMessageProcessHost::task_runner() const { | 183 NativeMessageProcessHost::task_runner() const { |
182 return task_runner_; | 184 return task_runner_; |
183 } | 185 } |
184 | 186 |
185 #if defined(OS_POSIX) | 187 #if defined(OS_POSIX) |
186 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { | 188 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { |
187 DCHECK_EQ(fd, read_file_); | 189 DCHECK_EQ(fd, read_file_); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 content::BrowserThread::PostBlockingPoolTask( | 356 content::BrowserThread::PostBlockingPoolTask( |
355 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); | 357 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); |
356 #else | 358 #else |
357 base::EnsureProcessTerminated(process_handle_); | 359 base::EnsureProcessTerminated(process_handle_); |
358 #endif | 360 #endif |
359 process_handle_ = base::kNullProcessHandle; | 361 process_handle_ = base::kNullProcessHandle; |
360 } | 362 } |
361 } | 363 } |
362 | 364 |
363 } // namespace extensions | 365 } // namespace extensions |
OLD | NEW |