File: lens_url_utils.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (247 lines) | stat: -rw-r--r-- 9,121 bytes parent folder | download
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
// Copyright 2021 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/lens/lens_url_utils.h"

#include <map>

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "components/lens/lens_entrypoints.h"
#include "components/lens/lens_features.h"
#include "components/lens/lens_metadata.h"
#include "components/lens/lens_metadata.mojom.h"
#include "components/lens/lens_rendering_environment.h"
#include "net/base/url_util.h"
#include "url/gurl.h"

namespace {

// Entry point string names.
constexpr char kEntryPointQueryParameter[] = "ep";
constexpr char kChromeRegionSearchMenuItem[] = "crs";
constexpr char kChromeSearchWithGoogleLensContextMenuItem[] = "ccm";
constexpr char kChromeTranslateImageWithGoogleLensContextMenuItem[] = "ctrcm";
constexpr char kChromeOpenNewTabSidePanel[] = "cnts";
constexpr char kChromeFullscreenSearchMenuItem[] = "cfs";
constexpr char kCompanionRegionSearch[] = "cscidr";

constexpr char kSurfaceQueryParameter[] = "s";
// The value of Surface.CHROMIUM expected by Lens Web
constexpr char kChromiumSurfaceProtoValue[] = "4";

constexpr char kStartTimeQueryParameter[] = "st";
constexpr char kLensMetadataParameter[] = "lm";

constexpr char kRenderingEnvironmentQueryParameter[] = "re";
constexpr char kOneLensDesktopWebChromeSidePanel[] = "dcsp";
constexpr char kOneLensDesktopWebFullscreen[] = "df";
constexpr char kOneLensAmbientVisualSearchWebFullscreen[] = "avsf";
constexpr char kChromeSearchCompanion[] = "csc";
constexpr char kViewportWidthQueryParameter[] = "vpw";
constexpr char kViewportHeightQueryParameter[] = "vph";
// Query parameter for source (aka Access Point).
constexpr char kSourceQueryParameter[] = "source";
constexpr char kSourceQueryParameterValue[] = "chrome.gsc";

void AppendQueryParam(std::string* query_string,
                      const char name[],
                      const char value[]) {
  if (!query_string->empty()) {
    base::StrAppend(query_string, {"&"});
  }
  base::StrAppend(query_string, {name, "=", value});
}

std::map<std::string, std::string> GetLensQueryParametersMap(
    lens::EntryPoint ep,
    lens::RenderingEnvironment re,
    bool is_side_panel_request) {
  std::map<std::string, std::string> query_parameters;
  switch (ep) {
    case lens::CHROME_OPEN_NEW_TAB_SIDE_PANEL:
      query_parameters.insert(
          {kEntryPointQueryParameter, kChromeOpenNewTabSidePanel});
      break;
    case lens::CHROME_REGION_SEARCH_MENU_ITEM:
      query_parameters.insert(
          {kEntryPointQueryParameter, kChromeRegionSearchMenuItem});
      break;
    case lens::CHROME_SEARCH_WITH_GOOGLE_LENS_CONTEXT_MENU_ITEM:
      query_parameters.insert({kEntryPointQueryParameter,
                               kChromeSearchWithGoogleLensContextMenuItem});
      break;
    case lens::CHROME_TRANSLATE_IMAGE_WITH_GOOGLE_LENS_CONTEXT_MENU_ITEM:
      query_parameters.insert(
          {kEntryPointQueryParameter,
           kChromeTranslateImageWithGoogleLensContextMenuItem});
      break;
    case lens::CHROME_FULLSCREEN_SEARCH_MENU_ITEM:
      query_parameters.insert(
          {kEntryPointQueryParameter, kChromeFullscreenSearchMenuItem});
      break;
    case lens::COMPANION_REGION_SEARCH:
      query_parameters.insert(
          {kEntryPointQueryParameter, kCompanionRegionSearch});
      break;
    default:
      // Empty strings are ignored when query parameters are built.
      break;
  }
  switch (re) {
    case lens::ONELENS_DESKTOP_WEB_CHROME_SIDE_PANEL:
      query_parameters.insert({kRenderingEnvironmentQueryParameter,
                               kOneLensDesktopWebChromeSidePanel});
      break;
    case lens::ONELENS_DESKTOP_WEB_FULLSCREEN:
      query_parameters.insert(
          {kRenderingEnvironmentQueryParameter, kOneLensDesktopWebFullscreen});
      break;
    case lens::ONELENS_AMBIENT_VISUAL_SEARCH_WEB_FULLSCREEN:
      query_parameters.insert({kRenderingEnvironmentQueryParameter,
                               kOneLensAmbientVisualSearchWebFullscreen});
      break;
    case lens::CHROME_SEARCH_COMPANION:
      query_parameters.insert(
          {kRenderingEnvironmentQueryParameter, kChromeSearchCompanion});
      query_parameters.insert(
          {kSourceQueryParameter, kSourceQueryParameterValue});
      break;
    default:
      // Empty strings are ignored when query parameters are built.
      break;
  }

  query_parameters.insert({kSurfaceQueryParameter, kChromiumSurfaceProtoValue});
  int64_t current_time_ms = base::Time::Now().InMillisecondsSinceUnixEpoch();
  query_parameters.insert(
      {kStartTimeQueryParameter, base::NumberToString(current_time_ms)});
  return query_parameters;
}

lens::RenderingEnvironment GetRenderingEnvironment(
    bool is_lens_side_panel_request,
    bool is_full_screen_region_search_request,
    bool is_companion_request) {
  if (is_companion_request) {
    return lens::RenderingEnvironment::CHROME_SEARCH_COMPANION;
  }

  if (is_full_screen_region_search_request)
    return lens::RenderingEnvironment::
        ONELENS_AMBIENT_VISUAL_SEARCH_WEB_FULLSCREEN;

  if (is_lens_side_panel_request) {
    return lens::RenderingEnvironment::ONELENS_DESKTOP_WEB_CHROME_SIDE_PANEL;
  }

  return lens::RenderingEnvironment::ONELENS_DESKTOP_WEB_FULLSCREEN;
}
}  // namespace

