File: lens_overlay_gen204_controller.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 (328 lines) | stat: -rw-r--r-- 13,760 bytes parent folder | download | duplicates (3)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// 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 "chrome/browser/ui/lens/lens_overlay_gen204_controller.h"

#include "base/base64url.h"
#include "base/containers/span.h"
#include "base/format_macros.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/lens/lens_overlay_url_builder.h"
#include "components/base32/base32.h"
#include "components/lens/lens_features.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/search_engines/template_url_service.h"
#include "net/base/url_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace lens {

namespace {

using LatencyType = LensOverlayGen204Controller::LatencyType;

constexpr int kMaxDownloadBytes = 1024 * 1024;

// Task completion ids.
constexpr int kCopyAsImageTaskCompletionID = 233325;
constexpr int kCopyTextTaskCompletionID = 198153;
constexpr int kSaveAsImageTaskCompletionID = 233326;
constexpr int kSelectTextTaskCompletionID = 198157;
constexpr int kTranslateTaskCompletionID = 198158;

// Semantic event ids.
constexpr int kTextGleamsViewStartSemanticEventID = 234181;
constexpr int kTextGleamsViewEndSemanticEventID = 234180;

// Query parameter keys.
constexpr char kEncodedAnalyticsIdParameter[] = "cad";
constexpr char kEncodedRequestIdParameter[] = "vsrid";
constexpr char kGen204IdentifierQueryParameter[] = "plla";
constexpr char kLatencyRequestTypeQueryParameter[] = "rt";
constexpr char kVisualInputTypeQueryParameter[] = "vit";
// Event id param used for both semantic events and task completions.
constexpr char kEventIdParameter[] = "rcid";

// Request type parameter values.
constexpr char kFullPageObjectsFetchLatencyId[] = "fpof";
constexpr char kFullPageTranslateFetchLatencyId[] = "fptf";
constexpr char kPageContentUploadLatencyId[] = "pcu";
constexpr char kPartialPageContentUploadLatencyId[] = "ppcu";
constexpr char kInteractionFetchLatencyId[] = "lif";
constexpr char kFetchStickyClusterInfoLatencyId[] = "sctr";
constexpr char kInvocationToInitialClusterInfoRequestLatencyId[] = "cstcirs";
constexpr char kInvocationToInitialFullObjectsRequestSentLatencyId[] =
    "cstiors";
constexpr char kInvocationToInitialFullObjectsResponseReceivedLatencyId[] =
    "cstiorr";
constexpr char kInvocationToInitialInteractionRequestLatencyId[] = "cstiirs";
constexpr char kInvocationToInitialPageContentRequestLatencyId[] = "cstipcurs";
constexpr char kInvocationToInitialPartialPageContentRequestLatencyId[] =
    "cstippcurs";

constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotationTag =
    net::DefineNetworkTrafficAnnotation("lens_overlay_gen204", R"(
        semantics {
          sender: "Lens"
          description: "A request to the gen204 endpoint for the Lens "
            "Overlay feature in Chrome."
          trigger: "The user triggered a Lens Overlay Flow by entering "
            "the experience via the right click menu option for "
            "searching images on the page. This annotation corresponds "
            "to the gen204 logging network requests sent by the Lens "
            "overlay to track latency and interaction data when the "
            "user is opted into metrics reporting."
          data: "Timestamp and interaction data. Only the action type "
            "(e.g. the  user selected text) and timestamp data is sent, "
            "along with basic state information from the query controller."
          destination: GOOGLE_OWNED_SERVICE
          internal {
            contacts {
              email: "hujasonx@google.com"
            }
            contacts {
              email: "lens-chrome@google.com"
            }
          }
          user_data {
            type: USER_CONTENT
            type: WEB_CONTENT
          }
          last_reviewed: "2024-09-24"
        }
        policy {
          cookies_allowed: YES
          cookies_store: "user"
          setting: "This feature is only shown in menus by default and does "
            "nothing without explicit user action. It will be disabled if "
            "the user is not opted into metrics reporting, which is on by "
            "default."
          chrome_policy {
            LensOverlaySettings {
              LensOverlaySettings: 1
            }
            MetricsReportingEnabled{
              policy_options {mode: MANDATORY}
              MetricsReportingEnabled: false
            }
          }
        }
      )");

std::string LatencyIdForType(LatencyType latency_type) {
  switch (latency_type) {
    case LatencyType::kInvocationToInitialClusterInfoRequestSent:
      return kInvocationToInitialClusterInfoRequestLatencyId;
    case LatencyType::kInvocationToInitialFullPageObjectsRequestSent:
      return kInvocationToInitialFullObjectsRequestSentLatencyId;
    case LatencyType::kInvocationToInitialFullPageObjectsResponseReceived:
      return kInvocationToInitialFullObjectsResponseReceivedLatencyId;
    case LatencyType::kInvocationToInitialInteractionRequestSent:
      return kInvocationToInitialInteractionRequestLatencyId;
    case LatencyType::kInvocationToInitialPageContentRequestSent:
      return kInvocationToInitialPageContentRequestLatencyId;
    case LatencyType::kInvocationToInitialPartialPageContentRequestSent:
      return kInvocationToInitialPartialPageContentRequestLatencyId;
    case LatencyType::kFullPageObjectsRequestFetchLatency:
      return kFullPageObjectsFetchLatencyId;
    case LatencyType::kFullPageTranslateRequestFetchLatency:
      return kFullPageTranslateFetchLatencyId;
    case LatencyType::kInteractionRequestFetchLatency:
      return kInteractionFetchLatencyId;
    case LatencyType::kPageContentUploadLatency:
      return kPageContentUploadLatencyId;
    case LatencyType::kPartialPageContentUploadLatency:
      return kPartialPageContentUploadLatencyId;
  }
}

std::string EncodeRequestId(const lens::LensOverlayRequestId& request_id) {
  std::string serialized_request_id;
  CHECK(request_id.SerializeToString(&serialized_request_id));
  std::string encoded_request_id;
  base::Base64UrlEncode(serialized_request_id,
                        base::Base64UrlEncodePolicy::OMIT_PADDING,
                        &encoded_request_id);
  return encoded_request_id;
}

}  // namespace

