tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

scoped_print_handle_win.cc (2100B)


      1 // Copyright 2023 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 #include "scoped_print_handle_win.h"
      6 
      7 namespace content_analysis {
      8 namespace sdk {
      9 
     10 std::unique_ptr<ScopedPrintHandle>
     11 CreateScopedPrintHandle(const ContentAnalysisRequest& request,
     12                        int64_t browser_pid) {
     13  if (!request.has_print_data() || !request.print_data().has_handle()) {
     14    return nullptr;
     15  }
     16 
     17  // The handle in the request must be duped to be read by the agent
     18  // process. If that doesn't work for any reason, return null.
     19  // See https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle
     20  // for details.
     21  HANDLE browser_process = OpenProcess(
     22      /*dwDesiredAccess=*/PROCESS_DUP_HANDLE,
     23      /*bInheritHandle=*/false,
     24      /*dwProcessId=*/browser_pid);
     25  if (!browser_process)
     26    return nullptr;
     27 
     28  HANDLE dupe = nullptr;
     29  DuplicateHandle(
     30      /*hSourceProcessHandle=*/browser_process,
     31      /*hSourceHandle=*/reinterpret_cast<HANDLE>(request.print_data().handle()),
     32      /*hTargetProcessHandle=*/GetCurrentProcess(),
     33      /*lpTargetHandle=*/&dupe,
     34      /*dwDesiredAccess=*/PROCESS_DUP_HANDLE | FILE_MAP_READ,
     35      /*bInheritHandle=*/false,
     36      /*dwOptions=*/0);
     37 
     38  CloseHandle(browser_process);
     39 
     40  if (!dupe)
     41    return nullptr;
     42 
     43  ContentAnalysisRequest::PrintData dupe_print_data;
     44  dupe_print_data.set_handle(reinterpret_cast<int64_t>(dupe));
     45  dupe_print_data.set_size(request.print_data().size());
     46 
     47 
     48  return std::make_unique<ScopedPrintHandleWin>(dupe_print_data);
     49 }
     50 
     51 ScopedPrintHandleWin::ScopedPrintHandleWin(
     52    const ContentAnalysisRequest::PrintData& print_data)
     53    : ScopedPrintHandleBase(print_data),
     54      handle_(reinterpret_cast<HANDLE>(print_data.handle())) {
     55  mapped_ = MapViewOfFile(handle_, FILE_MAP_READ, 0, 0, 0);
     56 }
     57 
     58 ScopedPrintHandleWin::~ScopedPrintHandleWin() {
     59  CloseHandle(handle_);
     60 }
     61 
     62 const char* ScopedPrintHandleWin::data() {
     63  return reinterpret_cast<const char*>(mapped_);
     64 }
     65 
     66 }  // namespace sdk
     67 }  // namespace content_analysis