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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/process/kill.h" | 14 #include "base/process/kill.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
16 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 17 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
17 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 18 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
(...skipping 22 matching lines...) Expand all Loading... |
41 | 42 |
42 namespace extensions { | 43 namespace extensions { |
43 | 44 |
44 NativeMessageProcessHost::NativeMessageProcessHost( | 45 NativeMessageProcessHost::NativeMessageProcessHost( |
45 const std::string& source_extension_id, | 46 const std::string& source_extension_id, |
46 const std::string& native_host_name, | 47 const std::string& native_host_name, |
47 scoped_ptr<NativeProcessLauncher> launcher) | 48 scoped_ptr<NativeProcessLauncher> launcher) |
48 : client_(NULL), | 49 : client_(NULL), |
49 source_extension_id_(source_extension_id), | 50 source_extension_id_(source_extension_id), |
50 native_host_name_(native_host_name), | 51 native_host_name_(native_host_name), |
51 launcher_(launcher.Pass()), | 52 launcher_(std::move(launcher)), |
52 closed_(false), | 53 closed_(false), |
53 #if defined(OS_POSIX) | 54 #if defined(OS_POSIX) |
54 read_file_(-1), | 55 read_file_(-1), |
55 #endif | 56 #endif |
56 read_pending_(false), | 57 read_pending_(false), |
57 write_pending_(false), | 58 write_pending_(false), |
58 weak_factory_(this) { | 59 weak_factory_(this) { |
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
60 | 61 |
61 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( | 62 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
62 content::BrowserThread::IO); | 63 content::BrowserThread::IO); |
63 } | 64 } |
64 | 65 |
65 NativeMessageProcessHost::~NativeMessageProcessHost() { | 66 NativeMessageProcessHost::~NativeMessageProcessHost() { |
66 DCHECK(task_runner_->BelongsToCurrentThread()); | 67 DCHECK(task_runner_->BelongsToCurrentThread()); |
67 | 68 |
68 if (process_.IsValid()) { | 69 if (process_.IsValid()) { |
69 // Kill the host process if necessary to make sure we don't leave zombies. | 70 // Kill the host process if necessary to make sure we don't leave zombies. |
70 // On OSX base::EnsureProcessTerminated() may block, so we have to post a | 71 // On OSX base::EnsureProcessTerminated() may block, so we have to post a |
71 // task on the blocking pool. | 72 // task on the blocking pool. |
72 #if defined(OS_MACOSX) | 73 #if defined(OS_MACOSX) |
73 content::BrowserThread::PostBlockingPoolTask( | 74 content::BrowserThread::PostBlockingPoolTask( |
74 FROM_HERE, | 75 FROM_HERE, |
75 base::Bind(&base::EnsureProcessTerminated, Passed(&process_))); | 76 base::Bind(&base::EnsureProcessTerminated, Passed(&process_))); |
76 #else | 77 #else |
77 base::EnsureProcessTerminated(process_.Pass()); | 78 base::EnsureProcessTerminated(std::move(process_)); |
78 #endif | 79 #endif |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 // static | 83 // static |
83 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | 84 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
84 gfx::NativeView native_view, | 85 gfx::NativeView native_view, |
85 const std::string& source_extension_id, | 86 const std::string& source_extension_id, |
86 const std::string& native_host_name, | 87 const std::string& native_host_name, |
87 bool allow_user_level, | 88 bool allow_user_level, |
88 std::string* error_message) { | 89 std::string* error_message) { |
89 return NativeMessageProcessHost::CreateWithLauncher( | 90 return NativeMessageProcessHost::CreateWithLauncher( |
90 source_extension_id, | 91 source_extension_id, |
91 native_host_name, | 92 native_host_name, |
92 NativeProcessLauncher::CreateDefault(allow_user_level, native_view)); | 93 NativeProcessLauncher::CreateDefault(allow_user_level, native_view)); |
93 } | 94 } |
94 | 95 |
95 // static | 96 // static |
96 scoped_ptr<NativeMessageHost> NativeMessageProcessHost::CreateWithLauncher( | 97 scoped_ptr<NativeMessageHost> NativeMessageProcessHost::CreateWithLauncher( |
97 const std::string& source_extension_id, | 98 const std::string& source_extension_id, |
98 const std::string& native_host_name, | 99 const std::string& native_host_name, |
99 scoped_ptr<NativeProcessLauncher> launcher) { | 100 scoped_ptr<NativeProcessLauncher> launcher) { |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
101 | 102 |
102 scoped_ptr<NativeMessageHost> process( | 103 scoped_ptr<NativeMessageHost> process(new NativeMessageProcessHost( |
103 new NativeMessageProcessHost(source_extension_id, | 104 source_extension_id, native_host_name, std::move(launcher))); |
104 native_host_name, | |
105 launcher.Pass())); | |
106 | 105 |
107 return process.Pass(); | 106 return process; |
108 } | 107 } |
109 | 108 |
110 void NativeMessageProcessHost::LaunchHostProcess() { | 109 void NativeMessageProcessHost::LaunchHostProcess() { |
111 DCHECK(task_runner_->BelongsToCurrentThread()); | 110 DCHECK(task_runner_->BelongsToCurrentThread()); |
112 | 111 |
113 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); | 112 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); |
114 launcher_->Launch(origin, native_host_name_, | 113 launcher_->Launch(origin, native_host_name_, |
115 base::Bind(&NativeMessageProcessHost::OnHostProcessLaunched, | 114 base::Bind(&NativeMessageProcessHost::OnHostProcessLaunched, |
116 weak_factory_.GetWeakPtr())); | 115 weak_factory_.GetWeakPtr())); |
117 } | 116 } |
(...skipping 15 matching lines...) Expand all Loading... |
133 case NativeProcessLauncher::RESULT_FORBIDDEN: | 132 case NativeProcessLauncher::RESULT_FORBIDDEN: |
134 Close(kForbiddenError); | 133 Close(kForbiddenError); |
135 return; | 134 return; |
136 case NativeProcessLauncher::RESULT_FAILED_TO_START: | 135 case NativeProcessLauncher::RESULT_FAILED_TO_START: |
137 Close(kFailedToStartError); | 136 Close(kFailedToStartError); |
138 return; | 137 return; |
139 case NativeProcessLauncher::RESULT_SUCCESS: | 138 case NativeProcessLauncher::RESULT_SUCCESS: |
140 break; | 139 break; |
141 } | 140 } |
142 | 141 |
143 process_ = process.Pass(); | 142 process_ = std::move(process); |
144 #if defined(OS_POSIX) | 143 #if defined(OS_POSIX) |
145 // This object is not the owner of the file so it should not keep an fd. | 144 // This object is not the owner of the file so it should not keep an fd. |
146 read_file_ = read_file.GetPlatformFile(); | 145 read_file_ = read_file.GetPlatformFile(); |
147 #endif | 146 #endif |
148 | 147 |
149 scoped_refptr<base::TaskRunner> task_runner( | 148 scoped_refptr<base::TaskRunner> task_runner( |
150 content::BrowserThread::GetBlockingPool()-> | 149 content::BrowserThread::GetBlockingPool()-> |
151 GetTaskRunnerWithShutdownBehavior( | 150 GetTaskRunnerWithShutdownBehavior( |
152 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | 151 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
153 | 152 |
154 read_stream_.reset(new net::FileStream(read_file.Pass(), task_runner)); | 153 read_stream_.reset(new net::FileStream(std::move(read_file), task_runner)); |
155 write_stream_.reset(new net::FileStream(write_file.Pass(), task_runner)); | 154 write_stream_.reset(new net::FileStream(std::move(write_file), task_runner)); |
156 | 155 |
157 WaitRead(); | 156 WaitRead(); |
158 DoWrite(); | 157 DoWrite(); |
159 } | 158 } |
160 | 159 |
161 void NativeMessageProcessHost::OnMessage(const std::string& json) { | 160 void NativeMessageProcessHost::OnMessage(const std::string& json) { |
162 DCHECK(task_runner_->BelongsToCurrentThread()); | 161 DCHECK(task_runner_->BelongsToCurrentThread()); |
163 | 162 |
164 if (closed_) | 163 if (closed_) |
165 return; | 164 return; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 359 |
361 if (!closed_) { | 360 if (!closed_) { |
362 closed_ = true; | 361 closed_ = true; |
363 read_stream_.reset(); | 362 read_stream_.reset(); |
364 write_stream_.reset(); | 363 write_stream_.reset(); |
365 client_->CloseChannel(error_message); | 364 client_->CloseChannel(error_message); |
366 } | 365 } |
367 } | 366 } |
368 | 367 |
369 } // namespace extensions | 368 } // namespace extensions |
OLD | NEW |