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
|
// 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 <string>
#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 "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 kChromeVideoFrameSearchContextMenuItem[] = "cvfs";
constexpr char kChromeLensOverlayLocationBar[] = "crmntob";
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 kOneLensDesktopWebFullscreen[] = "df";
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::string GetEntryPointQueryString(lens::EntryPoint entry_point) {
switch (entry_point) {
case lens::CHROME_REGION_SEARCH_MENU_ITEM:
return kChromeRegionSearchMenuItem;
case lens::CHROME_SEARCH_WITH_GOOGLE_LENS_CONTEXT_MENU_ITEM:
return kChromeSearchWithGoogleLensContextMenuItem;
case lens::CHROME_VIDEO_FRAME_SEARCH_CONTEXT_MENU_ITEM:
return kChromeVideoFrameSearchContextMenuItem;
case lens::CHROME_LENS_OVERLAY_LOCATION_BAR:
return kChromeLensOverlayLocationBar;
case lens::UNKNOWN:
return "";
}
}
std::map<std::string, std::string> GetLensQueryParametersMap(
lens::EntryPoint ep) {
std::map<std::string, std::string> query_parameters;
// Insert EntryPoint query parameter.
std::string entry_point_query_string = GetEntryPointQueryString(ep);
if (!entry_point_query_string.empty()) {
query_parameters.insert(
{kEntryPointQueryParameter, entry_point_query_string});
}
// Insert RenderingEnvironment desktop fullscreen query parameter.
query_parameters.insert(
{kRenderingEnvironmentQueryParameter, kOneLensDesktopWebFullscreen});
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;
}
} // 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) {
GURL modified_url(url);
for (auto const& param : GetLensQueryParametersMap(ep)) {
modified_url = net::AppendOrReplaceQueryParameter(modified_url, param.first,
param.second);
}
return modified_url;
}
std::string GetQueryParametersForLensRequest(lens::EntryPoint ep) {
std::string query_string;
for (auto const& param : GetLensQueryParametersMap(ep)) {
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 IsLensMWebResult(const GURL& url) {
std::string request_id;
std::string surface;
GURL result_url = GURL(lens::features::GetLensOverlayResultsSearchURL());
return !url.is_empty() && url.host() == result_url.host() &&
url.path() == result_url.path() &&
net::GetValueForKeyInQuery(url, kLensRequestQueryParameter,
&request_id) &&
!net::GetValueForKeyInQuery(url, kLensSurfaceQueryParameter, &surface);
}
} // namespace lens
|