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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
// 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/sharesheet/sharesheet_service.h"
#include <algorithm>
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/sharesheet/share_action/share_action.h"
#include "chrome/browser/sharesheet/share_action/share_action_cache.h"
#include "chrome/browser/sharesheet/sharesheet_controller.h"
#include "chrome/browser/sharesheet/sharesheet_service_delegator.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "chrome/grit/generated_resources.h"
#include "components/drive/drive_api_util.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/icon_effects.h"
#include "components/services/app_service/public/cpp/intent.h"
#include "content/public/browser/web_contents.h"
#include "ui/display/types/display_constants.h"
#include "ui/views/view.h"
namespace sharesheet {
namespace {
std::u16string& GetSelectedApp() {
static base::NoDestructor<std::u16string> selected_app;
return *selected_app;
}
gfx::NativeWindow GetNativeWindowFromWebContents(
base::WeakPtr<content::WebContents> web_contents) {
if (!web_contents) {
return nullptr;
}
return web_contents->GetTopLevelNativeWindow();
}
bool HasHostedDocument(const apps::Intent& intent) {
return std::ranges::any_of(
intent.files, [](const apps::IntentFilePtr& file) {
return drive::util::HasHostedDocumentExtension(
base::FilePath(file->url.ExtractFileName()));
});
}
} // namespace
SharesheetService::SharesheetService(Profile* profile)
: profile_(profile),
share_action_cache_(std::make_unique<ShareActionCache>(profile_)),
app_service_proxy_(
apps::AppServiceProxyFactory::GetForProfile(profile_)) {}
SharesheetService::~SharesheetService() = default;
void SharesheetService::ShowBubble(content::WebContents* web_contents,
apps::IntentPtr intent,
LaunchSource source,
DeliveredCallback delivered_callback,
CloseCallback close_callback) {
ShowBubble(std::move(intent), source,
base::BindOnce(&GetNativeWindowFromWebContents,
web_contents->GetWeakPtr()),
std::move(delivered_callback), std::move(close_callback));
}
void SharesheetService::ShowBubble(
apps::IntentPtr intent,
LaunchSource source,
GetNativeWindowCallback get_native_window_callback,
DeliveredCallback delivered_callback,
CloseCallback close_callback) {
DCHECK(intent);
DCHECK(intent->IsShareIntent());
CHECK(delivered_callback);
SharesheetMetrics::RecordSharesheetLaunchSource(source);
PrepareToShowBubble(std::move(intent), std::move(get_native_window_callback),
std::move(delivered_callback), std::move(close_callback));
}
SharesheetController* SharesheetService::GetSharesheetController(
gfx::NativeWindow native_window) {
SharesheetServiceDelegator* delegator = GetDelegator(native_window);
if (!delegator)
return nullptr;
return delegator->GetSharesheetController();
}
#if BUILDFLAG(IS_CHROMEOS)
void SharesheetService::ShowNearbyShareBubbleForArc(
gfx::NativeWindow native_window,
apps::IntentPtr intent,
LaunchSource source,
DeliveredCallback delivered_callback,
CloseCallback close_callback,
ActionCleanupCallback cleanup_callback) {
DCHECK(intent);
DCHECK(intent->IsShareIntent());
ShareAction* share_action =
share_action_cache_->GetActionFromType(ShareActionType::kNearbyShare);
if (!share_action || !share_action->ShouldShowAction(
intent, false /*contains_google_document=*/)) {
std::move(delivered_callback).Run(SharesheetResult::kCancel);
return;
}
share_action->SetActionCleanupCallbackForArc(std::move(cleanup_callback));
SharesheetMetrics::RecordSharesheetLaunchSource(source);
if (!native_window) {
std::move(delivered_callback).Run(SharesheetResult::kErrorWindowClosed);
return;
}
auto* sharesheet_service_delegator = GetOrCreateDelegator(native_window);
sharesheet_service_delegator->ShowNearbyShareBubbleForArc(
std::move(intent), std::move(delivered_callback),
std::move(close_callback));
}
#endif // BUILDFLAG(IS_CHROMEOS)
// Cleanup delegator when bubble closes.
void SharesheetService::OnBubbleClosed(
gfx::NativeWindow native_window,
const std::optional<ShareActionType>& share_action_type) {
auto iter = active_delegators_.begin();
while (iter != active_delegators_.end()) {
if ((*iter)->GetNativeWindow() == native_window) {
if (share_action_type) {
ShareAction* share_action =
share_action_cache_->GetActionFromType(share_action_type.value());
if (share_action != nullptr)
share_action->OnClosing(iter->get()->GetSharesheetController());
}
active_delegators_.erase(iter);
break;
}
++iter;
}
}
void SharesheetService::OnTargetSelected(
gfx::NativeWindow native_window,
const TargetType type,
const std::optional<ShareActionType>& share_action_type,
const std::optional<std::u16string>& app_name,
apps::IntentPtr intent,
views::View* share_action_view) {
SharesheetServiceDelegator* delegator = GetDelegator(native_window);
if (!delegator)
return;
RecordUserActionMetrics(share_action_type, app_name);
if (type == TargetType::kAction) {
CHECK(share_action_type.has_value());
ShareAction* share_action =
share_action_cache_->GetActionFromType(share_action_type.value());
if (share_action == nullptr)
return;
bool has_action_view = share_action->HasActionView();
delegator->OnActionLaunched(has_action_view);
share_action->LaunchAction(delegator->GetSharesheetController(),
(has_action_view ? share_action_view : nullptr),
std::move(intent));
} else if (type == TargetType::kArcApp || type == TargetType::kWebApp) {
CHECK(intent);
CHECK(app_name.has_value());
LaunchApp(app_name.value(), std::move(intent));
delegator->CloseBubble(SharesheetResult::kSuccess);
}
}
bool SharesheetService::OnAcceleratorPressed(
const ui::Accelerator& accelerator,
const ShareActionType share_action_type) {
ShareAction* share_action =
share_action_cache_->GetActionFromType(share_action_type);
DCHECK(share_action);
return share_action == nullptr
? false
: share_action->OnAcceleratorPressed(accelerator);
}
bool SharesheetService::HasShareTargets(const apps::IntentPtr& intent) {
bool contains_hosted_document = HasHostedDocument(*intent);
std::vector<apps::IntentLaunchInfo> intent_launch_info =
app_service_proxy_->GetAppsForIntent(intent);
return share_action_cache_->HasVisibleActions(intent,
contains_hosted_document) ||
(!contains_hosted_document && !intent_launch_info.empty());
}
Profile* SharesheetService::GetProfile() {
return profile_;
}
const gfx::VectorIcon* SharesheetService::GetVectorIcon(
const std::optional<ShareActionType>& share_action_type) {
if (!share_action_type.has_value()) {
return nullptr;
}
return share_action_cache_->GetVectorIconFromType(share_action_type.value());
}
void SharesheetService::ShowBubbleForTesting(
gfx::NativeWindow native_window,
apps::IntentPtr intent,
LaunchSource source,
DeliveredCallback delivered_callback,
CloseCallback close_callback,
int num_actions_to_add) {
CHECK(views::Widget::GetWidgetForNativeWindow(native_window));
SharesheetMetrics::RecordSharesheetLaunchSource(source);
for (int i = 0; i < num_actions_to_add; ++i) {
share_action_cache_->AddShareActionForTesting(); // IN-TEST
}
auto targets = GetActionsForIntent(intent);
OnReadyToShowBubble(native_window, std::move(intent),
std::move(delivered_callback), std::move(close_callback),
std::move(targets));
}
SharesheetUiDelegate* SharesheetService::GetUiDelegateForTesting(
gfx::NativeWindow native_window) {
auto* delegator = GetDelegator(native_window);
return delegator->GetUiDelegateForTesting(); // IN-TEST
}
// static
void SharesheetService::SetSelectedAppForTesting(
const std::u16string& target_name) {
GetSelectedApp() = target_name;
}
void SharesheetService::PrepareToShowBubble(
apps::IntentPtr intent,
GetNativeWindowCallback get_native_window_callback,
DeliveredCallback delivered_callback,
CloseCallback close_callback) {
bool contains_hosted_document = HasHostedDocument(*intent);
auto targets = GetActionsForIntent(intent);
std::vector<apps::IntentLaunchInfo> intent_launch_info =
contains_hosted_document ? std::vector<apps::IntentLaunchInfo>()
: app_service_proxy_->GetAppsForIntent(intent);
SharesheetMetrics::RecordSharesheetAppCount(intent_launch_info.size());
LoadAppIcons(
std::move(intent_launch_info), std::move(targets), 0,
base::BindOnce(&SharesheetService::OnAppIconsLoaded,
weak_factory_.GetWeakPtr(), std::move(intent),
std::move(get_native_window_callback),
std::move(delivered_callback), std::move(close_callback)));
}
std::vector<TargetInfo> SharesheetService::GetActionsForIntent(
const apps::IntentPtr& intent) {
bool contains_hosted_document = HasHostedDocument(*intent);
std::vector<TargetInfo> targets;
auto& actions = share_action_cache_->GetShareActions();
auto iter = actions.begin();
while (iter != actions.end()) {
if ((*iter)->ShouldShowAction(intent, contains_hosted_document)) {
targets.emplace_back(/*type=*/TargetType::kAction, /*icon=*/std::nullopt,
/*launch_name=*/(*iter)->GetActionName(),
/*display_name=*/(*iter)->GetActionName(),
/*share_action_type=*/(*iter)->GetActionType(),
/*secondary_display_name=*/std::nullopt,
/*activity_name=*/std::nullopt,
/*is_dlp_blocked=*/false);
}
++iter;
}
return targets;
}
void SharesheetService::LoadAppIcons(
std::vector<apps::IntentLaunchInfo> intent_launch_info,
std::vector<TargetInfo> targets,
size_t index,
SharesheetServiceIconLoaderCallback callback) {
if (index >= intent_launch_info.size()) {
std::move(callback).Run(std::move(targets));
return;
}
// Making a copy because we move |intent_launch_info| out below.
auto app_id = intent_launch_info[index].app_id;
uint32_t icon_effects = app_service_proxy_->GetIconEffects(app_id);
if (intent_launch_info[index].is_dlp_blocked) {
icon_effects |= apps::IconEffects::kBlocked;
}
app_service_proxy_->LoadIconWithIconEffects(
app_id, icon_effects, apps::IconType::kStandard, kIconSize,
/*allow_placeholder_icon=*/false,
base::BindOnce(&SharesheetService::OnIconLoaded,
weak_factory_.GetWeakPtr(), std::move(intent_launch_info),
std::move(targets), index, std::move(callback)));
}
void SharesheetService::OnIconLoaded(
std::vector<apps::IntentLaunchInfo> intent_launch_info,
std::vector<TargetInfo> targets,
size_t index,
SharesheetServiceIconLoaderCallback callback,
apps::IconValuePtr icon_value) {
const auto& launch_entry = intent_launch_info[index];
const auto& app_type =
app_service_proxy_->AppRegistryCache().GetAppType(launch_entry.app_id);
auto target_type = TargetType::kUnknown;
if (app_type == apps::AppType::kArc) {
target_type = TargetType::kArcApp;
} else if (app_type == apps::AppType::kWeb ||
app_type == apps::AppType::kSystemWeb) {
target_type = TargetType::kWebApp;
}
app_service_proxy_->AppRegistryCache().ForOneApp(
launch_entry.app_id, [&launch_entry, &targets, &icon_value,
&target_type](const apps::AppUpdate& update) {
gfx::ImageSkia image_skia =
(icon_value && icon_value->icon_type == apps::IconType::kStandard)
? icon_value->uncompressed
: gfx::ImageSkia();
targets.emplace_back(
/*type=*/target_type, /*icon=*/image_skia,
/*launch_name=*/base::UTF8ToUTF16(launch_entry.app_id),
/*display_name=*/base::UTF8ToUTF16(update.Name()),
/*share_action_type=*/std::nullopt,
/*secondary_display_name=*/
base::UTF8ToUTF16(launch_entry.activity_label),
/*activity_name=*/launch_entry.activity_name,
/*is_dlp_blocked=*/launch_entry.is_dlp_blocked);
});
LoadAppIcons(std::move(intent_launch_info), std::move(targets), index + 1,
std::move(callback));
}
void SharesheetService::OnAppIconsLoaded(
apps::IntentPtr intent,
GetNativeWindowCallback get_native_window_callback,
DeliveredCallback delivered_callback,
CloseCallback close_callback,
std::vector<TargetInfo> targets) {
gfx::NativeWindow native_window = std::move(get_native_window_callback).Run();
// Note that checking |native_window| is not sufficient: |widget| can be null
// even when |native_window| is 'true': https://crbug.com/1375887#c11
views::Widget* const widget =
views::Widget::GetWidgetForNativeWindow(native_window);
if (!widget) {
LOG(WARNING) << "Window has been closed";
std::move(delivered_callback).Run(SharesheetResult::kErrorWindowClosed);
return;
}
OnReadyToShowBubble(native_window, std::move(intent),
std::move(delivered_callback), std::move(close_callback),
std::move(targets));
}
void SharesheetService::OnReadyToShowBubble(
gfx::NativeWindow native_window,
apps::IntentPtr intent,
DeliveredCallback delivered_callback,
CloseCallback close_callback,
std::vector<TargetInfo> targets) {
auto* delegator = GetOrCreateDelegator(native_window);
RecordTargetCountMetrics(targets);
RecordShareDataMetrics(intent);
// If SetSelectedAppForTesting() has been called, immediately launch the app.
const std::u16string selected_app = GetSelectedApp();
if (!selected_app.empty()) {
SharesheetResult result = SharesheetResult::kCancel;
if (std::ranges::any_of(targets, [selected_app](const auto& target) {
return (target.type == TargetType::kArcApp ||
target.type == TargetType::kWebApp) &&
target.launch_name == selected_app;
})) {
LaunchApp(selected_app, std::move(intent));
result = SharesheetResult::kSuccess;
}
std::move(delivered_callback).Run(result);
delegator->OnBubbleClosed(/*share_action_type=*/std::nullopt);
return;
}
delegator->ShowBubble(std::move(targets), std::move(intent),
std::move(delivered_callback),
std::move(close_callback));
}
void SharesheetService::LaunchApp(const std::u16string& target_name,
apps::IntentPtr intent) {
app_service_proxy_->LaunchAppWithIntent(
base::UTF16ToUTF8(target_name),
apps::GetEventFlags(WindowOpenDisposition::NEW_WINDOW,
/*prefer_container=*/true),
std::move(intent), apps::LaunchSource::kFromSharesheet,
std::make_unique<apps::WindowInfo>(display::kDefaultDisplayId),
base::DoNothing());
}
SharesheetServiceDelegator* SharesheetService::GetOrCreateDelegator(
gfx::NativeWindow native_window) {
auto* delegator = GetDelegator(native_window);
if (delegator == nullptr) {
auto new_delegator =
std::make_unique<SharesheetServiceDelegator>(native_window, this);
delegator = new_delegator.get();
active_delegators_.push_back(std::move(new_delegator));
}
return delegator;
}
SharesheetServiceDelegator* SharesheetService::GetDelegator(
gfx::NativeWindow native_window) {
auto iter = active_delegators_.begin();
while (iter != active_delegators_.end()) {
if ((*iter)->GetNativeWindow() == native_window) {
return iter->get();
}
++iter;
}
return nullptr;
}
void SharesheetService::RecordUserActionMetrics(
const std::optional<ShareActionType>& share_action_type,
const std::optional<std::u16string>& app_name) {
// One of the two optional fields must be set.
CHECK(share_action_type || app_name);
if (share_action_type) {
switch (share_action_type.value()) {
case ShareActionType::kExample:
// This is a test. Do nothing.
return;
case ShareActionType::kNearbyShare:
SharesheetMetrics::RecordSharesheetActionMetrics(
SharesheetMetrics::UserAction::kNearbyAction);
return;
case ShareActionType::kDriveShare:
SharesheetMetrics::RecordSharesheetActionMetrics(
SharesheetMetrics::UserAction::kDriveAction);
return;
case ShareActionType::kCopyToClipboardShare:
SharesheetMetrics::RecordSharesheetActionMetrics(
SharesheetMetrics::UserAction::kCopyAction);
return;
default:
NOTREACHED();
}
}
if (app_name) {
// Should be an app if we reached here.
auto app_type = app_service_proxy_->AppRegistryCache().GetAppType(
base::UTF16ToUTF8(app_name.value()));
switch (app_type) {
case apps::AppType::kArc:
SharesheetMetrics::RecordSharesheetActionMetrics(
SharesheetMetrics::UserAction::kArc);
return;
case apps::AppType::kWeb:
// TODO(crbug.com/40172532): Add a separate metrics for System Web Apps if
// needed.
case apps::AppType::kSystemWeb:
SharesheetMetrics::RecordSharesheetActionMetrics(
SharesheetMetrics::UserAction::kWeb);
return;
case apps::AppType::kCrostini:
case apps::AppType::kChromeApp:
case apps::AppType::kPluginVm:
case apps::AppType::kRemote:
case apps::AppType::kBorealis:
case apps::AppType::kBruschetta:
case apps::AppType::kExtension:
case apps::AppType::kUnknown:
NOTREACHED();
}
}
}
void SharesheetService::RecordTargetCountMetrics(
const std::vector<TargetInfo>& targets) {
int arc_app_count = 0;
int web_app_count = 0;
for (const auto& target : targets) {
switch (target.type) {
case TargetType::kArcApp:
++arc_app_count;
break;
case TargetType::kWebApp:
++web_app_count;
break;
case TargetType::kAction:
break;
case TargetType::kUnknown:
NOTREACHED();
}
}
SharesheetMetrics::RecordSharesheetArcAppCount(arc_app_count);
SharesheetMetrics::RecordSharesheetWebAppCount(web_app_count);
}
void SharesheetService::RecordShareDataMetrics(const apps::IntentPtr& intent) {
// Record file count.
SharesheetMetrics::RecordSharesheetFilesSharedCount(intent->files.size());
}
} // namespace sharesheet
|