File: data_sharing_navigation_throttle.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 (139 lines) | stat: -rw-r--r-- 4,822 bytes parent folder | download | duplicates (3)
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
// 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 "chrome/browser/data_sharing/data_sharing_navigation_throttle.h"

#include "chrome/browser/collaboration/collaboration_service_factory.h"
#include "chrome/browser/data_sharing/data_sharing_navigation_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "components/data_sharing/public/data_sharing_utils.h"
#include "components/data_sharing/public/features.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"

namespace data_sharing {

namespace {
bool ShouldHandleShareURLNavigation(
    content::NavigationHandle* navigation_handle) {
  // Make sure to keep it in sync between platforms.
  // LINT.IfChange(ShouldHandleShareURLNavigation)
  if (!navigation_handle->IsInMainFrame()) {
    return false;
  }

  // If this is a session or tab restore, don't intercept the
  // navigation to avoid showing the dialog on each browser
  // start.
  if (navigation_handle->GetRestoreType() == content::RestoreType::kRestored) {
    return false;
  }

  if (navigation_handle->IsRendererInitiated()) {
    if (navigation_handle->HasUserGesture()) {
      return true;
    }

    if (DataSharingNavigationUtils::GetInstance()->IsLastUserInteractionExpired(
            navigation_handle->GetWebContents())) {
      return false;
    }

    // Only allow redirect if the user interaction has not expired.
    if (navigation_handle->GetRedirectChain().size() <= 1) {
      return false;
    }
  }

  return true;
  // LINT.ThenChange(/ios/chrome/browser/collaboration/model/data_sharing_tab_helper.mm:ShouldHandleShareURLNavigation)
}
}  // namespace

// static
void DataSharingNavigationThrottle::MaybeCreateAndAdd(
    content::NavigationThrottleRegistry& registry) {
  if (features::IsDataSharingFunctionalityEnabled() &&
      features::ShouldInterceptUrlForVersioning()) {
    registry.AddThrottle(
        std::make_unique<DataSharingNavigationThrottle>(registry));
  }
}

DataSharingNavigationThrottle::DataSharingNavigationThrottle(
    content::NavigationThrottleRegistry& registry)
    : content::NavigationThrottle(registry) {}

DataSharingNavigationThrottle::ThrottleCheckResult
DataSharingNavigationThrottle::WillStartRequest() {
  return CheckIfShouldIntercept();
}

DataSharingNavigationThrottle::ThrottleCheckResult
DataSharingNavigationThrottle::WillRedirectRequest() {
  return CheckIfShouldIntercept();
}

const char* DataSharingNavigationThrottle::GetNameForLogging() {
  return "DataSharingNavigationThrottle";
}

void DataSharingNavigationThrottle::SetServiceForTesting(
    collaboration::CollaborationService* test_service) {
  test_service_ = test_service;
}

DataSharingNavigationThrottle::ThrottleCheckResult
DataSharingNavigationThrottle::CheckIfShouldIntercept() {
  content::WebContents* web_contents = navigation_handle()->GetWebContents();
  if (!web_contents) {
    return PROCEED;
  }

  collaboration::CollaborationService* collaboration_service =
      collaboration::CollaborationServiceFactory::GetForProfile(
          Profile::FromBrowserContext(
              navigation_handle()->GetWebContents()->GetBrowserContext()));

  if (test_service_) {
    collaboration_service = test_service_;
  }

  const GURL& url = navigation_handle()->GetURL();
  if (collaboration_service &&
      DataSharingUtils::ShouldInterceptNavigationForShareURL(url)) {
    if (ShouldHandleShareURLNavigation(navigation_handle())) {
      collaboration_service->HandleShareURLNavigationIntercepted(
          url, /* context = */ nullptr,
          collaboration::GetEntryPointFromPageTransition(
              navigation_handle()->GetPageTransition()));
    }

    // crbug.com/411646000: Only enable this for Android because on Desktop if
    // user clicks an invite link to launch the browser, the browser will quit
    // when the current tab is closed due to no tab remains.
#if BUILDFLAG(IS_ANDROID)
    // Close the tab if the url interception ends with an empty page.
    const GURL& last_committed_url =
        navigation_handle()->GetWebContents()->GetLastCommittedURL();
    if (!last_committed_url.is_valid() || last_committed_url.IsAboutBlank() ||
        last_committed_url.is_empty()) {
      navigation_handle()->GetWebContents()->ClosePage();
    }
#endif  // BUILDFLAG(IS_ANDROID)

    return CANCEL;
  }

  // Update interaction time to handle the case of client redirect.
  if (navigation_handle()->IsInMainFrame() &&
      (!navigation_handle()->IsRendererInitiated() ||
       navigation_handle()->HasUserGesture())) {
    DataSharingNavigationUtils::GetInstance()->UpdateLastUserInteractionTime(
        web_contents);
  }
  return PROCEED;
}

}  // namespace data_sharing