| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/platform_app_launcher.h" | 5 #include "chrome/browser/extensions/platform_app_launcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; | 49 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; |
| 50 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; | 50 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; |
| 51 using extensions::app_file_handler_util::CreateFileEntry; | 51 using extensions::app_file_handler_util::CreateFileEntry; |
| 52 using extensions::app_file_handler_util::GrantedFileEntry; | 52 using extensions::app_file_handler_util::GrantedFileEntry; |
| 53 using extensions::app_file_handler_util::SavedFileEntry; | 53 using extensions::app_file_handler_util::SavedFileEntry; |
| 54 | 54 |
| 55 namespace extensions { | 55 namespace extensions { |
| 56 | 56 |
| 57 namespace { | 57 namespace { |
| 58 | 58 |
| 59 const char kFallbackMimeType[] = "application/octet-stream"; |
| 60 |
| 59 bool MakePathAbsolute(const base::FilePath& current_directory, | 61 bool MakePathAbsolute(const base::FilePath& current_directory, |
| 60 base::FilePath* file_path) { | 62 base::FilePath* file_path) { |
| 61 DCHECK(file_path); | 63 DCHECK(file_path); |
| 62 if (file_path->IsAbsolute()) | 64 if (file_path->IsAbsolute()) |
| 63 return true; | 65 return true; |
| 64 | 66 |
| 65 if (current_directory.empty()) { | 67 if (current_directory.empty()) { |
| 66 *file_path = base::MakeAbsoluteFilePath(*file_path); | 68 *file_path = base::MakeAbsoluteFilePath(*file_path); |
| 67 return !file_path->empty(); | 69 return !file_path->empty(); |
| 68 } | 70 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // If the file doesn't exist, or is a directory, launch with no launch data. | 149 // If the file doesn't exist, or is a directory, launch with no launch data. |
| 148 if (!file_util::PathExists(file_path_) || | 150 if (!file_util::PathExists(file_path_) || |
| 149 file_util::DirectoryExists(file_path_)) { | 151 file_util::DirectoryExists(file_path_)) { |
| 150 LOG(WARNING) << "No file exists with path " << file_path_.value(); | 152 LOG(WARNING) << "No file exists with path " << file_path_.value(); |
| 151 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 153 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 152 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); | 154 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); |
| 153 return; | 155 return; |
| 154 } | 156 } |
| 155 | 157 |
| 156 std::string mime_type; | 158 std::string mime_type; |
| 157 // If we cannot obtain the MIME type, launch with no launch data. | 159 if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) |
| 158 if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) { | 160 mime_type = kFallbackMimeType; |
| 159 LOG(WARNING) << "Could not obtain MIME type for " | |
| 160 << file_path_.value(); | |
| 161 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 162 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); | |
| 163 return; | |
| 164 } | |
| 165 | 161 |
| 166 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 162 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 167 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); | 163 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); |
| 168 } | 164 } |
| 169 | 165 |
| 170 #if defined(OS_CHROMEOS) | 166 #if defined(OS_CHROMEOS) |
| 171 void GetMimeTypeAndLaunchForDriveFile() { | 167 void GetMimeTypeAndLaunchForDriveFile() { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 | 169 |
| 174 drive::DriveSystemService* service = | 170 drive::DriveSystemService* service = |
| 175 drive::DriveSystemServiceFactory::FindForProfile(profile_); | 171 drive::DriveSystemServiceFactory::FindForProfile(profile_); |
| 176 if (!service) { | 172 if (!service) { |
| 177 LaunchWithNoLaunchData(); | 173 LaunchWithNoLaunchData(); |
| 178 return; | 174 return; |
| 179 } | 175 } |
| 180 | 176 |
| 181 service->file_system()->GetFileByPath( | 177 service->file_system()->GetFileByPath( |
| 182 drive::util::ExtractDrivePath(file_path_), | 178 drive::util::ExtractDrivePath(file_path_), |
| 183 base::Bind(&PlatformAppPathLauncher::OnGotDriveFile, this)); | 179 base::Bind(&PlatformAppPathLauncher::OnGotDriveFile, this)); |
| 184 } | 180 } |
| 185 | 181 |
| 186 void OnGotDriveFile(drive::DriveFileError error, | 182 void OnGotDriveFile(drive::DriveFileError error, |
| 187 const base::FilePath& file_path, | 183 const base::FilePath& file_path, |
| 188 const std::string& mime_type, | 184 const std::string& mime_type, |
| 189 drive::DriveFileType file_type) { | 185 drive::DriveFileType file_type) { |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 191 | 187 |
| 192 if (error != drive::DRIVE_FILE_OK || mime_type.empty() || | 188 if (error != drive::DRIVE_FILE_OK || file_type != drive::REGULAR_FILE) { |
| 193 file_type != drive::REGULAR_FILE) { | |
| 194 LaunchWithNoLaunchData(); | 189 LaunchWithNoLaunchData(); |
| 195 return; | 190 return; |
| 196 } | 191 } |
| 197 | 192 |
| 198 LaunchWithMimeType(mime_type); | 193 LaunchWithMimeType(mime_type.empty() ? kFallbackMimeType : mime_type); |
| 199 } | 194 } |
| 200 #endif // defined(OS_CHROMEOS) | 195 #endif // defined(OS_CHROMEOS) |
| 201 | 196 |
| 202 void LaunchWithNoLaunchData() { | 197 void LaunchWithNoLaunchData() { |
| 203 // This method is required as an entry point on the UI thread. | 198 // This method is required as an entry point on the UI thread. |
| 204 LaunchPlatformAppWithNoData(profile_, extension_); | 199 LaunchPlatformAppWithNoData(profile_, extension_); |
| 205 } | 200 } |
| 206 | 201 |
| 207 void LaunchWithMimeType(const std::string& mime_type) { | 202 void LaunchWithMimeType(const std::string& mime_type) { |
| 208 // Find file handler from the platform app for the file being opened. | 203 // Find file handler from the platform app for the file being opened. |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 void RestartPlatformAppWithFileEntries( | 414 void RestartPlatformAppWithFileEntries( |
| 420 Profile* profile, | 415 Profile* profile, |
| 421 const Extension* extension, | 416 const Extension* extension, |
| 422 const std::vector<SavedFileEntry>& file_entries) { | 417 const std::vector<SavedFileEntry>& file_entries) { |
| 423 scoped_refptr<SavedFileEntryLauncher> launcher = new SavedFileEntryLauncher( | 418 scoped_refptr<SavedFileEntryLauncher> launcher = new SavedFileEntryLauncher( |
| 424 profile, extension, file_entries); | 419 profile, extension, file_entries); |
| 425 launcher->Launch(); | 420 launcher->Launch(); |
| 426 } | 421 } |
| 427 | 422 |
| 428 } // namespace extensions | 423 } // namespace extensions |
| OLD | NEW |