Commit 3567d142 authored by benwells@chromium.org's avatar benwells@chromium.org

Use file thread when converting paths to absolute when loading apps.

BUG=349816

Review URL: https://codereview.chromium.org/277873002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269768 0039d316-1c4b-4281-b951-d872f2087c98
parent a8eaa189
...@@ -63,8 +63,8 @@ namespace { ...@@ -63,8 +63,8 @@ namespace {
const char kFallbackMimeType[] = "application/octet-stream"; const char kFallbackMimeType[] = "application/octet-stream";
bool MakePathAbsolute(const base::FilePath& current_directory, bool DoMakePathAbsolute(const base::FilePath& current_directory,
base::FilePath* file_path) { base::FilePath* file_path) {
DCHECK(file_path); DCHECK(file_path);
if (file_path->IsAbsolute()) if (file_path->IsAbsolute())
return true; return true;
...@@ -81,27 +81,11 @@ bool MakePathAbsolute(const base::FilePath& current_directory, ...@@ -81,27 +81,11 @@ bool MakePathAbsolute(const base::FilePath& current_directory,
return true; return true;
} }
bool GetAbsolutePathFromCommandLine(const CommandLine& command_line,
const base::FilePath& current_directory,
base::FilePath* path) {
if (!command_line.GetArgs().size())
return false;
base::FilePath relative_path(command_line.GetArgs()[0]);
base::FilePath absolute_path(relative_path);
if (!MakePathAbsolute(current_directory, &absolute_path)) {
LOG(WARNING) << "Cannot make absolute path from " << relative_path.value();
return false;
}
*path = absolute_path;
return true;
}
// Helper method to launch the platform app |extension| with no data. This // Helper method to launch the platform app |extension| with no data. This
// should be called in the fallback case, where it has been impossible to // should be called in the fallback case, where it has been impossible to
// load or obtain file launch data. // load or obtain file launch data.
void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK_CURRENTLY_ON(BrowserThread::UI);
AppEventRouter::DispatchOnLaunchedEvent(profile, extension); AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
} }
...@@ -119,7 +103,7 @@ class PlatformAppPathLauncher ...@@ -119,7 +103,7 @@ class PlatformAppPathLauncher
: profile_(profile), extension_(extension), file_path_(file_path) {} : profile_(profile), extension_(extension), file_path_(file_path) {}
void Launch() { void Launch() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (file_path_.empty()) { if (file_path_.empty()) {
LaunchPlatformAppWithNoData(profile_, extension_); LaunchPlatformAppWithNoData(profile_, extension_);
return; return;
...@@ -147,11 +131,33 @@ class PlatformAppPathLauncher ...@@ -147,11 +131,33 @@ class PlatformAppPathLauncher
Launch(); Launch();
} }
void LaunchWithRelativePath(const base::FilePath& current_directory) {
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&PlatformAppPathLauncher::MakePathAbsolute,
this,
current_directory));
}
private: private:
friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
virtual ~PlatformAppPathLauncher() {} virtual ~PlatformAppPathLauncher() {}
void MakePathAbsolute(const base::FilePath& current_directory) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
if (!DoMakePathAbsolute(current_directory, &file_path_)) {
LOG(WARNING) << "Cannot make absolute path from " << file_path_.value();
file_path_ = base::FilePath();
}
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&PlatformAppPathLauncher::Launch, this));
}
void OnFileValid() { void OnFileValid() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (drive::util::IsUnderDriveMountPoint(file_path_)) { if (drive::util::IsUnderDriveMountPoint(file_path_)) {
...@@ -171,7 +177,7 @@ class PlatformAppPathLauncher ...@@ -171,7 +177,7 @@ class PlatformAppPathLauncher
} }
void GetMimeTypeAndLaunch() { void GetMimeTypeAndLaunch() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK_CURRENTLY_ON(BrowserThread::FILE);
// If the file doesn't exist, or is a directory, launch with no launch data. // If the file doesn't exist, or is a directory, launch with no launch data.
if (!base::PathExists(file_path_) || if (!base::PathExists(file_path_) ||
...@@ -205,7 +211,7 @@ class PlatformAppPathLauncher ...@@ -205,7 +211,7 @@ class PlatformAppPathLauncher
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
void GetMimeTypeAndLaunchForDriveFile() { void GetMimeTypeAndLaunchForDriveFile() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK_CURRENTLY_ON(BrowserThread::UI);
drive::FileSystemInterface* file_system = drive::FileSystemInterface* file_system =
drive::util::GetFileSystemByProfile(profile_); drive::util::GetFileSystemByProfile(profile_);
...@@ -222,7 +228,7 @@ class PlatformAppPathLauncher ...@@ -222,7 +228,7 @@ class PlatformAppPathLauncher
void OnGotDriveFile(drive::FileError error, void OnGotDriveFile(drive::FileError error,
const base::FilePath& file_path, const base::FilePath& file_path,
scoped_ptr<drive::ResourceEntry> entry) { scoped_ptr<drive::ResourceEntry> entry) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (error != drive::FILE_ERROR_OK || if (error != drive::FILE_ERROR_OK ||
!entry || entry->file_specific_info().is_hosted_document()) { !entry || entry->file_specific_info().is_hosted_document()) {
...@@ -310,9 +316,12 @@ class PlatformAppPathLauncher ...@@ -310,9 +316,12 @@ class PlatformAppPathLauncher
// The profile the app should be run in. // The profile the app should be run in.
Profile* profile_; Profile* profile_;
// The extension providing the app. // The extension providing the app.
// TODO(benwells): Hold onto the extension ID instead of a pointer as it
// is possible the extension will be unloaded while we're doing our thing.
// See http://crbug.com/372270 for details.
const Extension* extension_; const Extension* extension_;
// The path to be passed through to the app. // The path to be passed through to the app.
const base::FilePath file_path_; base::FilePath file_path_;
// The ID of the file handler used to launch the app. // The ID of the file handler used to launch the app.
std::string handler_id_; std::string handler_id_;
...@@ -342,22 +351,20 @@ void LaunchPlatformAppWithCommandLine(Profile* profile, ...@@ -342,22 +351,20 @@ void LaunchPlatformAppWithCommandLine(Profile* profile,
} }
} }
base::FilePath path; if (command_line.GetArgs().empty()) {
if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) {
LaunchPlatformAppWithNoData(profile, extension); LaunchPlatformAppWithNoData(profile, extension);
return; return;
} }
// TODO(benwells): add a command-line argument to provide a handler ID. base::FilePath file_path(command_line.GetArgs()[0]);
LaunchPlatformAppWithPath(profile, extension, path); scoped_refptr<PlatformAppPathLauncher> launcher =
new PlatformAppPathLauncher(profile, extension, file_path);
launcher->LaunchWithRelativePath(current_directory);
} }
void LaunchPlatformAppWithPath(Profile* profile, void LaunchPlatformAppWithPath(Profile* profile,
const Extension* extension, const Extension* extension,
const base::FilePath& file_path) { const base::FilePath& file_path) {
// launcher will be freed when nothing has a reference to it. The message
// queue will retain a reference for any outstanding task, so when the
// launcher has finished it will be freed.
scoped_refptr<PlatformAppPathLauncher> launcher = scoped_refptr<PlatformAppPathLauncher> launcher =
new PlatformAppPathLauncher(profile, extension, file_path); new PlatformAppPathLauncher(profile, extension, file_path);
launcher->Launch(); launcher->Launch();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment