File: nearby_share_delegate_impl.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 (281 lines) | stat: -rw-r--r-- 9,617 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2020 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/nearby_sharing/nearby_share_delegate_impl.h"

#include "ash/public/cpp/nearby_share_controller.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_features.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_resource_getter.h"
#include "chrome/browser/nearby_sharing/nearby_share_settings.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/session/session_util.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/gfx/vector_icon_types.h"

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/browser/nearby_sharing/internal/icons/vector_icons.h"
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

namespace {

const char kStartOnboardingQueryParam[] = "onboarding";
const char kStartReceivingQueryParam[] = "receive";

constexpr base::TimeDelta kShutoffTimeoutLegacy = base::Minutes(5);
constexpr base::TimeDelta kShutoffTimeout = base::Minutes(10);

std::string GetTimestampString() {
  return base::NumberToString(
      base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
}

}  // namespace

NearbyShareDelegateImpl::NearbyShareDelegateImpl(
    ash::NearbyShareController* nearby_share_controller)
    : nearby_share_controller_(nearby_share_controller),
      settings_opener_(std::make_unique<SettingsOpener>()),
      shutoff_timer_(
          FROM_HERE,
          chromeos::features::IsQuickShareV2Enabled() ? kShutoffTimeout
                                                      : kShutoffTimeoutLegacy,
          base::BindRepeating(&NearbyShareDelegateImpl::DisableHighVisibility,
                              base::Unretained(this))) {
  ash::SessionController::Get()->AddObserver(this);
}

NearbyShareDelegateImpl::~NearbyShareDelegateImpl() {
  ash::SessionController::Get()->RemoveObserver(this);
  if (nearby_share_service_) {
    RemoveNearbyShareServiceObservers();
  }
  nearby_share_settings_receiver_.reset();
}

bool NearbyShareDelegateImpl::IsEnabled() {
  return nearby_share_service_ != nullptr &&
         nearby_share_service_->GetSettings()->GetEnabled();
}

void NearbyShareDelegateImpl::SetEnabled(bool enabled) {
  CHECK(nearby_share_service_);
  nearby_share_service_->GetSettings()->SetEnabled(enabled);
}

bool NearbyShareDelegateImpl::IsPodButtonVisible() {
  return nearby_share_service_ != nullptr &&
         !nearby_share_service_->GetSettings()->IsDisabledByPolicy();
}

bool NearbyShareDelegateImpl::IsHighVisibilityOn() {
  return nearby_share_service_ && nearby_share_service_->IsInHighVisibility();
}

bool NearbyShareDelegateImpl::IsOnboardingComplete() {
  DCHECK(nearby_share_settings_);
  return nearby_share_settings_->IsOnboardingComplete();
}

bool NearbyShareDelegateImpl::IsEnableHighVisibilityRequestActive() const {
  return is_enable_high_visibility_request_active_;
}

base::TimeTicks NearbyShareDelegateImpl::HighVisibilityShutoffTime() const {
  return shutoff_time_;
}

void NearbyShareDelegateImpl::EnableHighVisibility() {
  if (!nearby_share_service_)
    return;

  settings_opener_->ShowSettingsPage(kStartReceivingQueryParam);

  is_enable_high_visibility_request_active_ = true;
}

void NearbyShareDelegateImpl::DisableHighVisibility() {
  if (!nearby_share_service_)
    return;

  shutoff_timer_.Stop();

  nearby_share_service_->ClearForegroundReceiveSurfaces();
}

void NearbyShareDelegateImpl::OnLockStateChanged(bool locked) {
  if (locked && IsHighVisibilityOn()) {
    DisableHighVisibility();
  }
}

void NearbyShareDelegateImpl::OnFirstSessionReady() {
  nearby_share_service_ = NearbySharingServiceFactory::GetForBrowserContext(
      ProfileManager::GetPrimaryUserProfile());

  if (nearby_share_service_) {
    nearby_share_settings_ = nearby_share_service_->GetSettings();
    AddNearbyShareServiceObservers();
  }
}

void NearbyShareDelegateImpl::SetNearbyShareServiceForTest(
    NearbySharingService* service) {
  nearby_share_service_ = service;
  AddNearbyShareServiceObservers();
}

void NearbyShareDelegateImpl::SetNearbyShareSettingsForTest(
    NearbyShareSettings* settings) {
  nearby_share_settings_ = settings;
}

// In Quick Share v1, not used.
void NearbyShareDelegateImpl::OnEnabledChanged(bool enabled) {
  nearby_share_controller_->NearbyShareEnabledChanged(enabled);
}

void NearbyShareDelegateImpl::OnFastInitiationNotificationStateChanged(
    ::nearby_share::mojom::FastInitiationNotificationState state) {}

void NearbyShareDelegateImpl::OnIsFastInitiationHardwareSupportedChanged(
    bool is_supported) {}

void NearbyShareDelegateImpl::OnDeviceNameChanged(
    const std::string& device_name) {}

void NearbyShareDelegateImpl::OnDataUsageChanged(
    ::nearby_share::mojom::DataUsage data_usage) {}

void NearbyShareDelegateImpl::OnVisibilityChanged(
    ::nearby_share::mojom::Visibility visibility) {
  nearby_share_controller_->VisibilityChanged(visibility);
}

void NearbyShareDelegateImpl::OnAllowedContactsChanged(
    const std::vector<std::string>& visible_contact_ids) {}

void NearbyShareDelegateImpl::OnIsOnboardingCompleteChanged(bool is_complete) {}

void NearbyShareDelegateImpl::AddNearbyShareServiceObservers() {
  DCHECK(nearby_share_service_);
  DCHECK(!nearby_share_service_->HasObserver(this));
  nearby_share_service_->AddObserver(this);
  if (nearby_share_settings_) {
    nearby_share_settings_->AddSettingsObserver(
        nearby_share_settings_receiver_.BindNewPipeAndPassRemote());
  }
}

void NearbyShareDelegateImpl::RemoveNearbyShareServiceObservers() {
  DCHECK(nearby_share_service_);
  DCHECK(nearby_share_service_->HasObserver(this));
  nearby_share_service_->RemoveObserver(this);
}

void NearbyShareDelegateImpl::OnHighVisibilityChangeRequested() {
  is_enable_high_visibility_request_active_ = true;
}

void NearbyShareDelegateImpl::OnHighVisibilityChanged(bool high_visibility_on) {
  is_enable_high_visibility_request_active_ = false;

  if (high_visibility_on) {
    base::TimeDelta shutoff_timeout =
        chromeos::features::IsQuickShareV2Enabled() ? kShutoffTimeout
                                                    : kShutoffTimeoutLegacy;
    shutoff_time_ = base::TimeTicks::Now() + shutoff_timeout;
    shutoff_timer_.Reset();
  } else {
    shutoff_timer_.Stop();
  }

  nearby_share_controller_->HighVisibilityEnabledChanged(high_visibility_on);
}

void NearbyShareDelegateImpl::OnShutdown() {
  if (nearby_share_service_) {
    RemoveNearbyShareServiceObservers();
    nearby_share_service_ = nullptr;
  }
}

void NearbyShareDelegateImpl::ShowNearbyShareSettings() const {
  DCHECK(nearby_share_service_);

  std::string query_param =
      nearby_share_service_->GetSettings()->IsOnboardingComplete()
          ? std::string()  // Show settings subpage without dialog.
          : kStartOnboardingQueryParam;  // Show onboarding dialog.
  settings_opener_->ShowSettingsPage(query_param);
}

void NearbyShareDelegateImpl::ShowOnboardingPage() const {
  DCHECK(settings_opener_);
  settings_opener_->ShowSettingsPage(kStartOnboardingQueryParam);
}

void NearbyShareDelegateImpl::SettingsOpener::ShowSettingsPage(
    const std::string& sub_page) {
  std::string query_string;
  if (!sub_page.empty()) {
    // Append a timestamp to make the url unique per-call. Otherwise, settings
    // will not respond to successive calls if the url does not change.
    query_string += "?" + sub_page + "&time=" + GetTimestampString();

    if (sub_page == kStartReceivingQueryParam) {
      base::TimeDelta shutoff_timeout =
          chromeos::features::IsQuickShareV2Enabled() ? kShutoffTimeout
                                                      : kShutoffTimeoutLegacy;
      // Attach high visibility shutoff timeout for display in webui.
      query_string +=
          "&timeout=" + base::NumberToString(shutoff_timeout.InSeconds());
    }
  }

  chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
      ProfileManager::GetPrimaryUserProfile(),
      std::string(chromeos::settings::mojom::kNearbyShareSubpagePath) +
          query_string);
}

const gfx::VectorIcon& NearbyShareDelegateImpl::GetIcon(bool on_icon) const {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (features::IsNameEnabled()) {
    return on_icon ? kNearbyShareInternalIcon : kNearbyShareInternalOffIcon;
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  return gfx::VectorIcon::EmptyIcon();
}

std::u16string NearbyShareDelegateImpl::GetPlaceholderFeatureName() const {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (features::IsNameEnabled()) {
    return NearbyShareResourceGetter::GetInstance()->GetFeatureName();
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  return u"";
}

::nearby_share::mojom::Visibility NearbyShareDelegateImpl::GetVisibility()
    const {
  if (!nearby_share_settings_) {
    return ::nearby_share::mojom::Visibility::kUnknown;
  }

  return nearby_share_settings_->GetVisibility();
}

void NearbyShareDelegateImpl::SetVisibility(
    ::nearby_share::mojom::Visibility visibility) {
  DCHECK(nearby_share_settings_);
  return nearby_share_settings_->SetVisibility(visibility);
}