Chromium Code Reviews| 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_) { | |
|
Sergey Ulanov
2016/08/02 21:18:27
Do we need to support the case when EnsureElevate
joedow
2016/08/08 23:45:16
We need to support the elevated host being created
| |
| 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 if (result != PROCESS_LAUNCH_RESULT_CANCELLED) { | |
| 66 client_->CloseChannel(std::string()); | |
|
Sergey Ulanov
2016/08/02 21:18:27
I'm not sure it's safe to call CloseChannel() here
joedow
2016/08/08 23:45:16
Done. Removed in an upstream change actually.
| |
| 67 } | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 // Set up the native messaging channel to talk to the elevated host. | |
| 72 // Note that input for the elevated channel is output for the elevated host. | |
| 73 elevated_channel_.reset(new PipeMessagingChannel( | |
| 74 base::File(read_handle.Take()), base::File(write_handle.Take()))); | |
| 75 elevated_channel_->Start(this); | |
| 76 | |
| 77 if (!host_process_timeout_.is_zero()) { | |
| 78 elevated_host_timer_.Start( | |
| 79 FROM_HERE, host_process_timeout_, | |
| 80 this, &ElevatedNativeMessagingHost::DisconnectHost); | |
| 81 } | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void ElevatedNativeMessagingHost::SendMessage( | |
| 87 std::unique_ptr<base::Value> message) { | |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 89 elevated_channel_->SendMessage(std::move(message)); | |
| 90 } | |
| 91 | |
| 92 void ElevatedNativeMessagingHost::DisconnectHost() { | |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 94 | |
| 95 // This will send an EOF to the elevated host, triggering its shutdown. | |
| 96 elevated_channel_.reset(); | |
| 97 } | |
| 98 | |
| 99 } // namespace remoting | |
| OLD | NEW |