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 #ifndef REMOTING_HOST_WIN_ELEVATED_NATIVE_MESSAGING_HOST_H_
6 #define REMOTING_HOST_WIN_ELEVATED_NATIVE_MESSAGING_HOST_H_
7 
8 #include <cstdint>
9 #include <memory>
10 
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "extensions/browser/api/messaging/native_message_host.h"
17 #include "extensions/browser/api/messaging/native_messaging_channel.h"
18 #include "remoting/host/win/launch_native_messaging_host_process.h"
19 
20 namespace base {
21 class Value;
22 }  // namespace base
23 
24 namespace remoting {
25 
26 // Helper class which manages the creation and lifetime of an elevated native
27 // messaging host process.
28 class ElevatedNativeMessagingHost
29     : public extensions::NativeMessagingChannel::EventHandler {
30  public:
31   ElevatedNativeMessagingHost(const base::FilePath& binary_path,
32                               intptr_t parent_window_handle,
33                               bool elevate_process,
34                               base::TimeDelta host_timeout,
35                               extensions::NativeMessageHost::Client* client);
36   ~ElevatedNativeMessagingHost() override;
37 
38   // extensions::NativeMessagingChannel::EventHandle implementation.
39   void OnMessage(std::unique_ptr<base::Value> message) override;
40   void OnDisconnect() override;
41 
42   // Create and connect to an elevated host process if necessary.
43   // |elevated_channel_| will contain the native messaging channel to the
44   // elevated host if the function succeeds.
45   ProcessLaunchResult EnsureElevatedHostCreated();
46 
47   // Send |message| to the elevated host.
48   void SendMessage(std::unique_ptr<base::Value> message);
49 
50  private:
51   // Disconnect and shut down the elevated host.
52   void DisconnectHost();
53 
54   // Path to the binary to use for the elevated host process.
55   base::FilePath host_binary_path_;
56 
57   // Handle of the parent window.
58   intptr_t parent_window_handle_;
59 
60   // Indicates whether the launched process should be elevated when lauinched.
61   // Note: Binaries with uiaccess run at a higher UIPI level than the launching
62   // process so they still need to be launched and controlled by this class but
63   // do not require traditional elevation to function.
64   bool elevate_host_process_;
65 
66   // Specifies the amount of time to allow the elevated host to run.
67   base::TimeDelta host_process_timeout_;
68 
69   // EventHandler of the parent process.
70   extensions::NativeMessageHost::Client* client_;
71 
72   // Native messaging channel used to communicate with the elevated host.
73   std::unique_ptr<extensions::NativeMessagingChannel> elevated_channel_;
74 
75   // Timer to control the lifetime of the elevated host.
76   base::OneShotTimer elevated_host_timer_;
77 
78   base::ThreadChecker thread_checker_;
79 
80   DISALLOW_COPY_AND_ASSIGN(ElevatedNativeMessagingHost);
81 };
82 
83 }  // namespace remoting
84 
85 #endif  // REMOTING_HOST_WIN_ELEVATED_NATIVE_MESSAGING_HOST_H_
86