| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/win/elevated_native_messaging_host.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/json/json_writer.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/values.h" |
| 17 #include "base/win/scoped_handle.h" |
| 18 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
| 19 #include "remoting/host/win/launch_native_messaging_host_process.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 ElevatedNativeMessagingHost::ElevatedNativeMessagingHost( |
| 24 const base::FilePath& binary_path, |
| 25 intptr_t parent_window_handle, |
| 26 bool elevate_process, |
| 27 base::TimeDelta host_timeout, |
| 28 extensions::NativeMessageHost::Client* client) |
| 29 : host_binary_path_(binary_path), |
| 30 parent_window_handle_(parent_window_handle), |
| 31 elevate_host_process_(elevate_process), |
| 32 host_process_timeout_(host_timeout), |
| 33 client_(client) {} |
| 34 |
| 35 ElevatedNativeMessagingHost::~ElevatedNativeMessagingHost() {} |
| 36 |
| 37 void ElevatedNativeMessagingHost::OnMessage( |
| 38 std::unique_ptr<base::Value> message) { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 |
| 41 // Simply pass along the response from the elevated host to the client. |
| 42 std::string message_json; |
| 43 base::JSONWriter::Write(*message, &message_json); |
| 44 client_->PostMessageFromNativeHost(message_json); |
| 45 } |
| 46 |
| 47 void ElevatedNativeMessagingHost::OnDisconnect() { |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 client_->CloseChannel(std::string()); |
| 50 } |
| 51 |
| 52 bool ElevatedNativeMessagingHost::EnsureElevatedHostCreated() { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 |
| 55 if (elevated_channel_) { |
| 56 return true; |
| 57 } |
| 58 |
| 59 base::win::ScopedHandle read_handle; |
| 60 base::win::ScopedHandle write_handle; |
| 61 ProcessLaunchResult result = LaunchNativeMessagingHostProcess( |
| 62 host_binary_path_, parent_window_handle_, elevate_host_process_, |
| 63 &read_handle, &write_handle); |
| 64 if (result != PROCESS_LAUNCH_RESULT_SUCCESS) { |
| 65 return false; |
| 66 } |
| 67 |
| 68 // Set up the native messaging channel to talk to the elevated host. |
| 69 // Note that input for the elevated channel is output for the elevated host. |
| 70 elevated_channel_.reset(new PipeMessagingChannel( |
| 71 base::File(read_handle.Take()), base::File(write_handle.Take()))); |
| 72 elevated_channel_->Start(this); |
| 73 |
| 74 if (!host_process_timeout_.is_zero()) { |
| 75 elevated_host_timer_.Start( |
| 76 FROM_HERE, host_process_timeout_, |
| 77 this, &ElevatedNativeMessagingHost::DisconnectHost); |
| 78 } |
| 79 |
| 80 return true; |
| 81 } |
| 82 |
| 83 void ElevatedNativeMessagingHost::SendMessage( |
| 84 std::unique_ptr<base::Value> message) { |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 elevated_channel_->SendMessage(std::move(message)); |
| 87 } |
| 88 |
| 89 void ElevatedNativeMessagingHost::DisconnectHost() { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 |
| 92 // This will send an EOF to the elevated host, triggering its shutdown. |
| 93 elevated_channel_.reset(); |
| 94 } |
| 95 |
| 96 } // namespace remoting |
| OLD | NEW |