File: sharing_hub_bubble_controller_chromeos_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 (193 lines) | stat: -rw-r--r-- 6,686 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
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
// 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/ui/sharing_hub/sharing_hub_bubble_controller_chromeos_impl.h"

#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/share/share_metrics.h"
#include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "chrome/browser/sharesheet/sharesheet_metrics.h"
#include "chrome/browser/sharesheet/sharesheet_service.h"
#include "chrome/browser/sharesheet/sharesheet_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/services/app_service/public/cpp/intent.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "content/public/browser/web_contents.h"
#include "ui/views/controls/button/button.h"

namespace sharing_hub {

// static
SharingHubBubbleController*
SharingHubBubbleController::CreateOrGetFromWebContents(
    content::WebContents* web_contents) {
  SharingHubBubbleControllerChromeOsImpl::CreateForWebContents(web_contents);
  SharingHubBubbleControllerChromeOsImpl* controller =
      SharingHubBubbleControllerChromeOsImpl::FromWebContents(web_contents);
  return controller;
}

SharingHubBubbleControllerChromeOsImpl::
    ~SharingHubBubbleControllerChromeOsImpl() {
  if (bubble_showing_) {
    bubble_showing_ = false;
    // Close any remnant Sharesheet dialog.
    CloseSharesheet();

    // We must deselect the icon manually since the Sharesheet will not be able
    // to invoke OnSharesheetClosed() at this point.
    DeselectIcon();
  }
}

void SharingHubBubbleControllerChromeOsImpl::HideBubble() {
  if (bubble_showing_) {
    CloseSharesheet();
  }
}

void SharingHubBubbleControllerChromeOsImpl::ShowBubble(
    share::ShareAttempt attempt) {
  Browser* browser = chrome::FindBrowserWithTab(web_contents());

  // Ignore subsequent calls to open the Sharesheet if it already is open. This
  // is especially for the Nearby Share dialog, where clicking outside of it
  // will not dismiss the dialog.
  if (bubble_showing_) {
    return;
  }
  bubble_showing_ = true;
  ShowSharesheet(browser->window()->GetSharingHubIconButton());

  share::LogShareSourceDesktop(share::ShareSourceDesktop::kOmniboxSharingHub);
}

SharingHubBubbleView*
SharingHubBubbleControllerChromeOsImpl::sharing_hub_bubble_view() const {
  return nullptr;
}

bool SharingHubBubbleControllerChromeOsImpl::ShouldOfferOmniboxIcon() {
  return !GetProfile()->IsIncognitoProfile() && !GetProfile()->IsGuestSession();
}

Profile* SharingHubBubbleControllerChromeOsImpl::GetProfile() const {
  return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
}

void SharingHubBubbleControllerChromeOsImpl::OnVisibilityChanged(
    content::Visibility visibility) {
  // Cancel the current share attempt if the user switches to a different tab in
  // the window. Switching windows is permitted since a Sharesheet is tied to
  // the native window.
  if (bubble_showing_ && visibility == content::Visibility::HIDDEN) {
    CloseSharesheet();
  }
}

void SharingHubBubbleControllerChromeOsImpl::ShowSharesheet(
    views::Button* highlighted_button) {
  DCHECK(highlighted_button);
  highlighted_button_tracker_.SetView(highlighted_button);

  ShowSharesheetAsh();

  // Save the window in order to close the Sharesheet if the tab is closed. This
  // will return the incorrect window if called later.
  parent_window_ = GetWebContents().GetTopLevelNativeWindow();
  parent_window_tracker_ = views::NativeWindowTracker::Create(parent_window_);
}

void SharingHubBubbleControllerChromeOsImpl::CloseSharesheet() {
  if (parent_window_ && !parent_window_tracker_->WasNativeWindowDestroyed()) {
    CloseSharesheetAsh();
  }

  // OnSharesheetClosed() is not guaranteed to be called by the
  // SharesheetController (specifically for the case where this is invoked by
  // our destructor). Hence, we must explicitly set this null here.
  parent_window_ = nullptr;
  parent_window_tracker_ = nullptr;
}

sharesheet::SharesheetService*
SharingHubBubbleControllerChromeOsImpl::GetSharesheetService() {
  Profile* const profile =
      Profile::FromBrowserContext(GetWebContents().GetBrowserContext());
  if (!profile) {
    return nullptr;
  }

  return sharesheet::SharesheetServiceFactory::GetForProfile(profile);
}

void SharingHubBubbleControllerChromeOsImpl::ShowSharesheetAsh() {
  sharesheet::SharesheetService* sharesheet_service = GetSharesheetService();
  if (!sharesheet_service) {
    return;
  }

  apps::IntentPtr intent = apps_util::MakeShareIntent(
      GetWebContents().GetLastCommittedURL().spec(),
      base::UTF16ToUTF8(GetWebContents().GetTitle()));
  sharesheet_service->ShowBubble(
      &GetWebContents(), std::move(intent),
      sharesheet::LaunchSource::kOmniboxShare,
      base::BindOnce([](sharesheet::SharesheetResult result) {}),
      base::BindOnce(
          &SharingHubBubbleControllerChromeOsImpl::OnSharesheetClosed,
          weak_ptr_factory_.GetWeakPtr()));
}

void SharingHubBubbleControllerChromeOsImpl::CloseSharesheetAsh() {
  sharesheet::SharesheetService* sharesheet_service = GetSharesheetService();
  if (!sharesheet_service) {
    return;
  }

  sharesheet::SharesheetController* sharesheet_controller =
      sharesheet_service->GetSharesheetController(parent_window_);
  if (!sharesheet_controller) {
    return;
  }

  sharesheet_controller->CloseBubble(sharesheet::SharesheetResult::kCancel);
}

void SharingHubBubbleControllerChromeOsImpl::OnSharesheetClosed(
    views::Widget::ClosedReason reason) {
  bubble_showing_ = false;
  parent_window_ = nullptr;
  parent_window_tracker_ = nullptr;
  // Deselect the omnibox icon now that the Sharesheet is closed.
  DeselectIcon();
}

void SharingHubBubbleControllerChromeOsImpl::DeselectIcon() {
  if (!highlighted_button_tracker_.view()) {
    return;
  }

  views::Button* button =
      views::Button::AsButton(highlighted_button_tracker_.view());
  if (button) {
    button->SetHighlighted(false);
  }
}

SharingHubBubbleControllerChromeOsImpl::SharingHubBubbleControllerChromeOsImpl(
    content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      content::WebContentsUserData<SharingHubBubbleControllerChromeOsImpl>(
          *web_contents) {}

WEB_CONTENTS_USER_DATA_KEY_IMPL(SharingHubBubbleControllerChromeOsImpl);

}  // namespace sharing_hub