File: link_capturing.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (97 lines) | stat: -rw-r--r-- 3,894 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 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/jobs/link_capturing.h"

#include "base/values.h"
#include "chrome/browser/web_applications/locks/all_apps_lock.h"
#include "chrome/browser/web_applications/proto/web_app.pb.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"

namespace web_app {

void SetAppCapturesSupportedLinksDisableOverlapping(
    const webapps::AppId& app_id,
    bool set_to_preferred,
    AllAppsLock& lock,
    base::Value::Dict& debug_value) {
  debug_value.Set("app_id", app_id);
  debug_value.Set("set_to_preferred", set_to_preferred);

  if (!lock.registrar().IsInstallState(
          app_id, {proto::INSTALLED_WITHOUT_OS_INTEGRATION,
                   proto::INSTALLED_WITH_OS_INTEGRATION})) {
    debug_value.Set("result", "App not installed.");
    return;
  }

  // When disabling, simply disable & exit because this doesn't need to affect
  // the state of other apps.
  if (!set_to_preferred) {
    {
      ScopedRegistryUpdate update = lock.sync_bridge().BeginUpdate();
      WebApp* app_to_update = update->UpdateApp(app_id);
      app_to_update->SetLinkCapturingUserPreference(
          proto::NAVIGATION_CAPTURING_PREFERENCE_DO_NOT_CAPTURE);
    }
    debug_value.Set("app_updated", true);
    // TODO(b/273830801): Automatically call observers when changes are
    // committed to the web_app DB (here and below).
    lock.registrar().NotifyWebAppUserLinkCapturingPreferencesChanged(
        app_id, set_to_preferred);
    return;
  }

  // Whe enabling, any app with the same scope (overlapping) that is explicitly
  // enabled must be disabled to prevent multiple apps from capturing links on
  // the same scope.
  for (const webapps::AppId& other_app_id : lock.registrar().GetAppIds()) {
    if (other_app_id == app_id) {
      {
        ScopedRegistryUpdate update = lock.sync_bridge().BeginUpdate();
        WebApp* app_to_update = update->UpdateApp(app_id);
        app_to_update->SetLinkCapturingUserPreference(
            set_to_preferred
                ? proto::NAVIGATION_CAPTURING_PREFERENCE_CAPTURE
                : proto::NAVIGATION_CAPTURING_PREFERENCE_DO_NOT_CAPTURE);
      }
      debug_value.Set("app_updated", true);
      lock.registrar().NotifyWebAppUserLinkCapturingPreferencesChanged(
          app_id, set_to_preferred);
      continue;
    }
    if (!lock.registrar().CanCaptureLinksInScope(other_app_id)) {
      continue;
    }
    if (!lock.registrar().AppScopesMatchForUserLinkCapturing(app_id,
                                                             other_app_id)) {
      continue;
    }
    const WebApp* other_app = lock.registrar().GetAppById(other_app_id);
    CHECK(other_app);
    // Only update apps that are explicitly turned on. We want to leave any set
    // as 'default' to stay as such, allowing them to take over if this one is
    // uninstalled.
    if (other_app->user_link_capturing_preference() !=
        proto::NAVIGATION_CAPTURING_PREFERENCE_CAPTURE) {
      continue;
    }
    {
      ScopedRegistryUpdate update = lock.sync_bridge().BeginUpdate();
      WebApp* app_to_update = update->UpdateApp(other_app_id);
      app_to_update->SetLinkCapturingUserPreference(
          proto::NAVIGATION_CAPTURING_PREFERENCE_DO_NOT_CAPTURE);
    }
    debug_value.EnsureList("capturing_apps_disabled")->Append(other_app_id);
    lock.registrar().NotifyWebAppUserLinkCapturingPreferencesChanged(
        other_app_id, /*is_preferred=*/false);
  }
  return;
}

}  // namespace web_app