File: protocol_handling_sub_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (163 lines) | stat: -rw-r--r-- 6,277 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
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
// 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 "chrome/browser/web_applications/os_integration/protocol_handling_sub_manager.h"

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/web_applications/os_integration/os_integration_sub_manager.h"
#include "chrome/browser/web_applications/os_integration/os_integration_test_override.h"
#include "chrome/browser/web_applications/os_integration/web_app_protocol_handler_registration.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "components/services/app_service/public/cpp/protocol_handler_info.h"
#include "url/gurl.h"

namespace web_app {

namespace {

std::vector<apps::ProtocolHandlerInfo> GetApprovedProtocolHandlers(
    const proto::os_state::WebAppOsIntegration& state) {
  std::vector<apps::ProtocolHandlerInfo> protocol_handlers_info;
  for (const auto& proto_data : state.protocols_handled().protocols()) {
    apps::ProtocolHandlerInfo info;
    info.protocol = proto_data.protocol();
    info.url = GURL(proto_data.url());
    protocol_handlers_info.push_back(info);
  }
  return protocol_handlers_info;
}

void RecordProtocolHandlingResult(const std::string histogram_name,
                                  Result result) {
  base::UmaHistogramBoolean(histogram_name, (result == Result::kOk));
}

}  // namespace

ProtocolHandlingSubManager::ProtocolHandlingSubManager(
    const base::FilePath& profile_path,
    WebAppProvider& provider)
    : profile_path_(profile_path), provider_(provider) {}

ProtocolHandlingSubManager::~ProtocolHandlingSubManager() = default;

void ProtocolHandlingSubManager::Configure(
    const webapps::AppId& app_id,
    proto::os_state::WebAppOsIntegration& desired_state,
    base::OnceClosure configure_done) {
  DCHECK(!desired_state.has_protocols_handled());
  if (provider_->registrar_unsafe().GetInstallState(app_id) !=
      proto::INSTALLED_WITH_OS_INTEGRATION) {
    std::move(configure_done).Run();
    return;
  }

  const WebApp* web_app = provider_->registrar_unsafe().GetAppById(app_id);
  DCHECK(web_app);

  for (const auto& protocol_handler : web_app->protocol_handlers()) {
    if (base::Contains(web_app->disallowed_launch_protocols(),
                       protocol_handler.protocol)) {
      continue;
    }

    proto::os_state::ProtocolsHandled::Protocol* protocol =
        desired_state.mutable_protocols_handled()->add_protocols();
    protocol->set_protocol(protocol_handler.protocol);
    protocol->set_url(protocol_handler.url.spec());
  }
  std::move(configure_done).Run();
}

void ProtocolHandlingSubManager::Execute(
    const webapps::AppId& app_id,
    const std::optional<SynchronizeOsOptions>& synchronize_options,
    const proto::os_state::WebAppOsIntegration& desired_state,
    const proto::os_state::WebAppOsIntegration& current_state,
    base::OnceClosure callback) {
  // No-op if both the desired and current states are empty.
  if (!desired_state.has_protocols_handled() &&
      !current_state.has_protocols_handled()) {
    std::move(callback).Run();
    return;
  }

  CHECK_OS_INTEGRATION_ALLOWED();

  // Handle unregistration case.
  if (current_state.has_protocols_handled() &&
      !desired_state.has_protocols_handled()) {
    UnregisterProtocolHandlersWithOs(
        app_id, profile_path_,
        base::BindOnce(&RecordProtocolHandlingResult,
                       "WebApp.ProtocolHandlers.Unregistration.Result")
            .Then(std::move(callback)));
    return;
  }

  // Handle registration case.
  if (!current_state.has_protocols_handled() &&
      desired_state.has_protocols_handled()) {
    RegisterProtocolHandlersWithOs(
        app_id, provider_->registrar_unsafe().GetAppShortName(app_id),
        profile_path_, GetApprovedProtocolHandlers(desired_state),
        base::BindOnce(&RecordProtocolHandlingResult,
                       "WebApp.ProtocolHandlers.Registration.Result")
            .Then(std::move(callback)));
    return;
  }

  // If an update is required, then both states should have protocol handling
  // information.
  DCHECK(desired_state.has_protocols_handled());
  DCHECK(current_state.has_protocols_handled());

  // Protocol Handling Diff detection.
  std::string desired_protocols_handled =
      desired_state.protocols_handled().SerializeAsString();
  std::string current_protocols_handled =
      current_state.protocols_handled().SerializeAsString();
  if (desired_protocols_handled == current_protocols_handled) {
    std::move(callback).Run();
    return;
  }

  // If either a registration or unregistration is not done, then an update
  // needs to happen.
  auto register_and_complete =
      base::BindOnce(&RegisterProtocolHandlersWithOs, app_id,
                     provider_->registrar_unsafe().GetAppShortName(app_id),
                     profile_path_, GetApprovedProtocolHandlers(desired_state),
                     base::BindOnce(&RecordProtocolHandlingResult,
                                    "WebApp.ProtocolHandlers.Update.Result")
                         .Then(std::move(callback)));
  UnregisterProtocolHandlersWithOs(
      app_id, profile_path_,
      base::IgnoreArgs<Result>(std::move(register_and_complete)));
}

void ProtocolHandlingSubManager::ForceUnregister(const webapps::AppId& app_id,
                                                 base::OnceClosure callback) {
  UnregisterProtocolHandlersWithOs(
      app_id, profile_path_,
      base::BindOnce(&RecordProtocolHandlingResult,
                     "WebApp.ProtocolHandlers.Unregistration.Result")
          .Then(std::move(callback)));
}

}  // namespace web_app