LensOverlayGen204Controller::LensOverlayGen204Controller() = default;
LensOverlayGen204Controller::~LensOverlayGen204Controller() = default;

void LensOverlayGen204Controller::OnQueryFlowStart(
    lens::LensOverlayInvocationSource invocation_source,
    Profile* profile,
    uint64_t gen204_id) {
  invocation_source_ = invocation_source;
  profile_ = profile;
  gen204_id_ = gen204_id;
}

void LensOverlayGen204Controller::SendLatencyGen204IfEnabled(
    LatencyType latency_type,
    base::TimeDelta latency_duration,
    std::string vit_query_param_value,
    std::optional<base::TimeDelta> cluster_info_latency,
    std::optional<std::string> encoded_analytics_id,
    std::optional<lens::LensOverlayRequestId> request_id) {
  if (profile_ && lens::features::GetLensOverlaySendLatencyGen204()) {
    std::string cluster_info_latency_string =
        cluster_info_latency.has_value() &&
                latency_type == LatencyType::kFullPageObjectsRequestFetchLatency
            ? base::StringPrintf(
                  ",%s.%s", kFetchStickyClusterInfoLatencyId,
                  base::NumberToString(cluster_info_latency->InMilliseconds())
                      .c_str())
            : "";
    // PRIu64 and PRId64 are macros for formatting uint64_t and int64_t
    // respectively, allowing us to bypass using NumberToString.
    std::string query = base::StringPrintf(
        "gen_204?atyp=csi&%s=%" PRIu64 "&%s=%s.%" PRId64 "%s&s=web&%s=%s",
        kGen204IdentifierQueryParameter, gen204_id_,
        kLatencyRequestTypeQueryParameter,
        LatencyIdForType(latency_type).c_str(),
        latency_duration.InMilliseconds(), cluster_info_latency_string,
        kVisualInputTypeQueryParameter, vit_query_param_value);
    auto fetch_url = GURL(TemplateURLServiceFactory::GetForProfile(profile_)
                              ->search_terms_data()
                              .GoogleBaseURLValue())
                         .Resolve(query);
    fetch_url =
        lens::AppendInvocationSourceParamToURL(fetch_url, invocation_source_);
    if (encoded_analytics_id.has_value()) {
      fetch_url = net::AppendOrReplaceQueryParameter(
          fetch_url, kEncodedAnalyticsIdParameter,
          encoded_analytics_id.value());
    }
    if (request_id.has_value()) {
      fetch_url = net::AppendOrReplaceQueryParameter(
          fetch_url, kEncodedRequestIdParameter,
          EncodeRequestId(request_id.value()));
    }
    CheckMetricsConsentAndIssueGen204NetworkRequest(fetch_url);
  }
}

