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
|
// 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/ash/arc/arc_open_url_delegate_impl.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/containers/fixed_flat_map.h"
#include "base/files/file_path.h"
#include "base/files/safe_base_name.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.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/intent_util.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/apps/apk_web_app_service.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/fileapi/arc_content_file_system_url_util.h"
#include "chrome/browser/ash/arc/intent_helper/custom_tab_session_impl.h"
#include "chrome/browser/ash/browser_delegate/browser_controller.h"
#include "chrome/browser/ash/browser_delegate/browser_delegate.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/fileapi/external_file_url_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/shelf/app_service/app_service_app_window_arc_tracker.h"
#include "chrome/browser/ui/ash/shelf/app_service/app_service_app_window_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/browser/webshare/prepare_directory_task.h"
#include "chrome/common/webui_url_constants.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "chromeos/ash/experiences/arc/intent_helper/arc_intent_helper_bridge.h"
#include "chromeos/ash/experiences/arc/intent_helper/custom_tab.h"
#include "chromeos/ash/experiences/arc/mojom/intent_helper.mojom.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/intent.h"
#include "components/services/app_service/public/cpp/intent_filter_util.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "components/user_manager/user_manager.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/url_util.h"
#include "storage/browser/file_system/file_system_context.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"
#include "url/url_constants.h"
using arc::mojom::ChromePage;
namespace {
ArcOpenUrlDelegateImpl* g_instance = nullptr;
constexpr auto kOSSettingsMap =
base::MakeFixedFlatMap<ChromePage, const char*>({
{ChromePage::ACCOUNTS,
chromeos::settings::mojom::kManageOtherPeopleSubpagePathV2},
{ChromePage::AUDIO, chromeos::settings::mojom::kAudioSubpagePath},
{ChromePage::BLUETOOTH,
chromeos::settings::mojom::kBluetoothDevicesSubpagePath},
{ChromePage::BLUETOOTHDEVICES,
chromeos::settings::mojom::kBluetoothDevicesSubpagePath},
{ChromePage::CUPSPRINTERS,
chromeos::settings::mojom::kPrintingDetailsSubpagePath},
{ChromePage::DATETIME,
chromeos::settings::mojom::kSystemPreferencesSectionPath},
{ChromePage::DISPLAY, chromeos::settings::mojom::kDisplaySubpagePath},
{ChromePage::GRAPHICSTABLET,
chromeos::settings::mojom::kGraphicsTabletSubpagePath},
{ChromePage::HELP,
chromeos::settings::mojom::kAboutChromeOsSectionPath},
{ChromePage::KEYBOARDOVERLAY,
chromeos::settings::mojom::kKeyboardSubpagePath},
{ChromePage::LOCKSCREEN,
chromeos::settings::mojom::kSecurityAndSignInSubpagePathV2},
{ChromePage::MAIN, ""},
{ChromePage::MANAGEACCESSIBILITY,
chromeos::settings::mojom::kAccessibilitySectionPath},
{ChromePage::MANAGEACCESSIBILITYTTS,
chromeos::settings::mojom::kTextToSpeechSubpagePath},
{ChromePage::MULTIDEVICE,
chromeos::settings::mojom::kMultiDeviceSectionPath},
{ChromePage::NETWORKSTYPEVPN,
chromeos::settings::mojom::kVpnDetailsSubpagePath},
{ChromePage::OSLANGUAGESINPUT,
chromeos::settings::mojom::kInputSubpagePath},
{ChromePage::OSLANGUAGESLANGUAGES,
chromeos::settings::mojom::kLanguagesSubpagePath},
{ChromePage::PERDEVICEKEYBOARD,
chromeos::settings::mojom::kPerDeviceKeyboardSubpagePath},
{ChromePage::PERDEVICEMOUSE,
chromeos::settings::mojom::kPerDeviceMouseSubpagePath},
{ChromePage::PERDEVICEPOINTINGSTICK,
chromeos::settings::mojom::kPerDevicePointingStickSubpagePath},
{ChromePage::PERDEVICETOUCHPAD,
chromeos::settings::mojom::kPerDeviceTouchpadSubpagePath},
{ChromePage::POINTEROVERLAY,
chromeos::settings::mojom::kPointersSubpagePath},
{ChromePage::POWER, chromeos::settings::mojom::kPowerSubpagePath},
{ChromePage::PRIVACYHUB,
chromeos::settings::mojom::kPrivacyHubSubpagePath},
{ChromePage::SMARTPRIVACY,
chromeos::settings::mojom::kSmartPrivacySubpagePath},
{ChromePage::STORAGE, chromeos::settings::mojom::kStorageSubpagePath},
{ChromePage::WIFI, chromeos::settings::mojom::kWifiNetworksSubpagePath},
{ChromePage::NETWORKS, chromeos::settings::mojom::kNetworkSectionPath},
});
constexpr auto kBrowserSettingsMap =
base::MakeFixedFlatMap<ChromePage, const char*>({
{ChromePage::APPEARANCE, chrome::kAppearanceSubPage},
{ChromePage::AUTOFILL, chrome::kAutofillSubPage},
{ChromePage::CLEARBROWSERDATA, chrome::kClearBrowserDataSubPage},
{ChromePage::DOWNLOADS, chrome::kDownloadsSubPage},
{ChromePage::LANGUAGES, chrome::kLanguagesSubPage},
{ChromePage::ONSTARTUP, chrome::kOnStartupSubPage},
{ChromePage::PASSWORDS, chrome::kPasswordManagerSubPage},
{ChromePage::PRIVACY, chrome::kPrivacySubPage},
{ChromePage::RESET, chrome::kResetSubPage},
{ChromePage::SEARCH, chrome::kSearchSubPage},
{ChromePage::SYNCSETUP, chrome::kSyncSetupSubPage},
});
constexpr auto kAboutPagesMap =
base::MakeFixedFlatMap<ChromePage, const char*>({
{ChromePage::ABOUTBLANK, url::kAboutBlankURL},
{ChromePage::ABOUTDOWNLOADS, "chrome://downloads/"},
{ChromePage::ABOUTHISTORY, "chrome://history/"},
});
// Converts the given ARC URL to an external file URL to read it via ARC content
// file system when necessary. Otherwise, returns the given URL unchanged.
GURL ConvertArcUrlToExternalFileUrlIfNeeded(const GURL& url) {
if (url.SchemeIs(url::kFileScheme) || url.SchemeIs(url::kContentScheme)) {
// Chrome cannot open this URL. Read the contents via ARC content file
// system with an external file URL.
return arc::ArcUrlToExternalFileUrl(url);
}
return url;
}
// Converts a content:// ARC URL to a file:// URL managed by the FuseBox Moniker
// system. This Moniker file is readable on the Linux filesystem like any other
// file. Returns an empty URL if a Moniker could not be created.
GURL ConvertToMonikerFileUrl(Profile* profile, GURL content_url) {
return ash::ExternalFileURLToFuseboxMonikerFileURL(
profile, arc::ArcUrlToExternalFileUrl(content_url),
/*read_only=*/true, webshare::PrepareDirectoryTask::kSharedFileLifetime);
}
apps::IntentPtr ConvertLaunchIntent(
Profile* profile,
const arc::mojom::LaunchIntentPtr& launch_intent) {
const char* action =
apps_util::ConvertArcToAppServiceIntentAction(launch_intent->action);
auto intent = std::make_unique<apps::Intent>(action ? action : "");
intent->url = launch_intent->data;
intent->mime_type = launch_intent->type;
intent->share_title = launch_intent->extra_subject;
intent->share_text = launch_intent->extra_text;
if (launch_intent->files.has_value() && launch_intent->files->size() > 0) {
std::vector<std::string> mime_types;
for (const auto& file_info : *launch_intent->files) {
GURL moniker_url =
ConvertToMonikerFileUrl(profile, file_info->content_uri);
if (moniker_url.is_empty()) {
// Continue launching the web app, but without any invalid attached
// files.
continue;
}
apps::IntentFilePtr file =
std::make_unique<apps::IntentFile>(moniker_url);
file->mime_type = file_info->type;
file->file_name = file_info->name;
file->file_size = file_info->size;
intent->files.push_back(std::move(file));
mime_types.push_back(file_info->type);
}
// Override the given MIME type based on the files that we're sharing.
intent->mime_type = apps_util::CalculateCommonMimeType(mime_types);
}
return intent;
}
} // namespace
ArcOpenUrlDelegateImpl::ArcOpenUrlDelegateImpl() {
arc::ArcIntentHelperBridge::SetOpenUrlDelegate(this);
DCHECK(!g_instance);
g_instance = this;
}
ArcOpenUrlDelegateImpl::~ArcOpenUrlDelegateImpl() {
DCHECK_EQ(g_instance, this);
g_instance = nullptr;
arc::ArcIntentHelperBridge::SetOpenUrlDelegate(nullptr);
}
ArcOpenUrlDelegateImpl* ArcOpenUrlDelegateImpl::GetForTesting() {
return g_instance;
}
void ArcOpenUrlDelegateImpl::OpenUrlFromArc(const GURL& url) {
if (!url.is_valid()) {
return;
}
GURL url_to_open = ConvertArcUrlToExternalFileUrlIfNeeded(url);
ash::NewWindowDelegate::GetPrimary()->OpenUrl(
url_to_open, ash::NewWindowDelegate::OpenUrlFrom::kArc,
ash::NewWindowDelegate::Disposition::kNewForegroundTab);
}
void ArcOpenUrlDelegateImpl::OpenWebAppFromArc(const GURL& url) {
DCHECK(url.is_valid() && url.SchemeIs(url::kHttpsScheme));
// Fetch the profile associated with ARC. This method should only be called
// for a |url| which was installed via ARC, and so we want the web app that is
// opened through here to be installed in the profile associated with ARC.
// |user| may be null if sign-in hasn't happened yet
const auto* user = user_manager::UserManager::Get()->GetPrimaryUser();
if (!user) {
return;
}
// `profile` may be null if sign-in has happened but the profile isn't loaded
// yet.
Profile* profile = ash::ProfileHelper::Get()->GetProfileByUser(user);
if (!profile) {
return;
}
std::optional<webapps::AppId> app_id =
web_app::FindInstalledAppWithUrlInScope(profile, url,
/*window_only=*/true);
if (!app_id) {
OpenUrlFromArc(url);
return;
}
int event_flags = apps::GetEventFlags(WindowOpenDisposition::NEW_WINDOW,
/*prefer_container=*/false);
apps::AppServiceProxy* proxy =
apps::AppServiceProxyFactory::GetForProfile(profile);
proxy->AppRegistryCache().ForOneApp(
*app_id, [&event_flags](const apps::AppUpdate& update) {
if (update.WindowMode() == apps::WindowMode::kBrowser) {
event_flags =
apps::GetEventFlags(WindowOpenDisposition::NEW_FOREGROUND_TAB,
/*prefer_container=*/false);
}
});
proxy->LaunchAppWithUrl(*app_id, event_flags, url,
apps::LaunchSource::kFromArc);
ash::ApkWebAppService* apk_web_app_service =
ash::ApkWebAppService::Get(profile);
if (!apk_web_app_service ||
!apk_web_app_service->IsWebAppInstalledFromArc(app_id.value())) {
return;
}
ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile);
if (!prefs) {
return;
}
std::optional<std::string> package_name =
apk_web_app_service->GetPackageNameForWebApp(app_id.value());
if (!package_name.has_value()) {
return;
}
ChromeShelfController* chrome_shelf_controller =
ChromeShelfController::instance();
if (!chrome_shelf_controller) {
return;
}
auto* arc_tracker =
chrome_shelf_controller->app_service_app_window_controller()
->app_service_arc_tracker();
if (!arc_tracker) {
return;
}
for (const auto& id : prefs->GetAppsForPackage(package_name.value())) {
arc_tracker->CloseWindows(id);
}
}
void ArcOpenUrlDelegateImpl::OpenArcCustomTab(
const GURL& url,
int32_t task_id,
arc::mojom::IntentHelperHost::OnOpenCustomTabCallback callback) {
GURL url_to_open = ConvertArcUrlToExternalFileUrlIfNeeded(url);
Profile* profile = ProfileManager::GetActiveUserProfile();
aura::Window* arc_window = arc::GetArcWindow(task_id);
if (!arc_window) {
std::move(callback).Run(mojo::NullRemote());
return;
}
auto custom_tab = std::make_unique<arc::CustomTab>(arc_window);
auto web_contents = arc::CreateArcCustomTabWebContents(profile, url);
const user_manager::User& user = CHECK_DEREF(
ash::BrowserContextHelper::Get()->GetUserByBrowserContext(profile));
// |custom_tab_browser| will be destroyed when its tab strip becomes empty,
// either due to the user opening the custom tab page in a tabbed browser or
// because of the CustomTabSessionImpl object getting destroyed.
ash::BrowserDelegate* custom_tab_browser =
ash::BrowserController::GetInstance()->CreateCustomTab(
user, std::move(web_contents));
CHECK(custom_tab_browser);
// TODO(crbug.com/41454219): Remove this temporary conversion to InterfacePtr
// once OnOpenCustomTab from
// //chromeos/ash/experiences/arc/mojom/intent_helper.mojom could take
// pending_remote directly. Refer to crrev.com/c/1868870.
auto custom_tab_remote(CustomTabSessionImpl::Create(
std::move(custom_tab), &custom_tab_browser->GetBrowser()));
std::move(callback).Run(std::move(custom_tab_remote));
}
void ArcOpenUrlDelegateImpl::OpenChromePageFromArc(ChromePage page) {
if (auto it = kOSSettingsMap.find(page); it != kOSSettingsMap.end()) {
Profile* profile = ProfileManager::GetActiveUserProfile();
std::string sub_page = it->second;
chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(profile,
sub_page);
return;
}
if (auto it = kBrowserSettingsMap.find(page);
it != kBrowserSettingsMap.end()) {
OpenUrlFromArc(GURL(chrome::kChromeUISettingsURL).Resolve(it->second));
return;
}
if (auto it = kAboutPagesMap.find(page); it != kAboutPagesMap.end()) {
OpenUrlFromArc(GURL(it->second));
return;
}
NOTREACHED();
}
void ArcOpenUrlDelegateImpl::OpenAppWithIntent(
const GURL& start_url,
arc::mojom::LaunchIntentPtr arc_intent) {
DCHECK(start_url.is_valid());
DCHECK(start_url.SchemeIs(url::kHttpsScheme) || net::IsLocalhost(start_url));
// Fetch the profile associated with ARC. This method should only be called
// for a |url| which was installed via ARC, and so we want the web app that is
// opened through here to be installed in the profile associated with ARC.
const auto* user = user_manager::UserManager::Get()->GetPrimaryUser();
DCHECK(user);
// |profile| may be null if sign-in has happened but the profile isn't loaded
// yet.
Profile* profile = ash::ProfileHelper::Get()->GetProfileByUser(user);
if (!profile) {
return;
}
webapps::AppId app_id =
web_app::GenerateAppId(/*manifest_id=*/std::nullopt, start_url);
bool app_installed = false;
auto* proxy = apps::AppServiceProxyFactory::GetForProfile(profile);
proxy->AppRegistryCache().ForOneApp(
app_id, [&app_installed](const apps::AppUpdate& update) {
app_installed = apps_util::IsInstalled(update.Readiness());
});
if (!app_installed) {
if (arc_intent->data) {
OpenUrlFromArc(*arc_intent->data);
}
return;
}
apps::IntentPtr intent = ConvertLaunchIntent(profile, arc_intent);
auto disposition = WindowOpenDisposition::NEW_WINDOW;
proxy->AppRegistryCache().ForOneApp(
app_id, [&disposition](const apps::AppUpdate& update) {
if (update.WindowMode() == apps::WindowMode::kBrowser) {
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
}
});
int event_flags = apps::GetEventFlags(disposition,
/*prefer_container=*/false);
proxy->LaunchAppWithIntent(app_id, event_flags, std::move(intent),
apps::LaunchSource::kFromArc, nullptr,
base::DoNothing());
}
|