utils_win.h (2705B)
1 // Copyright 2022 The Chromium Authors. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Utility and helper functions common to both the agent and browser code. 6 // This code is not publicly exposed from the SDK. 7 8 #ifndef CONTENT_ANALYSIS_COMMON_UTILS_WIN_H_ 9 #define CONTENT_ANALYSIS_COMMON_UTILS_WIN_H_ 10 11 #include <string> 12 13 namespace content_analysis { 14 namespace sdk { 15 namespace internal { 16 17 // The default size of the buffer used to hold messages received from 18 // Google Chrome. 19 const DWORD kBufferSize = 4096; 20 21 // Named pipe prefixes used for agent and client side of pipe. 22 constexpr char kPipePrefixForAgent[] = R"(\\.\\pipe\)"; 23 constexpr char kPipePrefixForClient[] = R"(\Device\NamedPipe\)"; 24 25 // Returns the user SID of the thread or process that calls thie function. 26 // Returns an empty string on error. 27 std::string GetUserSID(); 28 29 // Returns the name of the pipe that should be used to communicate between 30 // the agent and Google Chrome. If `sid` is non-empty, make the pip name 31 // specific to that user. 32 // 33 // GetPipeNameForAgent() is meant to be used in the agent. The returned 34 // path can be used with CreatePipe() below. GetPipeNameForClient() is meant 35 // to be used in the client. The returned path can only be used with 36 // NtCreateFile() and not CreateFile(). 37 std::string GetPipeNameForAgent(const std::string& base, bool user_specific); 38 std::string GetPipeNameForClient(const std::string& base, bool user_specific); 39 40 // Creates a named pipe with the give name. If `is_first_pipe` is true, 41 // fail if this is not the first pipe using this name. 42 // 43 // This function create a pipe whose DACL allow full control to the creator 44 // owner and administrators. If `user_specific` the DACL only allows the 45 // logged on user to read from and write to the pipe. Otherwise anyone logged 46 // in can read from and write to the pipe. 47 // 48 // A handle to the pipe is retuned in `handle`. 49 DWORD CreatePipe(const std::string& name, 50 bool user_specific, 51 bool is_first_pipe, 52 HANDLE* handle); 53 54 // Returns the full path to the main binary file of the process with the given 55 // process ID. 56 bool GetProcessPath(unsigned long pid, std::string* binary_path); 57 58 // A class that scopes the creation and destruction of an OVERLAPPED structure 59 // used for async IO. 60 class ScopedOverlapped { 61 public: 62 ScopedOverlapped(); 63 ~ScopedOverlapped(); 64 65 bool is_valid() { return overlapped_.hEvent != nullptr; } 66 operator OVERLAPPED*() { return &overlapped_; } 67 68 private: 69 OVERLAPPED overlapped_; 70 }; 71 72 } // internal 73 } // namespace sdk 74 } // namespace content_analysis 75 76 #endif // CONTENT_ANALYSIS_COMMON_UTILS_WIN_H_