void LensOverlayGen204Controller::SendTaskCompletionGen204IfEnabled(
    std::string encoded_analytics_id,
    lens::mojom::UserAction user_action,
    lens::LensOverlayRequestId request_id) {
  if (profile_ && lens::features::GetLensOverlaySendTaskCompletionGen204()) {
    int task_id;
    switch (user_action) {
      case mojom::UserAction::kTextSelection:
        [[fallthrough]];
      case mojom::UserAction::kTranslateTextSelection:
        task_id = kSelectTextTaskCompletionID;
        break;
      case mojom::UserAction::kCopyText:
        task_id = kCopyTextTaskCompletionID;
        break;
      case mojom::UserAction::kTranslateText:
        task_id = kTranslateTaskCompletionID;
        break;
      case mojom::UserAction::kCopyAsImage:
        task_id = kCopyAsImageTaskCompletionID;
        break;
      case mojom::UserAction::kSaveAsImage:
        task_id = kSaveAsImageTaskCompletionID;
        break;
      default:
        // Other user actions should not send an associated gen204 ping.
        return;
    }
    std::string query = base::StringPrintf(
        "gen_204?uact=4&%s=%" PRIu64 "&%s=%d&%s=%s&%s=%s",
        kGen204IdentifierQueryParameter, gen204_id_, kEventIdParameter, task_id,
        kEncodedAnalyticsIdParameter, encoded_analytics_id.c_str(),
        kEncodedRequestIdParameter, EncodeRequestId(request_id).c_str());
    auto fetch_url = GURL(TemplateURLServiceFactory::GetForProfile(profile_)
                              ->search_terms_data()
                              .GoogleBaseURLValue())
                         .Resolve(query);
    fetch_url =
        lens::AppendInvocationSourceParamToURL(fetch_url, invocation_source_);
    CheckMetricsConsentAndIssueGen204NetworkRequest(fetch_url);
  }
}

void LensOverlayGen204Controller::SendSemanticEventGen204IfEnabled(
    lens::mojom::SemanticEvent event,
    std::optional<lens::LensOverlayRequestId> request_id) {
  if (profile_ && lens::features::GetLensOverlaySendSemanticEventGen204()) {
    int event_id;
    switch (event) {
      case mojom::SemanticEvent::kTextGleamsViewStart:
        event_id = kTextGleamsViewStartSemanticEventID;
        break;
      case mojom::SemanticEvent::kTextGleamsViewEnd:
        event_id = kTextGleamsViewEndSemanticEventID;
        break;
    }
    std::string query = base::StringPrintf(
        "gen_204?uact=1&%s=%d&zx=%" PRId64 "&%s=%" PRIu64, kEventIdParameter,
        event_id, base::Time::Now().InMillisecondsSinceUnixEpoch(),
        kGen204IdentifierQueryParameter, gen204_id_);
    auto fetch_url = GURL(TemplateURLServiceFactory::GetForProfile(profile_)
                              ->search_terms_data()
                              .GoogleBaseURLValue())
                         .Resolve(query);
    fetch_url =
        lens::AppendInvocationSourceParamToURL(fetch_url, invocation_source_);
    if (request_id.has_value()) {
      fetch_url = net::AppendOrReplaceQueryParameter(
          fetch_url, kEncodedRequestIdParameter,
          EncodeRequestId(request_id.value()));
    }
    CheckMetricsConsentAndIssueGen204NetworkRequest(fetch_url);
  }
}

void LensOverlayGen204Controller::OnQueryFlowEnd() {
  // Send a text gleams view end event because the event trigger from webui
  // will not fire when the overlay is closing. The server will dedupe
  // end events.
  SendSemanticEventGen204IfEnabled(mojom::SemanticEvent::kTextGleamsViewEnd,
                                   /*request_id=*/std::nullopt);
  profile_ = nullptr;
}

void LensOverlayGen204Controller::
    CheckMetricsConsentAndIssueGen204NetworkRequest(GURL url) {
  if (!g_browser_process->GetMetricsServicesManager()
           ->IsMetricsConsentGiven()) {
    return;
  }
  auto request = std::make_unique<network::ResourceRequest>();
  request->url = url;
  gen204_loaders_.push_back(network::SimpleURLLoader::Create(
      std::move(request), kTrafficAnnotationTag));
  gen204_loaders_.back()->DownloadToString(
      profile_->GetURLLoaderFactory().get(),
      base::BindOnce(&LensOverlayGen204Controller::OnGen204NetworkResponse,
                     weak_ptr_factory_.GetWeakPtr(),
                     gen204_loaders_.back().get()),
      kMaxDownloadBytes);
}

void LensOverlayGen204Controller::OnGen204NetworkResponse(
    const network::SimpleURLLoader* source,
    std::unique_ptr<std::string> response_body) {
  std::erase_if(
      gen204_loaders_,
      [source](const std::unique_ptr<network::SimpleURLLoader>& loader) {
        return loader.get() == source;
      });
}

}  // namespace lens