File: browser_app_launcher.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 (188 lines) | stat: -rw-r--r-- 7,524 bytes parent folder | download | duplicates (6)
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
// 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/browser_app_launcher.h"

#include <memory>
#include <utility>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/app_launch_params.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/extension.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "components/app_restore/app_launch_info.h"
#include "components/app_restore/full_restore_save_handler.h"
#include "components/app_restore/full_restore_utils.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/intent.h"
#include "components/sessions/core/session_id.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace {

void OnLaunchCompleteReportRestoreMetrics(
    base::OnceCallback<void(content::WebContents*)> callback,
    Profile* profile,
    int restore_id,
    apps::AppLaunchParams params_for_restore,
    base::WeakPtr<Browser> browser,
    base::WeakPtr<content::WebContents> web_contents,
    apps::LaunchContainer launch_container) {
#if BUILDFLAG(IS_CHROMEOS)
  if (!SessionID::IsValidValue(restore_id)) {
    RecordAppLaunchMetrics(
        profile, apps::AppType::kWeb, params_for_restore.app_id,
        params_for_restore.launch_source, params_for_restore.container);
    std::move(callback).Run(web_contents.get());
    return;
  }

  RecordAppLaunchMetrics(
      profile, apps::AppType::kWeb, params_for_restore.app_id,
      apps::LaunchSource::kFromFullRestore, params_for_restore.container);

  int session_id =
      apps::GetSessionIdForRestoreFromWebContents(web_contents.get());
  if (!SessionID::IsValidValue(session_id)) {
    std::move(callback).Run(web_contents.get());
    return;
  }

  // If the restore id is available, save the launch parameters to the full
  // restore file for the system web apps.
  auto launch_info = std::make_unique<app_restore::AppLaunchInfo>(
      params_for_restore.app_id, session_id, params_for_restore.container,
      params_for_restore.disposition, params_for_restore.display_id,
      std::move(params_for_restore.launch_files),
      std::move(params_for_restore.intent));
  full_restore::SaveAppLaunchInfo(profile->GetPath(), std::move(launch_info));

#endif  // BUILDFLAG(IS_CHROMEOS)
  std::move(callback).Run(web_contents.get());
}

void LaunchAppWithParamsImpl(
    apps::AppLaunchParams params,
    Profile* profile,
    base::OnceCallback<void(content::WebContents*)> on_complete) {
  const extensions::Extension* extension =
      extensions::ExtensionRegistry::Get(profile)->GetInstalledExtension(
          params.app_id);

  apps::AppLaunchParams params_for_restore(
      params.app_id, params.container, params.disposition, params.launch_source,
      params.display_id, params.launch_files, params.intent);
  int restore_id = params.restore_id;
  std::string app_id = params.app_id;

  if (!extension) {
    web_app::WebAppProvider* provider =
        web_app::WebAppProvider::GetForLocalAppsUnchecked(profile);
#if BUILDFLAG(IS_CHROMEOS)
    // Create the FullRestoreSaveHandler instance before launching the app to
    // observe the browser window.
    full_restore::FullRestoreSaveHandler::GetInstance();
#endif  // BUILDFLAG(IS_CHROMEOS)
    provider->scheduler().LaunchAppWithCustomParams(
        std::move(params),
        base::BindOnce(OnLaunchCompleteReportRestoreMetrics,
                       std::move(on_complete), profile, restore_id,
                       std::move(params_for_restore)));
    return;
  }

#if BUILDFLAG(IS_CHROMEOS)
  // If the restore id is available, save the launch parameters to the full
  // restore file.
  if (SessionID::IsValidValue(restore_id)) {
    RecordAppLaunchMetrics(profile, apps::AppType::kChromeApp, app_id,
                           apps::LaunchSource::kFromFullRestore,
                           params.container);

    auto launch_info = std::make_unique<app_restore::AppLaunchInfo>(
        params_for_restore.app_id, params_for_restore.container,
        params_for_restore.disposition, params_for_restore.display_id,
        std::move(params_for_restore.launch_files),
        std::move(params_for_restore.intent));
    full_restore::SaveAppLaunchInfo(profile->GetPath(), std::move(launch_info));
  } else {
    RecordAppLaunchMetrics(profile, apps::AppType::kChromeApp, params.app_id,
                           params.launch_source, params.container);
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
  std::move(on_complete).Run(::OpenApplication(profile, std::move(params)));
}
}  // namespace

namespace apps {

BrowserAppLauncher::BrowserAppLauncher(Profile* profile) : profile_(profile) {}

BrowserAppLauncher::~BrowserAppLauncher() = default;

#if !BUILDFLAG(IS_CHROMEOS)
void BrowserAppLauncher::LaunchAppWithParams(
    AppLaunchParams params,
    base::OnceCallback<void(content::WebContents*)> callback) {
  LaunchAppWithParamsImpl(std::move(params), profile_, std::move(callback));
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

content::WebContents* BrowserAppLauncher::LaunchAppWithParamsForTesting(
    AppLaunchParams params) {
  // For some ChromeOS tests (and specifically ones that use SpeechMonitor),
  // they use a base::RunLoop already to wait for accessibility tasks to
  // complete. Because that makes this base::RunLoop nested,
  // `kNestableTasksAllowed` is required to allow the posted launch command to
  // execute, as it is not a system task.
  base::RunLoop launch_waiter(base::RunLoop::Type::kNestableTasksAllowed);
  content::WebContents* web_contents_holder;
  LaunchAppWithParamsImpl(
      std::move(params), profile_,
      base::BindOnce(
          [](base::OnceClosure done, content::WebContents** output,
             content::WebContents* contents) {
            *output = contents;
            std::move(done).Run();
          },
          launch_waiter.QuitClosure(), &web_contents_holder));
  launch_waiter.Run();
  return web_contents_holder;
}

#if BUILDFLAG(IS_CHROMEOS)
void BrowserAppLauncher::LaunchPlayStoreWithExtensions() {
  const extensions::Extension* extension =
      extensions::ExtensionRegistry::Get(profile_)->GetInstalledExtension(
          arc::kPlayStoreAppId);
  DCHECK(extension);
  DCHECK(extensions::util::IsAppLaunchable(arc::kPlayStoreAppId, profile_));
  LaunchAppWithParamsImpl(
      CreateAppLaunchParamsUserContainer(
          profile_, extension, WindowOpenDisposition::NEW_WINDOW,
          apps::LaunchSource::kFromChromeInternal),
      profile_, base::DoNothing());
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace apps