namespace lens {

void AppendLogsQueryParam(
    std::string* query_string,
    const std::vector<lens::mojom::LatencyLogPtr>& log_data) {
  if (!log_data.empty()) {
    AppendQueryParam(query_string, kLensMetadataParameter,
                     LensMetadata::CreateProto(std::move(log_data)).c_str());
  }
}

GURL AppendOrReplaceQueryParametersForLensRequest(const GURL& url,
                                                  lens::EntryPoint ep,
                                                  lens::RenderingEnvironment re,
                                                  bool is_side_panel_request) {
  GURL modified_url(url);
  for (auto const& param :
       GetLensQueryParametersMap(ep, re, is_side_panel_request)) {
    modified_url = net::AppendOrReplaceQueryParameter(modified_url, param.first,
                                                      param.second);
  }

  // Remove the viewport width and height params if the request is not a side
  // panel request.
  if (!is_side_panel_request) {
    modified_url = net::AppendOrReplaceQueryParameter(
        modified_url, kViewportWidthQueryParameter, absl::nullopt);
    modified_url = net::AppendOrReplaceQueryParameter(
        modified_url, kViewportHeightQueryParameter, absl::nullopt);
  }
  return modified_url;
}

GURL AppendOrReplaceStartTimeIfLensRequest(const GURL& url) {
  if (!IsLensUrl(url)) {
    return url;
  }

  GURL modified_url(url);

  int64_t current_time_ms = base::Time::Now().InMillisecondsSinceUnixEpoch();
  modified_url =
      net::AppendOrReplaceQueryParameter(modified_url, kStartTimeQueryParameter,
                                         base::NumberToString(current_time_ms));
  return modified_url;
}

GURL AppendOrReplaceViewportSizeForRequest(const GURL& url,
                                           const gfx::Size& viewport_size) {
  GURL modified_url(url);

  const int viewport_width = viewport_size.width();
  const int viewport_height = viewport_size.height();
  if (viewport_width != 0) {
    modified_url = net::AppendOrReplaceQueryParameter(
        modified_url, kViewportWidthQueryParameter,
        base::NumberToString(viewport_width));
  }
  if (viewport_height != 0) {
    modified_url = net::AppendOrReplaceQueryParameter(
        modified_url, kViewportHeightQueryParameter,
        base::NumberToString(viewport_height));
  }
  base::UmaHistogramBoolean("Search.Lens.ViewportDimensionsSent.Success",
                            viewport_width != 0 && viewport_height != 0);
  return modified_url;
}

std::string GetQueryParametersForLensRequest(
    lens::EntryPoint ep,
    bool is_lens_side_panel_request,
    bool is_full_screen_region_search_request,
    bool is_companion_request) {
  auto re = GetRenderingEnvironment(is_lens_side_panel_request,
                                    is_full_screen_region_search_request,
                                    is_companion_request);
  std::string query_string;
  const bool is_side_panel_request =
      is_lens_side_panel_request || is_companion_request;
  for (auto const& param :
       GetLensQueryParametersMap(ep, re, is_side_panel_request)) {
    AppendQueryParam(&query_string, param.first.c_str(), param.second.c_str());
  }
  return query_string;
}

bool IsValidLensResultUrl(const GURL& url) {
  if (url.is_empty()) {
    return false;
  }

  std::string payload;
  // Make sure the payload is present
  return net::GetValueForKeyInQuery(url, kPayloadQueryParameter, &payload);
}

bool IsLensUrl(const GURL& url) {
  return !url.is_empty() &&
         url.host() == GURL(lens::features::GetHomepageURLForLens()).host();
}

}  // namespace lens