Skip to content

Commit

Permalink
Add the LaunchPlatformAppWithFilePaths interface.
Browse files Browse the repository at this point in the history
For extensions, add the LaunchPlatformAppWithFilePaths interface to
launch the app by issuing an onLaunched event with the contents of
file_paths.

BUG=1090211

Change-Id: I92db10bac0d64e3c108d4aa67770a9b5f35d30f3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2264080
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: Dominick Ng <dominickn@chromium.org>
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805637}
  • Loading branch information
nancylingwang authored and Commit Bot committed Sep 10, 2020
1 parent 30a5da0 commit d282a5a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
13 changes: 11 additions & 2 deletions apps/launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,17 @@ void LaunchPlatformAppWithCommandLineAndLaunchId(
void LaunchPlatformAppWithPath(content::BrowserContext* context,
const Extension* app,
const base::FilePath& file_path) {
scoped_refptr<PlatformAppPathLauncher> launcher =
new PlatformAppPathLauncher(context, app, file_path);
auto launcher =
base::MakeRefCounted<PlatformAppPathLauncher>(context, app, file_path);
launcher->Launch();
}

void LaunchPlatformAppWithFilePaths(
content::BrowserContext* context,
const extensions::Extension* app,
const std::vector<base::FilePath>& file_paths) {
auto launcher =
base::MakeRefCounted<PlatformAppPathLauncher>(context, app, file_paths);
launcher->Launch();
}

Expand Down
7 changes: 7 additions & 0 deletions apps/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ void LaunchPlatformAppWithPath(content::BrowserContext* context,
const extensions::Extension* app,
const base::FilePath& file_path);

// Launches the platform app |app| by issuing an onLaunched event with the
// contents of |file_paths| available through the launch data.
void LaunchPlatformAppWithFilePaths(
content::BrowserContext* context,
const extensions::Extension* app,
const std::vector<base::FilePath>& file_paths);

// Launches the platform app |app| with the specific |action_data|. |file_path|
// is an optional argument and if present contains the file that the app should
// open w.r.t. the given action.
Expand Down
32 changes: 32 additions & 0 deletions chrome/browser/apps/platform_apps/app_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ class PlatformAppWithFileBrowserTest : public PlatformAppBrowserTest {
extension_name, *base::CommandLine::ForCurrentProcess());
}

void RunPlatformAppTestWithFiles(const std::string& extension_name,
const std::string& test_file) {
extensions::ResultCatcher catcher;

base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
base::FilePath file_path = test_doc.NormalizePathSeparators();

base::FilePath extension_path = test_data_dir_.AppendASCII(extension_name);
const extensions::Extension* extension =
LoadExtensionWithFlags(extension_path, kFlagNone);
ASSERT_TRUE(extension);

apps::mojom::FilePathsPtr launch_files = apps::mojom::FilePaths::New();
launch_files->file_paths.push_back(file_path);
apps::AppServiceProxyFactory::GetForProfile(browser()->profile())
->LaunchAppWithFiles(
extension->id(), apps::mojom::LaunchContainer::kLaunchContainerNone,
apps::GetEventFlags(
apps::mojom::LaunchContainer::kLaunchContainerNone,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
true /* preferred_container */),
apps::mojom::LaunchSource::kFromTest, std::move(launch_files));
ASSERT_TRUE(catcher.GetNextResult());
}

private:
bool RunPlatformAppTestWithCommandLine(
const std::string& extension_name,
Expand Down Expand Up @@ -567,6 +592,13 @@ IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_ExtensionWindowingApis) {
// ChromeOS does not support passing arguments on the command line, so the tests
// that rely on this functionality are disabled.
#if !defined(OS_CHROMEOS)
// Tests that launch data is sent through if the file extension matches.
IN_PROC_BROWSER_TEST_F(PlatformAppWithFileBrowserTest,
LaunchFilesWithFileExtension) {
RunPlatformAppTestWithFiles("platform_apps/launch_file_by_extension",
kTestFilePath);
}

// Tests that command line parameters get passed through to platform apps
// via launchData correctly when launching with a file.
// TODO(benwells/jeremya): tests need a way to specify a handler ID.
Expand Down
12 changes: 12 additions & 0 deletions chrome/browser/ui/extensions/application_launch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ WebContents* OpenEnabledApplication(Profile* profile,
prefs->SetActiveBit(extension->id(), true);

if (CanLaunchViaEvent(extension)) {
// When launching an app with a command line, there might be a file path to
// work with that command line, so
// LaunchPlatformAppWithCommandLineAndLaunchId should be called to handle
// the command line. If |launch_files| is set without |command_line|, that
// means launching the app with files, so call
// LaunchPlatformAppWithFilePaths to forward |launch_files| to the app.
if (params.command_line.GetArgs().empty() && !params.launch_files.empty()) {
apps::LaunchPlatformAppWithFilePaths(profile, extension,
params.launch_files);
return nullptr;
}

apps::LaunchPlatformAppWithCommandLineAndLaunchId(
profile, extension, params.launch_id, params.command_line,
params.current_directory, params.source);
Expand Down

0 comments on commit d282a5a

Please sign in to comment.