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
|
// 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/apps/app_service/launch_utils.h"
#include <memory>
#include <optional>
#include <string_view>
#include <utility>
#include "base/check.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/file_utils.h"
#include "chrome/browser/apps/app_service/intent_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/web_applications/link_capturing_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/webui_url_constants.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/sessions/core/session_id.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/constants.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "storage/browser/file_system/file_system_url.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/events/event_constants.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/public/cpp/new_window_delegate.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chromeos/ash/experiences/arc/mojom/app.mojom.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace apps {
LaunchContainer ConvertWindowModeToAppLaunchContainer(WindowMode window_mode) {
switch (window_mode) {
case WindowMode::kBrowser:
return LaunchContainer::kLaunchContainerTab;
case WindowMode::kWindow:
case WindowMode::kTabbedWindow:
return LaunchContainer::kLaunchContainerWindow;
case WindowMode::kUnknown:
return LaunchContainer::kLaunchContainerNone;
}
}
std::vector<base::FilePath> GetLaunchFilesFromCommandLine(
const base::CommandLine& command_line) {
std::vector<base::FilePath> launch_files;
if (!command_line.HasSwitch(switches::kAppId)) {
return launch_files;
}
launch_files.reserve(command_line.GetArgs().size());
for (const auto& arg : command_line.GetArgs()) {
#if BUILDFLAG(IS_WIN)
GURL url(base::AsStringPiece16(arg));
#else
GURL url(arg);
#endif
if (url.is_valid() && !url.SchemeIsFile()) {
continue;
}
base::FilePath path(arg);
if (path.empty()) {
continue;
}
launch_files.push_back(path);
}
return launch_files;
}
Browser* CreateBrowserWithNewTabPage(Profile* profile) {
Browser::CreateParams create_params(profile, /*user_gesture=*/false);
Browser* browser = Browser::Create(create_params);
NavigateParams params(browser, GURL(chrome::kChromeUINewTabURL),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.tabstrip_add_types = AddTabTypes::ADD_ACTIVE;
Navigate(¶ms);
browser->window()->Show();
return browser;
}
AppLaunchParams CreateAppIdLaunchParamsWithEventFlags(
const std::string& app_id,
int event_flags,
LaunchSource launch_source,
int64_t display_id,
LaunchContainer fallback_container) {
WindowOpenDisposition raw_disposition =
ui::DispositionFromEventFlags(event_flags);
LaunchContainer container;
WindowOpenDisposition disposition;
if (raw_disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB ||
raw_disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB) {
container = LaunchContainer::kLaunchContainerTab;
disposition = raw_disposition;
} else if (raw_disposition == WindowOpenDisposition::NEW_WINDOW) {
container = LaunchContainer::kLaunchContainerWindow;
disposition = raw_disposition;
} else {
// Look at preference to find the right launch container. If no preference
// is set, launch as a regular tab.
container = fallback_container;
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
}
return AppLaunchParams(app_id, container, disposition, launch_source,
display_id);
}
AppLaunchParams CreateAppLaunchParamsForIntent(
const std::string& app_id,
int32_t event_flags,
LaunchSource launch_source,
int64_t display_id,
LaunchContainer fallback_container,
IntentPtr&& intent,
Profile* profile) {
auto params = CreateAppIdLaunchParamsWithEventFlags(
app_id, event_flags, launch_source, display_id, fallback_container);
if (intent->url.has_value()) {
params.override_url = intent->url.value();
}
#if BUILDFLAG(IS_CHROMEOS)
if (!intent->files.empty()) {
std::vector<GURL> file_urls;
for (const auto& intent_file : intent->files) {
if (intent_file->url.SchemeIsFile()) {
DCHECK(file_urls.empty());
break;
}
file_urls.push_back(intent_file->url);
}
if (!file_urls.empty()) {
std::vector<storage::FileSystemURL> file_system_urls =
GetFileSystemURL(profile, file_urls);
for (const auto& file_system_url : file_system_urls) {
params.launch_files.push_back(file_system_url.path());
}
}
}
#endif // BUILDFLAG(IS_CHROMEOS)
params.intent = std::move(intent);
return params;
}
extensions::AppLaunchSource GetAppLaunchSource(LaunchSource launch_source) {
switch (launch_source) {
case LaunchSource::kUnknown:
case LaunchSource::kFromAppListGrid:
case LaunchSource::kFromAppListGridContextMenu:
case LaunchSource::kFromAppListQuery:
case LaunchSource::kFromAppListQueryContextMenu:
case LaunchSource::kFromAppListRecommendation:
case LaunchSource::kFromParentalControls:
case LaunchSource::kFromShelf:
case LaunchSource::kFromLink:
case LaunchSource::kFromOmnibox:
case LaunchSource::kFromOtherApp:
case LaunchSource::kFromSharesheet:
return extensions::AppLaunchSource::kSourceAppLauncher;
case LaunchSource::kFromMenu:
return extensions::AppLaunchSource::kSourceContextMenu;
case LaunchSource::kFromKeyboard:
return extensions::AppLaunchSource::kSourceKeyboard;
case LaunchSource::kFromFileManager:
return extensions::AppLaunchSource::kSourceFileHandler;
case LaunchSource::kFromChromeInternal:
case LaunchSource::kFromReleaseNotesNotification:
case LaunchSource::kFromFullRestore:
case LaunchSource::kFromSmartTextContextMenu:
case LaunchSource::kFromDiscoverTabNotification:
case LaunchSource::kFromFirstRun:
case LaunchSource::kFromWelcomeTour:
return extensions::AppLaunchSource::kSourceChromeInternal;
case LaunchSource::kFromInstalledNotification:
return extensions::AppLaunchSource::kSourceInstalledNotification;
case LaunchSource::kFromTest:
return extensions::AppLaunchSource::kSourceTest;
case LaunchSource::kFromArc:
return extensions::AppLaunchSource::kSourceArc;
case LaunchSource::kFromManagementApi:
return extensions::AppLaunchSource::kSourceManagementApi;
case LaunchSource::kFromKiosk:
return extensions::AppLaunchSource::kSourceKiosk;
case LaunchSource::kFromCommandLine:
return extensions::AppLaunchSource::kSourceCommandLine;
case LaunchSource::kFromBackgroundMode:
return extensions::AppLaunchSource::kSourceBackground;
case LaunchSource::kFromNewTabPage:
return extensions::AppLaunchSource::kSourceNewTabPage;
case LaunchSource::kFromIntentUrl:
return extensions::AppLaunchSource::kSourceIntentUrl;
case LaunchSource::kFromOsLogin:
return extensions::AppLaunchSource::kSourceRunOnOsLogin;
case LaunchSource::kFromProtocolHandler:
return extensions::AppLaunchSource::kSourceProtocolHandler;
case LaunchSource::kFromUrlHandler:
return extensions::AppLaunchSource::kSourceUrlHandler;
case LaunchSource::kFromLockScreen:
return extensions::AppLaunchSource::kSourceUntracked;
case LaunchSource::kFromAppHomePage:
return extensions::AppLaunchSource::kSourceAppHomePage;
case LaunchSource::kFromFocusMode:
return extensions::AppLaunchSource::kSourceFocusMode;
case LaunchSource::kFromSparky:
return extensions::AppLaunchSource::kSourceSparky;
// No equivalent extensions launch source or not needed in extensions:
case LaunchSource::kFromReparenting:
case LaunchSource::kFromProfileMenu:
case LaunchSource::kFromSysTrayCalendar:
case LaunchSource::kFromInstaller:
case LaunchSource::kFromNavigationCapturing:
case LaunchSource::kFromWebInstallApi:
return extensions::AppLaunchSource::kSourceNone;
}
}
int GetEventFlags(WindowOpenDisposition disposition, bool prefer_container) {
if (prefer_container) {
return ui::EF_NONE;
}
switch (disposition) {
case WindowOpenDisposition::NEW_WINDOW:
return ui::EF_SHIFT_DOWN;
case WindowOpenDisposition::NEW_BACKGROUND_TAB:
return ui::EF_MIDDLE_MOUSE_BUTTON;
case WindowOpenDisposition::NEW_FOREGROUND_TAB:
return ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_SHIFT_DOWN;
default:
NOTREACHED();
}
}
int GetSessionIdForRestoreFromWebContents(
const content::WebContents* web_contents) {
if (!web_contents) {
return SessionID::InvalidValue().id();
}
const tabs::TabInterface* tab =
tabs::TabInterface::GetFromContents(web_contents);
const BrowserWindowInterface* browser = tab->GetBrowserWindowInterface();
if (!browser) {
return SessionID::InvalidValue().id();
}
return browser->GetSessionID().id();
}
#if BUILDFLAG(IS_CHROMEOS)
arc::mojom::WindowInfoPtr MakeArcWindowInfo(WindowInfoPtr window_info) {
if (!window_info) {
return nullptr;
}
arc::mojom::WindowInfoPtr arc_window_info = arc::mojom::WindowInfo::New();
arc_window_info->window_id = window_info->window_id;
arc_window_info->state = window_info->state;
arc_window_info->display_id = window_info->display_id;
if (window_info->bounds.has_value()) {
arc_window_info->bounds = std::move(window_info->bounds);
}
return arc_window_info;
}
AppIdsToLaunchForUrl::AppIdsToLaunchForUrl() = default;
AppIdsToLaunchForUrl::AppIdsToLaunchForUrl(AppIdsToLaunchForUrl&&) = default;
AppIdsToLaunchForUrl::~AppIdsToLaunchForUrl() = default;
AppIdsToLaunchForUrl FindAppIdsToLaunchForUrl(AppServiceProxy* proxy,
const GURL& url) {
// Navigation Capturing also enables launching of browser-tab apps.
bool exclude_browser_tab_apps = !features::IsNavigationCapturingReimplEnabled();
AppIdsToLaunchForUrl result;
result.candidates =
proxy->GetAppIdsForUrl(url, /*exclude_browsers=*/true, exclude_browser_tab_apps);
if (result.candidates.empty()) {
return result;
}
std::optional<std::string> preferred =
proxy->PreferredAppsList().FindPreferredAppForUrl(url);
if (preferred && base::Contains(result.candidates, *preferred)) {
result.preferred = std::move(preferred);
}
return result;
}
void MaybeLaunchPreferredAppForUrl(Profile* profile,
const GURL& url,
LaunchSource launch_source) {
if (AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
auto* proxy = AppServiceProxyFactory::GetForProfile(profile);
AppIdsToLaunchForUrl app_id_to_launch =
FindAppIdsToLaunchForUrl(proxy, url);
if (app_id_to_launch.preferred) {
proxy->LaunchAppWithUrl(*app_id_to_launch.preferred,
/*event_flags=*/0, url, launch_source);
return;
}
}
CHECK(ash::NewWindowDelegate::GetPrimary());
ash::NewWindowDelegate::GetPrimary()->OpenUrl(
url, ash::NewWindowDelegate::OpenUrlFrom::kUserInteraction,
ash::NewWindowDelegate::Disposition::kNewForegroundTab);
}
void LaunchUrlInInstalledAppOrBrowser(Profile* profile,
const GURL& url,
LaunchSource launch_source) {
if (AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
auto* proxy = AppServiceProxyFactory::GetForProfile(profile);
AppIdsToLaunchForUrl candidate_apps = FindAppIdsToLaunchForUrl(proxy, url);
std::optional<std::string> app_id = candidate_apps.preferred;
if (!app_id && candidate_apps.candidates.size() == 1) {
app_id = candidate_apps.candidates[0];
}
if (app_id) {
proxy->LaunchAppWithUrl(*app_id,
/*event_flags=*/0, url, launch_source);
return;
}
}
CHECK(ash::NewWindowDelegate::GetPrimary());
ash::NewWindowDelegate::GetPrimary()->OpenUrl(
url, ash::NewWindowDelegate::OpenUrlFrom::kUserInteraction,
ash::NewWindowDelegate::Disposition::kNewForegroundTab);
}
#endif // BUILDFLAG(IS_CHROMEOS)
} // namespace apps
|