File: scanner_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (176 lines) | stat: -rw-r--r-- 5,867 bytes parent folder | download | duplicates (6)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/manta/scanner_provider.h"

#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/manta/base_provider.h"
#include "components/manta/features.h"
#include "components/manta/manta_service_callbacks.h"
#include "components/manta/manta_status.h"
#include "components/manta/proto/common.pb.h"
#include "components/manta/proto/manta.pb.h"
#include "components/manta/proto/scanner.pb.h"
#include "components/signin/public/identity_manager/identity_manager.h"

namespace manta {

namespace {

constexpr char kOauthConsumerName[] = "manta_scanner";
constexpr base::TimeDelta kTimeout = base::Seconds(30);
constexpr auto kTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("chromeos_scanner_provider", R"(
      semantics {
        sender: "ChromeOS Scanner"
        description:
          "Requests extracted details for a selected region of their screen "
          "from the Scanner service."
        trigger:
          "User selecting a region via the Scanner UI."
        internal {
          contacts {
            email: "e14s-eng@google.com"
          }
        }
        user_data {
          type: USER_CONTENT
        }
        data:
          "A selected region of their screen."
        destination: GOOGLE_OWNED_SERVICE
        last_reviewed: "2025-02-25"
      }
      policy {
        cookies_allowed: NO
        setting:
          "No setting. Users must take explicit action to trigger the feature."
        policy_exception_justification:
          "Not implemented, not considered useful. This request is part of a "
          "flow which is user-initiated."
      }
    )");

bool IsValidScannerInput(const proto::ScannerInput& scanner_input) {
  return scanner_input.image().size() > 0;
}

std::unique_ptr<proto::ScannerOutput> GetScannerOutput(
    const proto::Response& manta_response) {
  // There should only be one output data.
  if (manta_response.output_data_size() != 1) {
    return nullptr;
  }
  const proto::OutputData& output_data = manta_response.output_data(0);

  // There should be a custom proto.
  if (!output_data.has_custom()) {
    return nullptr;
  }

  // The custom proto should be a ScannerOutput.
  if (output_data.custom().type_url() != kScannerOutputTypeUrl) {
    return nullptr;
  }

  const proto::Proto3Any& custom_data = output_data.custom();

  auto scanner_output = std::make_unique<proto::ScannerOutput>();
  scanner_output->ParseFromString(custom_data.value());

  return scanner_output;
}

void OnServerResponseOrErrorReceived(
    ScannerProvider::ScannerProtoResponseCallback callback,
    std::unique_ptr<proto::Response> manta_response,
    MantaStatus manta_status) {
  // Check for Manta error status.
  if (manta_status.status_code != MantaStatusCode::kOk) {
    std::move(callback).Run(nullptr, std::move(manta_status));
    return;
  }

  // Check for a valid Manta response.
  if (manta_response == nullptr) {
    std::move(callback).Run(nullptr, {MantaStatusCode::kMalformedResponse});
    return;
  }

  // Check that the Manta response has output data.
  if (manta_response->output_data_size() == 0) {
    std::string message;

    // If there are no outputs, the response might have been filtered.
    if (manta_response->filtered_data_size() > 0 &&
        manta_response->filtered_data(0).is_output_data()) {
      message = base::StrCat({"filtered output for: ",
                              proto::FilteredReason_Name(
                                  manta_response->filtered_data(0).reason())});
    }
    std::move(callback).Run(nullptr,
                            {MantaStatusCode::kBlockedOutputs, message});
    return;
  }

  // Try to extract a ScannerOutput object from the Manta response.
  std::unique_ptr<proto::ScannerOutput> scanner_output =
      GetScannerOutput(*manta_response);
  if (scanner_output == nullptr) {
    std::move(callback).Run(nullptr, {MantaStatusCode::kMalformedResponse});
    return;
  }

  // All checks passed, run callback with valid ScannerOutput.
  std::move(callback).Run(std::move(scanner_output), std::move(manta_status));
}

}  // namespace

ScannerProvider::ScannerProvider(
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
    signin::IdentityManager* identity_manager,
    const ProviderParams& provider_params)
    : BaseProvider(url_loader_factory, identity_manager, provider_params) {}

ScannerProvider::~ScannerProvider() = default;

void ScannerProvider::Call(
    const manta::proto::ScannerInput& scanner_input,
    ScannerProvider::ScannerProtoResponseCallback done_callback) {
  // Check for valid ScannerInput.
  if (!IsValidScannerInput(scanner_input)) {
    std::move(done_callback).Run(nullptr, {MantaStatusCode::kInvalidInput});
    return;
  }
  // Populate Manta request with the specified ScannerInput as the custom input
  // data.
  proto::Request request;
  request.set_feature_name(proto::FeatureName::CHROMEOS_SCANNER);

  proto::InputData* input_data = request.add_input_data();
  input_data->set_tag("scanner_input");

  proto::Proto3Any& custom = *input_data->mutable_custom();
  custom.set_type_url(kScannerInputTypeUrl);
  custom.set_value(scanner_input.SerializeAsString());

  RequestInternal(
      GURL{GetProviderEndpoint(features::IsScannerUseProdServerEnabled())},
      kOauthConsumerName, /*annotation_tag=*/kTrafficAnnotation, request,
      MantaMetricType::kScanner,
      base::BindOnce(&OnServerResponseOrErrorReceived,
                     std::move(done_callback)),
      kTimeout);
}

}  // namespace manta