File: companion_app_parser.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 4,067 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/companion_app/companion_app_parser.h"

#include <optional>
#include <string_view>

#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/repository/fast_pair/device_metadata.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "components/cross_device/logging/logging.h"

namespace {
constexpr char kIntentKeyPrefix[] = "intent:";
constexpr char kIntentPrefix[] = "#Intent";
constexpr char kEndSuffix[] = "end";
constexpr char companionAppKey[] = "EXTRA_COMPANION_APP=";
}  // namespace

namespace ash {
namespace quick_pair {

CompanionAppParser::CompanionAppParser() = default;

CompanionAppParser::~CompanionAppParser() = default;

void CompanionAppParser::GetAppPackageName(
    scoped_refptr<Device> device,
    base::OnceCallback<void(std::optional<std::string>)>
        on_companion_app_parsed) {
  const auto metadata_id = device->metadata_id();
  FastPairRepository::Get()->GetDeviceMetadata(
      metadata_id,
      base::BindOnce(&CompanionAppParser::OnDeviceMetadataRetrieved,
                     weak_pointer_factory_.GetWeakPtr(), std::move(device),
                     std::move(on_companion_app_parsed)));
}

void CompanionAppParser::OnDeviceMetadataRetrieved(
    scoped_refptr<Device> device,
    base::OnceCallback<void(std::optional<std::string>)> callback,
    DeviceMetadata* device_metadata,
    bool retryable_err) {
  if (!device_metadata)
    return;

  const std::string intent_uri_from_metadata =
      device_metadata->GetDetails().intent_uri();
  std::optional<std::string_view> remainder =
      base::RemovePrefix(intent_uri_from_metadata, kIntentKeyPrefix);
  if (!remainder) {
    std::move(callback).Run(std::nullopt);
    return;
  }
  std::move(callback).Run(GetCompanionAppExtra(std::string(*remainder)));
}

std::optional<std::string> CompanionAppParser::GetCompanionAppExtra(
    const std::string& intent_as_string) {
  // Here is an an example of what Intents look like
  //
  // #Intent;action=android.intent.action.MAIN;
  //         category=android.intent.category.LAUNCHER;
  //         component=package_name/activity;
  //         param1;param2;end
  //
  // They must always begin with "#Intent", have components be separated by ";"
  // and end with "end"
  const std::vector<std::string_view> parts = base::SplitStringPiece(
      intent_as_string, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  if (parts.size() < 2 || parts.front() != kIntentPrefix ||
      parts.back() != kEndSuffix) {
    CD_LOG(WARNING, Feature::FP)
        << "Failed to split intent " << intent_as_string << ".";
    return std::nullopt;
  }

  for (size_t i = 1; i < parts.size() - 1; ++i) {
    const size_t separator = parts[i].find('=');
    if (separator == std::string::npos) {
      if (parts[i].empty()) {
        // Intent should not have empty param. The empty param would appear in
        // intent string as ';;'. In the last case it would cause error in
        // Android framework. Such intents must not appear in the system.
        CD_LOG(WARNING, Feature::FP)
            << "Found empty param in " << intent_as_string << ".";
        return std::nullopt;
      }
      continue;
    }
  }

  // Devices with companion apps always have a key in their intent uri titled:
  // "EXTRA_COMPANION_APP", with the name of that app stored as the value
  size_t companionAppIndex = intent_as_string.find(companionAppKey);
  if (companionAppIndex == std::string::npos) {
    return std::nullopt;
  }

  std::string companionAppId =
      intent_as_string.substr(companionAppIndex + strlen(companionAppKey));
  return companionAppId.substr(0, companionAppId.find(';'));
}

}  // namespace quick_pair
}  // namespace ash