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
|
// 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/headless/screen_info/headless_screen_info.h"
#include <optional>
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "third_party/re2/src/re2/re2.h"
using re2::RE2;
namespace headless {
namespace {
// Screen Info parser error messages.
constexpr char kMissingScreenInfo[] = "Missing screen info.";
constexpr char kInvalidScreenInfo[] = "Invalid screen info: ";
constexpr char kUnknownScreenInfoParam[] = "Unknown screen info parameter: ";
constexpr char kInvalidScreenColorDepth[] = "Invalid screen color depth: ";
constexpr char kInvalidScreenIsInternal[] = "Invalid screen is internal: ";
constexpr char kInvalidScreenDevicePixelRatio[] =
"Invalid screen device pixel ratio: ";
constexpr char kInvalidWorkAreaInset[] = "Invalid work area inset: ";
constexpr char kInvalidRotation[] = "Invalid rotation: ";
constexpr char kNonZeroPrimaryScreenOrigin[] =
"Primary screen origin can only be at {0,0}";
// Screen Info parameters, keep in sync with window.getScreenDetails() output.
constexpr char kColorDepth[] = "colorDepth";
constexpr char kDevicePixelRatio[] = "devicePixelRatio";
constexpr char kIsInternal[] = "isInternal";
constexpr char kLabel[] = "label";
constexpr char kWorkAreaLeft[] = "workAreaLeft";
constexpr char kWorkAreaRight[] = "workAreaRight";
constexpr char kWorkAreaTop[] = "workAreaTop";
constexpr char kWorkAreaBottom[] = "workAreaBottom";
constexpr char kRotation[] = "rotation";
constexpr int kMinColorDepth = 1;
constexpr float kMinDevicePixelRatio = 0.5f;
void TrimAndUnescape(std::string* str) {
if (!str->empty() && str->front() == '\'') {
str->erase(0, 1);
}
if (!str->empty() && str->back() == '\'') {
str->erase(str->size() - 1);
}
base::ReplaceSubstringsAfterOffset(str, 0, "\\'", "'");
}
std::optional<bool> GetBooleanParam(std::string_view value) {
if (value == "1" || value == "true") {
return true;
}
if (value == "0" || value == "false") {
return false;
}
return std::nullopt;
}
// This is the same as display::Display::IsValidRotation(). It is replicated
// here to prevent dependency on //ui/display/ which is undesired because this
// file is intended to be included from there.
bool IsValidRotation(int degrees) {
return degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270;
}
// Parse screen info parameter key value pair returning an error message or
// an empty string if there is no error.
std::string ParseScreenInfoParameter(std::string_view key,
std::string_view value,
HeadlessScreenInfo* screen_info) {
// colorDepth=24
if (key == kColorDepth) {
int color_depth;
if (!base::StringToInt(value, &color_depth) ||
color_depth < kMinColorDepth) {
return kInvalidScreenColorDepth + std::string(value);
}
screen_info->color_depth = color_depth;
return {};
}
// devicePixelRatio=1.0
if (key == kDevicePixelRatio) {
double device_pixel_ratio;
if (!base::StringToDouble(value, &device_pixel_ratio) ||
device_pixel_ratio < kMinDevicePixelRatio) {
return kInvalidScreenDevicePixelRatio + std::string(value);
}
screen_info->device_pixel_ratio = static_cast<float>(device_pixel_ratio);
return {};
}
// isInternal=0|1|false|true
if (key == kIsInternal) {
std::optional<bool> is_internal_opt = GetBooleanParam(value);
if (!is_internal_opt) {
return kInvalidScreenIsInternal + std::string(value);
}
screen_info->is_internal = is_internal_opt.value();
return {};
}
// label='primary screen'
if (key == kLabel) {
screen_info->label = value;
return {};
}
// workAreaLeft=NNN
if (key == kWorkAreaLeft) {
int work_area_left;
if (!base::StringToInt(value, &work_area_left) || work_area_left < 0) {
return kInvalidWorkAreaInset + std::string(value);
}
screen_info->work_area_insets.set_left(work_area_left);
return {};
}
// workAreaRight=NNN
if (key == kWorkAreaRight) {
int work_area_right;
if (!base::StringToInt(value, &work_area_right) || work_area_right < 0) {
return kInvalidWorkAreaInset + std::string(value);
}
screen_info->work_area_insets.set_right(work_area_right);
return {};
}
// workAreaTop=NNN
if (key == kWorkAreaTop) {
int work_area_top;
if (!base::StringToInt(value, &work_area_top) || work_area_top < 0) {
return kInvalidWorkAreaInset + std::string(value);
}
screen_info->work_area_insets.set_top(work_area_top);
return {};
}
// workAreaBottom=NNN
if (key == kWorkAreaBottom) {
int work_area_bottom;
if (!base::StringToInt(value, &work_area_bottom) || work_area_bottom < 0) {
return kInvalidWorkAreaInset + std::string(value);
}
screen_info->work_area_insets.set_bottom(work_area_bottom);
return {};
}
// rotation=0|90|180|270
if (key == kRotation) {
int rotation;
if (!base::StringToInt(value, &rotation) || !IsValidRotation(rotation)) {
return kInvalidRotation + std::string(value);
}
screen_info->rotation = rotation;
return {};
}
return kUnknownScreenInfoParam + std::string(key);
}
// Parse a single screen info specification in the format of
// [X,Y] WxH [key=value ...] returning an error message or
// an empty string if there is no error.
std::string ParseOneScreenInfo(std::string_view screen_info,
std::vector<HeadlessScreenInfo>& result) {
HeadlessScreenInfo new_screen_info;
// Scan in the screen origin if any, matching any leading space, then
// optional '-' followed by one or more digits, followed by comma and
// another optional '-' followed by one or more digits.
int x, y;
if (RE2::Consume(&screen_info, R"(\s*(-?\d+),(-?\d+)\s*)", &x, &y)) {
new_screen_info.bounds.set_origin({x, y});
} else if (!result.empty()) {
// If no origin is given for a secondary screen shift it to the
// right of the previous screen so that they don't overlap.
const HeadlessScreenInfo& prev_screen = result.back();
new_screen_info.bounds.set_origin(
{prev_screen.bounds.right(), prev_screen.bounds.y()});
}
// Scan in the screen size if any, matching any leading white space followed
// by one or more digits, followed by an 'x' followed by one or more digits.
int width, height;
if (RE2::Consume(&screen_info, R"(\s*(\d+)x(\d+)\s*)", &width, &height)) {
new_screen_info.bounds.set_size({width, height});
}
// Scan in the screen info parameters key value pairs.
while (!screen_info.empty()) {
std::string key, value;
if (RE2::Consume(&screen_info,
R"(\s*([^= ]*)=('(?:\\.|[^'\\]+)*'|[^ ']*)\s*)", &key,
&value)) {
TrimAndUnescape(&key);
TrimAndUnescape(&value);
std::string error =
ParseScreenInfoParameter(key, value, &new_screen_info);
if (!error.empty()) {
return error;
}
} else {
std::string leftover_text;
base::TrimWhitespaceASCII(screen_info, base::TRIM_ALL, &leftover_text);
if (!leftover_text.empty()) {
return kInvalidScreenInfo + leftover_text;
}
break;
}
}
if (result.empty() && !new_screen_info.bounds.origin().IsOrigin()) {
return kNonZeroPrimaryScreenOrigin;
}
result.push_back(new_screen_info);
return {};
}
} // namespace
bool HeadlessScreenInfo::operator==(const HeadlessScreenInfo& other) const =
default;
// static
base::expected<std::vector<HeadlessScreenInfo>, std::string>
HeadlessScreenInfo::FromString(std::string_view screen_info) {
std::vector<HeadlessScreenInfo> result;
// Match leading space before '{', grab everything until the '}', followed
// by an optional ',' or spaces.
RE2 re(R"(\s*{([^}]*)}\s*,?\s*)");
while (!screen_info.empty()) {
std::string one_screen_info;
if (RE2::Consume(&screen_info, re, &one_screen_info)) {
std::string error = ParseOneScreenInfo(one_screen_info, result);
if (!error.empty()) {
return base::unexpected(error);
}
} else if (!screen_info.empty()) {
return base::unexpected(kInvalidScreenInfo + std::string(screen_info));
}
}
if (result.empty()) {
return base::unexpected(kMissingScreenInfo);
}
return base::ok(result);
}
std::string HeadlessScreenInfo::ToString() const {
return base::StringPrintf(
"%s color_depth=%d device_pixel_ratio=%g is_internal=%d label='%s' "
"workarea TLBR={%d,%d,%d,%d} rotation=%d",
bounds.ToString().c_str(), color_depth, device_pixel_ratio, is_internal,
label.c_str(), work_area_insets.top(), work_area_insets.left(),
work_area_insets.bottom(), work_area_insets.right(), rotation);
}
} // namespace headless
|