summaryrefslogtreecommitdiffstats
path: root/third_party/content_analysis_sdk/agent/src/event_win.cc
blob: 907bdfb8582aff1781e3ea442cfbbbde92991ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 The Chromium Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <ios>
#include <iostream>
#include <sstream>
#include <utility>

#include "event_win.h"

#include "common/utils_win.h"

#include "agent_utils_win.h"
#include "scoped_print_handle_win.h"

namespace content_analysis {
namespace sdk {

// Writes a string to the pipe. Returns ERROR_SUCCESS if successful, else
// returns GetLastError() of the write.  This function does not return until
// the entire message has been sent (or an error occurs).
static DWORD WriteMessageToPipe(HANDLE pipe, const std::string& message) {
  if (message.empty()) {
    return ERROR_SUCCESS;
  }

  internal::ScopedOverlapped overlapped;
  if (!overlapped.is_valid()) {
    return GetLastError();
  }

  DWORD err = ERROR_SUCCESS;
  const char* cursor = message.data();
  for (DWORD size = message.length(); size > 0;) {
    if (WriteFile(pipe, cursor, size, /*written=*/nullptr, overlapped)) {
      err = ERROR_SUCCESS;
      break;
    }

    // If an I/O is not pending, return the error.
    err = GetLastError();
    if (err != ERROR_IO_PENDING) {
      break;
    }

    DWORD written;
    if (!GetOverlappedResult(pipe, overlapped, &written, /*wait=*/TRUE)) {
      err = GetLastError();
      break;
    }

    cursor += written;
    size -= written;
  }

  return err;
}


ContentAnalysisEventWin::ContentAnalysisEventWin(
    HANDLE handle,
    const BrowserInfo& browser_info,
    ContentAnalysisRequest req)
    : ContentAnalysisEventBase(browser_info),
      hPipe_(handle) {
  *request() = std::move(req);
}

ContentAnalysisEventWin::~ContentAnalysisEventWin() {
  Shutdown();
}

ResultCode ContentAnalysisEventWin::Init() {
  // TODO(rogerta): do some extra validation of the request?
  if (request()->request_token().empty()) {
    return ResultCode::ERR_MISSING_REQUEST_TOKEN;
  }

  response()->set_request_token(request()->request_token());

  // Prepare the response so that ALLOW verdicts are the default().
  return UpdateResponse(*response(),
      request()->tags_size() > 0 ? request()->tags(0) : std::string(),
      ContentAnalysisResponse::Result::SUCCESS);
}

ResultCode ContentAnalysisEventWin::Close() {
  Shutdown();
  return ContentAnalysisEventBase::Close();
}

ResultCode ContentAnalysisEventWin::Send() {
  if (response_sent_) {
    return ResultCode::ERR_RESPONSE_ALREADY_SENT;
  }

  response_sent_ = true;

  DWORD err = WriteMessageToPipe(hPipe_,
                                 agent_to_chrome()->SerializeAsString());
  return ErrorToResultCode(err);
}

std::string ContentAnalysisEventWin::DebugString() const {
  std::stringstream state;
  state.setf(std::ios::boolalpha);
  state << "ContentAnalysisEventWin{handle=" << hPipe_;
  state << " pid=" << GetBrowserInfo().pid;
  state << " rtoken=" << GetRequest().request_token();
  state << " sent="  << response_sent_;
  state << "}" << std::ends;

  return state.str();
}

void ContentAnalysisEventWin::Shutdown() {
  if (hPipe_ != INVALID_HANDLE_VALUE) {
    // If no response has been sent yet, attempt to send it now.  Otherwise
    // the client may be stuck waiting.  After shutdown the agent will not
    // have any other chance to respond.
    if (!response_sent_) {
      Send();
    }

    // This event does not own the pipe, so don't close it.
    FlushFileBuffers(hPipe_);
    hPipe_ = INVALID_HANDLE_VALUE;
  }
}

}  // namespace sdk
}  // namespace content_analysis