File: resource_request_allowed_notifier.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 (153 lines) | stat: -rw-r--r-- 5,236 bytes parent folder | download | duplicates (11)
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
// Copyright 2013 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/web_resource/resource_request_allowed_notifier.h"

#include "base/command_line.h"
#include "base/functional/bind.h"

namespace web_resource {

ResourceRequestAllowedNotifier::ResourceRequestAllowedNotifier(
    PrefService* local_state,
    const char* disable_network_switch,
    NetworkConnectionTrackerGetter network_connection_tracker_getter)
    : disable_network_switch_(disable_network_switch),
      local_state_(local_state),
      observer_requested_permission_(false),
      waiting_for_user_to_accept_eula_(false),
      observer_(nullptr),
      network_connection_tracker_getter_(
          std::move(network_connection_tracker_getter)) {}

ResourceRequestAllowedNotifier::~ResourceRequestAllowedNotifier() {
  if (observer_)
    network_connection_tracker_->RemoveNetworkConnectionObserver(this);
}

void ResourceRequestAllowedNotifier::Init(Observer* observer, bool leaky) {
  Init(observer, leaky, /*wait_for_eula=*/true);
}

void ResourceRequestAllowedNotifier::Init(Observer* observer,
                                          bool leaky,
                                          bool wait_for_eula) {
  DCHECK(!observer_);
  DCHECK(observer);
  observer_ = observer;

  DCHECK(network_connection_tracker_getter_);
  network_connection_tracker_ =
      std::move(network_connection_tracker_getter_).Run();

  if (leaky)
    network_connection_tracker_->AddLeakyNetworkConnectionObserver(this);
  else
    network_connection_tracker_->AddNetworkConnectionObserver(this);
  if (network_connection_tracker_->GetConnectionType(
          &connection_type_,
          base::BindOnce(&ResourceRequestAllowedNotifier::SetConnectionType,
                         weak_factory_.GetWeakPtr()))) {
    connection_initialized_ = true;
  }

  if (wait_for_eula) {
    eula_notifier_.reset(CreateEulaNotifier());
    if (eula_notifier_) {
      eula_notifier_->Init(this);
      waiting_for_user_to_accept_eula_ = !eula_notifier_->IsEulaAccepted();
    }
  }
}

ResourceRequestAllowedNotifier::State
ResourceRequestAllowedNotifier::GetResourceRequestsAllowedState() {
  if (disable_network_switch_ &&
      base::CommandLine::ForCurrentProcess()->HasSwitch(
          disable_network_switch_)) {
    return DISALLOWED_COMMAND_LINE_DISABLED;
  }

  // The observer requested permission. Return the current criteria state and
  // set a flag to remind this class to notify the observer once the criteria
  // is met.
  observer_requested_permission_ =
      waiting_for_user_to_accept_eula_ || IsOffline();
  if (!observer_requested_permission_)
    return ALLOWED;
  if (waiting_for_user_to_accept_eula_)
    return DISALLOWED_EULA_NOT_ACCEPTED;
  if (!connection_initialized_)
    return DISALLOWED_NETWORK_STATE_NOT_INITIALIZED;
  return DISALLOWED_NETWORK_DOWN;
}

bool ResourceRequestAllowedNotifier::IsOffline() {
  return !connection_initialized_ ||
         connection_type_ == network::mojom::ConnectionType::CONNECTION_NONE;
}

bool ResourceRequestAllowedNotifier::ResourceRequestsAllowed() {
  return GetResourceRequestsAllowedState() == ALLOWED;
}

void ResourceRequestAllowedNotifier::SetWaitingForEulaForTesting(bool waiting) {
  waiting_for_user_to_accept_eula_ = waiting;
}

void ResourceRequestAllowedNotifier::SetObserverRequestedForTesting(
    bool requested) {
  observer_requested_permission_ = requested;
}

void ResourceRequestAllowedNotifier::SetConnectionTypeForTesting(
    network::mojom::ConnectionType type) {
  SetConnectionType(type);
}

void ResourceRequestAllowedNotifier::MaybeNotifyObserver() {
  // Need to ensure that all criteria are met before notifying observers.
  if (observer_requested_permission_ && ResourceRequestsAllowed()) {
    DVLOG(1) << "Notifying observer of state change.";
    observer_->OnResourceRequestsAllowed();
    // Reset this so the observer is not informed again unless they check
    // ResourceRequestsAllowed again.
    observer_requested_permission_ = false;
  }
}

EulaAcceptedNotifier* ResourceRequestAllowedNotifier::CreateEulaNotifier() {
  return EulaAcceptedNotifier::Create(local_state_);
}

void ResourceRequestAllowedNotifier::OnEulaAccepted() {
  // This flag should have been set if this was waiting on the EULA
  // notification.
  DCHECK(waiting_for_user_to_accept_eula_);
  DVLOG(1) << "EULA was accepted.";
  waiting_for_user_to_accept_eula_ = false;
  MaybeNotifyObserver();
}

void ResourceRequestAllowedNotifier::OnConnectionChanged(
    network::mojom::ConnectionType type) {
  SetConnectionType(type);
  if (type != network::mojom::ConnectionType::CONNECTION_NONE) {
    DVLOG(1) << "Network came online.";
    // MaybeNotifyObserver() internally guarantees that it will only notify the
    // observer if it's currently waiting for the network to come online.
    MaybeNotifyObserver();
  }
}

void ResourceRequestAllowedNotifier::SetConnectionType(
    network::mojom::ConnectionType connection_type) {
  connection_type_ = connection_type;
  if (!connection_initialized_) {
    connection_initialized_ = true;
    MaybeNotifyObserver();
  }
}

}  // namespace web_resource