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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
// Copyright 2019 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/services/app_service/public/cpp/intent_util.h"
#include <algorithm>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/to_string.h"
#include "base/values.h"
#include "third_party/blink/public/common/mime_util/mime_util.h"
namespace {
const char kWildCardAny[] = "*";
const char kMimeTypeSeparator[] = "/";
constexpr size_t kMimeTypeComponentSize = 2;
const char kAuthorityHostPortSeparator[] = ":";
const char kActionKey[] = "action";
const char kUrlKey[] = "url";
const char kMimeTypeKey[] = "mime_type";
const char kFileUrlsKey[] = "file_urls";
const char kActivityNameKey[] = "activity_name";
const char kDriveShareUrlKey[] = "drive_share_url";
const char kShareTextKey[] = "share_text";
const char kShareTitleKey[] = "share_title";
const char kStartTypeKey[] = "start_type";
const char kCategoriesKey[] = "categories";
const char kDataKey[] = "data";
const char kUiBypassedKey[] = "ui_bypassed";
const char kExtrasKey[] = "extras";
const char kMimeTypeInodeDirectory[] = "inode/directory";
bool ComponentMatched(const std::string& intent_component,
const std::string& filter_component) {
return filter_component == kWildCardAny ||
intent_component == filter_component;
}
} // namespace
namespace apps_util {
const char kIntentActionMain[] = "main";
const char kIntentActionView[] = "view";
const char kIntentActionSend[] = "send";
const char kIntentActionSendMultiple[] = "send_multiple";
const char kIntentActionCreateNote[] = "create_note";
const char kIntentActionStartOnLockScreen[] = "start_on_lock_screen";
const char kIntentActionEdit[] = "edit";
const char kIntentActionPotentialFileHandler[] = "potential_file_handler";
const char kUseBrowserForLink[] = "use_browser";
const char kGuestOsActivityName[] = "open-with";
apps::IntentPtr MakeShareIntent(const std::vector<GURL>& filesystem_urls,
const std::vector<std::string>& mime_types) {
auto intent = std::make_unique<apps::Intent>(filesystem_urls.size() == 1
? kIntentActionSend
: kIntentActionSendMultiple);
intent->mime_type = CalculateCommonMimeType(mime_types);
DCHECK_EQ(filesystem_urls.size(), mime_types.size());
for (size_t i = 0; i < filesystem_urls.size(); i++) {
auto file = std::make_unique<apps::IntentFile>(filesystem_urls[i]);
file->mime_type = mime_types.at(i);
intent->files.push_back(std::move(file));
}
return intent;
}
apps::IntentPtr MakeShareIntent(const std::vector<GURL>& filesystem_urls,
const std::vector<std::string>& mime_types,
const std::string& text,
const std::string& title) {
auto intent = MakeShareIntent(filesystem_urls, mime_types);
if (!text.empty()) {
intent->share_text = text;
}
if (!title.empty()) {
intent->share_title = title;
}
return intent;
}
apps::IntentPtr MakeShareIntent(const GURL& filesystem_url,
const std::string& mime_type,
const GURL& drive_share_url,
bool is_directory) {
auto intent = std::make_unique<apps::Intent>(kIntentActionSend);
if (!is_directory) {
intent->mime_type = mime_type;
intent->files = std::vector<apps::IntentFilePtr>{};
auto file = std::make_unique<apps::IntentFile>(filesystem_url);
file->mime_type = mime_type;
intent->files.push_back(std::move(file));
}
if (!drive_share_url.is_empty()) {
intent->drive_share_url = drive_share_url;
}
return intent;
}
apps::IntentPtr MakeShareIntent(const std::string& text,
const std::string& title) {
auto intent = std::make_unique<apps::Intent>(kIntentActionSend);
intent->mime_type = "text/plain";
intent->share_text = text;
if (!title.empty()) {
intent->share_title = title;
}
return intent;
}
apps::IntentPtr MakeShareIntent(
const std::vector<GURL>& filesystem_urls,
const std::vector<std::string>& mime_types,
const std::vector<std::string>& dlp_source_urls) {
auto intent = MakeShareIntent(filesystem_urls, mime_types);
DCHECK_EQ(filesystem_urls.size(), dlp_source_urls.size());
for (size_t i = 0; i < filesystem_urls.size(); i++) {
intent->files[i]->dlp_source_url = dlp_source_urls[i];
}
return intent;
}
apps::IntentPtr MakeEditIntent(const GURL& filesystem_url,
const std::string& mime_type) {
auto intent = std::make_unique<apps::Intent>(kIntentActionEdit);
intent->mime_type = mime_type;
auto file = std::make_unique<apps::IntentFile>(filesystem_url);
file->mime_type = mime_type;
intent->files.push_back(std::move(file));
return intent;
}
apps::IntentPtr MakeIntentForActivity(const std::string& activity,
const std::string& start_type,
const std::string& category) {
auto intent = std::make_unique<apps::Intent>(kIntentActionMain);
intent->activity_name = activity;
intent->start_type = start_type;
intent->categories = std::vector<std::string>{category};
return intent;
}
apps::IntentPtr CreateCreateNoteIntent() {
return std::make_unique<apps::Intent>(kIntentActionCreateNote);
}
apps::IntentPtr CreateStartOnLockScreenIntent() {
return std::make_unique<apps::Intent>(kIntentActionStartOnLockScreen);
}
bool ConditionValueMatches(std::string_view value,
const apps::ConditionValuePtr& condition_value) {
return PatternMatchValue(value, condition_value->match_type,
condition_value->value);
}
bool PatternMatchValue(std::string_view test_value,
apps::PatternMatchType match_type,
std::string_view match_value) {
switch (match_type) {
case apps::PatternMatchType::kLiteral:
return test_value == match_value;
case apps::PatternMatchType::kPrefix:
return base::StartsWith(test_value, match_value,
base::CompareCase::INSENSITIVE_ASCII);
case apps::PatternMatchType::kSuffix:
return base::EndsWith(test_value, match_value,
base::CompareCase::INSENSITIVE_ASCII);
case apps::PatternMatchType::kGlob:
return MatchGlob(test_value, match_value);
case apps::PatternMatchType::kMimeType:
// kMimeType as a match for kFile is handled in FileMatchesConditionValue.
return MimeTypeMatched(test_value, match_value);
case apps::PatternMatchType::kFileExtension:
case apps::PatternMatchType::kIsDirectory: {
// Handled in FileMatchesConditionValue.
NOTREACHED();
}
}
}
bool IsGenericFileHandler(const apps::IntentPtr& intent,
const apps::IntentFilterPtr& filter) {
if (!intent || !filter || intent->files.empty()) {
return false;
}
std::set<std::string> mime_types;
std::set<std::string> file_extensions;
filter->GetMimeTypesAndExtensions(mime_types, file_extensions);
if (file_extensions.count("*") > 0 || mime_types.count("*") > 0 ||
mime_types.count("*/*") > 0) {
return true;
}
// If a text/* file handler matches with an unsupported text mime type, we
// regard it as a generic match.
if (mime_types.count("text/*")) {
for (const auto& file : intent->files) {
DCHECK(file);
if (file->mime_type.has_value() &&
blink::IsUnsupportedTextMimeType(file->mime_type.value())) {
return true;
}
}
}
// If directory is selected, it is generic unless mime_types included
// 'inode/directory'.
for (const auto& file : intent->files) {
DCHECK(file);
if (file->is_directory.value_or(false)) {
return mime_types.count(kMimeTypeInodeDirectory) == 0;
}
}
return false;
}
bool MatchGlob(std::string_view value, std::string_view pattern) {
static constexpr auto get_char = [](std::string_view s, size_t i) {
if (i >= s.length()) [[unlikely]] {
return '\0';
}
return s[i];
};
const size_t NP = pattern.length();
const size_t NS = value.length();
if (NP == 0) {
return NS == 0;
}
size_t ip = 0, is = 0;
char nextChar = get_char(pattern, 0);
while (ip < NP && is < NS) {
char c = nextChar;
++ip;
nextChar = get_char(pattern, ip);
const bool escaped = (c == '\\');
if (escaped) {
c = nextChar;
++ip;
nextChar = get_char(pattern, ip);
}
if (nextChar == '*') {
if (!escaped && c == '.') {
if (ip >= (NP - 1)) {
// At the end with a pattern match
return true;
}
++ip;
nextChar = get_char(pattern, ip);
// Consume everything until the next char in the pattern is found.
if (nextChar == '\\') {
++ip;
nextChar = get_char(pattern, ip);
}
do {
if (get_char(value, is) == nextChar) {
break;
}
++is;
} while (is < NS);
if (is == NS) {
// Next char in the pattern didn't exist in the match.
return false;
}
++ip;
nextChar = get_char(pattern, ip);
++is;
} else {
// Consume only characters matching the one before '*'.
do {
if (get_char(value, is) != c) {
break;
}
++is;
} while (is < NS);
++ip;
nextChar = get_char(pattern, ip);
}
} else {
if (c != '.' && get_char(value, is) != c) {
return false;
}
++is;
}
}
if (ip >= NP && is >= NS) {
// Reached the end of both strings
return true;
}
// One last check: we may have finished the match string, but still have a
// '.*' at the end of the pattern, which is still a match.
if (ip == NP - 2 && get_char(pattern, ip) == '.' &&
get_char(pattern, ip + 1) == '*') {
return true;
}
return false;
}
bool MimeTypeMatched(std::string_view intent_mime_type,
std::string_view filter_mime_type) {
std::vector<std::string> intent_components =
base::SplitString(intent_mime_type, kMimeTypeSeparator,
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
std::vector<std::string> filter_components =
base::SplitString(filter_mime_type, kMimeTypeSeparator,
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (intent_components.size() > kMimeTypeComponentSize ||
filter_components.size() > kMimeTypeComponentSize ||
intent_components.size() == 0 || filter_components.size() == 0) {
return false;
}
// If the filter component only contain main mime type, check if main type
// matches.
if (filter_components.size() == 1) {
return ComponentMatched(intent_components[0], filter_components[0]);
}
// If the intent component only contain main mime type, complete the
// mime type.
if (intent_components.size() == 1) {
intent_components.push_back(kWildCardAny);
}
// Both intent and intent filter can use wildcard for mime type.
for (size_t i = 0; i < kMimeTypeComponentSize; i++) {
if (!ComponentMatched(intent_components[i], filter_components[i])) {
return false;
}
}
return true;
}
bool ExtensionMatched(const std::string& file_name,
const std::string& filter_extension) {
if (filter_extension == kWildCardAny)
return true;
// Normalise to have a preceding ".".
std::string normalised_extension = filter_extension;
if (filter_extension.length() > 0 && filter_extension[0] != '.') {
normalised_extension = '.' + normalised_extension;
}
base::FilePath::StringType handler_extension =
base::FilePath::FromUTF8Unsafe(normalised_extension).Extension();
base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_name);
// Accept files whose extension or combined extension (e.g. ".tar.gz")
// match the filter extension.
return base::FilePath::CompareEqualIgnoreCase(handler_extension,
file_path.Extension()) ||
base::FilePath::CompareEqualIgnoreCase(handler_extension,
file_path.FinalExtension());
}
base::Value ConvertIntentToValue(const apps::IntentPtr& intent) {
base::Value::Dict intent_value;
intent_value.Set(kActionKey, intent->action);
if (intent->url.has_value()) {
DCHECK(intent->url.value().is_valid());
intent_value.Set(kUrlKey, intent->url.value().spec());
}
if (intent->mime_type.has_value() && !intent->mime_type.value().empty())
intent_value.Set(kMimeTypeKey, intent->mime_type.value());
if (!intent->files.empty()) {
base::Value::List file_urls_list;
for (const auto& file : intent->files) {
DCHECK(file->url.is_valid());
file_urls_list.Append(base::Value(file->url.spec()));
}
intent_value.Set(kFileUrlsKey, std::move(file_urls_list));
}
if (intent->activity_name.has_value() &&
!intent->activity_name.value().empty()) {
intent_value.Set(kActivityNameKey, intent->activity_name.value());
}
if (intent->drive_share_url.has_value()) {
DCHECK(intent->drive_share_url.value().is_valid());
intent_value.Set(kDriveShareUrlKey, intent->drive_share_url.value().spec());
}
if (intent->share_text.has_value() && !intent->share_text.value().empty())
intent_value.Set(kShareTextKey, intent->share_text.value());
if (intent->share_title.has_value() && !intent->share_title.value().empty())
intent_value.Set(kShareTitleKey, intent->share_title.value());
if (intent->start_type.has_value() && !intent->start_type.value().empty())
intent_value.Set(kStartTypeKey, intent->start_type.value());
if (!intent->categories.empty()) {
base::Value::List categories;
for (const auto& category : intent->categories) {
categories.Append(category);
}
intent_value.Set(kCategoriesKey, std::move(categories));
}
if (intent->data.has_value() && !intent->data.value().empty())
intent_value.Set(kDataKey, intent->data.value());
if (intent->ui_bypassed.has_value()) {
intent_value.Set(kUiBypassedKey, intent->ui_bypassed.value());
}
if (!intent->extras.empty()) {
base::Value::Dict extras;
for (const auto& extra : intent->extras) {
extras.Set(extra.first, extra.second);
}
intent_value.Set(kExtrasKey, std::move(extras));
}
return base::Value(std::move(intent_value));
}
std::optional<std::string> GetStringValueFromDict(const base::Value::Dict& dict,
const std::string& key_name) {
const base::Value* value = dict.Find(key_name);
if (!value)
return std::nullopt;
const std::string* string_value = value->GetIfString();
if (!string_value || string_value->empty())
return std::nullopt;
return *string_value;
}
std::optional<bool> GetBoolValueFromDict(const base::Value::Dict& dict,
const std::string& key_name) {
return dict.FindBool(key_name);
}
std::optional<GURL> GetGurlValueFromDict(const base::Value::Dict& dict,
const std::string& key_name) {
const std::string* url_spec = dict.FindString(key_name);
if (!url_spec)
return std::nullopt;
GURL url(*url_spec);
if (!url.is_valid())
return std::nullopt;
return url;
}
std::vector<apps::IntentFilePtr> GetFilesFromDict(const base::Value::Dict& dict,
const std::string& key_name) {
const base::Value::List* value = dict.FindList(key_name);
if (!value || value->empty())
return std::vector<apps::IntentFilePtr>();
std::vector<apps::IntentFilePtr> files;
for (const auto& item : *value) {
GURL url(item.GetString());
if (url.is_valid()) {
files.push_back(std::make_unique<apps::IntentFile>(url));
}
}
return files;
}
std::vector<std::string> GetCategoriesFromDict(const base::Value::Dict& dict,
const std::string& key_name) {
const base::Value::List* value = dict.FindList(key_name);
if (!value || value->empty())
return std::vector<std::string>();
std::vector<std::string> categories;
for (const auto& item : *value)
categories.push_back(item.GetString());
return categories;
}
base::flat_map<std::string, std::string> GetExtrasFromDict(
const base::Value::Dict& dict,
const std::string& key_name) {
const base::Value::Dict* value = dict.FindDict(key_name);
if (!value)
return base::flat_map<std::string, std::string>();
base::flat_map<std::string, std::string> extras;
for (auto pair : *value) {
if (pair.second.is_string())
extras[pair.first] = pair.second.GetString();
}
return extras;
}
apps::IntentPtr ConvertValueToIntent(base::Value&& value) {
base::Value::Dict* dict = value.GetIfDict();
if (!dict)
return nullptr;
return ConvertDictToIntent(*dict);
}
apps::IntentPtr ConvertDictToIntent(const base::Value::Dict& dict) {
auto action = GetStringValueFromDict(dict, kActionKey);
if (!action.has_value())
return nullptr;
auto intent = std::make_unique<apps::Intent>(action.value());
intent->url = GetGurlValueFromDict(dict, kUrlKey);
intent->mime_type = GetStringValueFromDict(dict, kMimeTypeKey);
intent->files = GetFilesFromDict(dict, kFileUrlsKey);
intent->activity_name = GetStringValueFromDict(dict, kActivityNameKey);
intent->drive_share_url = GetGurlValueFromDict(dict, kDriveShareUrlKey);
intent->share_text = GetStringValueFromDict(dict, kShareTextKey);
intent->share_title = GetStringValueFromDict(dict, kShareTitleKey);
intent->start_type = GetStringValueFromDict(dict, kStartTypeKey);
intent->categories = GetCategoriesFromDict(dict, kCategoriesKey);
intent->data = GetStringValueFromDict(dict, kDataKey);
intent->ui_bypassed = GetBoolValueFromDict(dict, kUiBypassedKey);
intent->extras = GetExtrasFromDict(dict, kExtrasKey);
return intent;
}
std::string CalculateCommonMimeType(
const std::vector<std::string>& mime_types) {
const std::string any_mime_type = std::string(kWildCardAny) +
std::string(kMimeTypeSeparator) +
std::string(kWildCardAny);
if (mime_types.size() == 0) {
return any_mime_type;
}
std::vector<std::string> common_type =
base::SplitString(mime_types[0], kMimeTypeSeparator,
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (common_type.size() != 2) {
return any_mime_type;
}
for (auto& mime_type : mime_types) {
std::vector<std::string> type =
base::SplitString(mime_type, kMimeTypeSeparator, base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
if (type.size() != kMimeTypeComponentSize) {
return any_mime_type;
}
if (common_type[0] != type[0]) {
return any_mime_type;
}
if (common_type[1] != type[1]) {
common_type[1] = kWildCardAny;
}
}
return common_type[0] + kMimeTypeSeparator + common_type[1];
}
SharedText ExtractSharedText(const std::string& share_text) {
SharedText shared_text;
std::string extracted_text = share_text;
GURL extracted_url;
size_t separator_pos = extracted_text.find_last_of(' ');
size_t newline_pos = extracted_text.find_last_of('\n');
if (newline_pos != std::string::npos &&
(separator_pos == std::string::npos || separator_pos < newline_pos)) {
separator_pos = newline_pos;
}
if (separator_pos == std::string::npos) {
extracted_url = GURL(extracted_text);
if (extracted_url.is_valid())
extracted_text.clear();
} else {
extracted_url = GURL(extracted_text.substr(separator_pos + 1));
if (extracted_url.is_valid())
extracted_text.erase(separator_pos);
}
if (!extracted_text.empty())
shared_text.text = extracted_text;
if (extracted_url.is_valid())
shared_text.url = extracted_url;
return shared_text;
}
// static
std::optional<std::string> AuthorityView::PortToString(const GURL& url) {
int port_number = url.EffectiveIntPort();
if (port_number == url::PORT_UNSPECIFIED) {
return std::nullopt;
}
return base::ToString(port_number);
}
// static
std::optional<std::string> AuthorityView::PortToString(
const url::Origin& origin) {
if (origin.port() == 0) {
return std::nullopt;
}
return base::ToString(origin.port());
}
// static
AuthorityView AuthorityView::Decode(std::string_view encoded_string) {
size_t i = encoded_string.find_last_of(kAuthorityHostPortSeparator);
if (i == std::string_view::npos) {
return {.host = encoded_string};
}
return {.host = encoded_string.substr(0, i),
.port = encoded_string.substr(i + 1)};
}
// static
std::string AuthorityView::Encode(const GURL& url) {
CHECK(url.is_valid());
return AuthorityView{.host = url.host(), .port = PortToString(url)}.Encode();
}
// static
std::string AuthorityView::Encode(const url::Origin& origin) {
CHECK(!origin.opaque());
return AuthorityView{.host = origin.host(), .port = PortToString(origin)}
.Encode();
}
std::string AuthorityView::Encode() {
if (!port) {
return std::string(host);
}
return base::StrCat({host, kAuthorityHostPortSeparator, *port});
}
} // namespace apps_util
|