[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 5 | #include "apps/launcher.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 6 | |
[email protected] | bdb8a1e | 2013-10-03 08:14:00 | [diff] [blame] | 7 | #include "apps/apps_client.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/memory/ref_counted.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 6e22243 | 2012-08-14 22:12:58 | [diff] [blame] | 15 | #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
[email protected] | e3b1938 | 2013-01-12 03:58:14 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 17 | #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 18 | #include "chrome/browser/extensions/event_router.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_prefs.h" |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 24 | #include "chrome/common/extensions/api/app_runtime.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 25 | #include "chrome/common/extensions/extension_messages.h" |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 26 | #include "chrome/common/extensions/manifest_handlers/kiosk_mode_info.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 28 | #include "content/public/browser/render_process_host.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 29 | #include "content/public/browser/web_contents.h" |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 30 | #include "extensions/browser/lazy_background_task_queue.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 31 | #include "extensions/browser/process_manager.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame^] | 32 | #include "extensions/common/extension.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 33 | #include "net/base/mime_util.h" |
| 34 | #include "net/base/net_util.h" |
[email protected] | 43197ea2 | 2013-09-10 15:31:56 | [diff] [blame] | 35 | #include "url/gurl.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 36 | |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 37 | #if defined(OS_CHROMEOS) |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 38 | #include "chrome/browser/chromeos/drive/file_errors.h" |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 39 | #include "chrome/browser/chromeos/drive/file_system_interface.h" |
[email protected] | 4fa2fd5d | 2013-04-26 03:42:52 | [diff] [blame] | 40 | #include "chrome/browser/chromeos/drive/file_system_util.h" |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 41 | #include "chrome/browser/chromeos/login/user_manager.h" |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 42 | #endif |
| 43 | |
[email protected] | c1dbcb1 | 2013-03-25 00:23:34 | [diff] [blame] | 44 | #if defined(OS_WIN) |
| 45 | #include "win8/util/win8_util.h" |
| 46 | #endif |
| 47 | |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 48 | namespace app_runtime = extensions::api::app_runtime; |
| 49 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 50 | using content::BrowserThread; |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 51 | using extensions::app_file_handler_util::CheckWritableFiles; |
[email protected] | e3b1938 | 2013-01-12 03:58:14 | [diff] [blame] | 52 | using extensions::app_file_handler_util::FileHandlerForId; |
[email protected] | 99b9204 | 2013-05-03 11:26:38 | [diff] [blame] | 53 | using extensions::app_file_handler_util::FileHandlerCanHandleFile; |
| 54 | using extensions::app_file_handler_util::FirstFileHandlerForFile; |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 55 | using extensions::app_file_handler_util::CreateFileEntry; |
| 56 | using extensions::app_file_handler_util::GrantedFileEntry; |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 57 | using extensions::app_file_handler_util::HasFileSystemWritePermission; |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 58 | using extensions::Extension; |
| 59 | using extensions::ExtensionHost; |
| 60 | using extensions::ExtensionSystem; |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 61 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 62 | namespace apps { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 63 | |
| 64 | namespace { |
| 65 | |
[email protected] | 9be0bad | 2013-04-18 05:51:36 | [diff] [blame] | 66 | const char kFallbackMimeType[] = "application/octet-stream"; |
| 67 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 68 | bool MakePathAbsolute(const base::FilePath& current_directory, |
| 69 | base::FilePath* file_path) { |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 70 | DCHECK(file_path); |
| 71 | if (file_path->IsAbsolute()) |
| 72 | return true; |
| 73 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 74 | if (current_directory.empty()) { |
| 75 | *file_path = base::MakeAbsoluteFilePath(*file_path); |
| 76 | return !file_path->empty(); |
| 77 | } |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 78 | |
| 79 | if (!current_directory.IsAbsolute()) |
| 80 | return false; |
| 81 | |
| 82 | *file_path = current_directory.Append(*file_path); |
| 83 | return true; |
| 84 | } |
| 85 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 86 | bool GetAbsolutePathFromCommandLine(const CommandLine* command_line, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 87 | const base::FilePath& current_directory, |
| 88 | base::FilePath* path) { |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 89 | if (!command_line || !command_line->GetArgs().size()) |
| 90 | return false; |
| 91 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 92 | base::FilePath relative_path(command_line->GetArgs()[0]); |
| 93 | base::FilePath absolute_path(relative_path); |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 94 | if (!MakePathAbsolute(current_directory, &absolute_path)) { |
| 95 | LOG(WARNING) << "Cannot make absolute path from " << relative_path.value(); |
| 96 | return false; |
| 97 | } |
| 98 | *path = absolute_path; |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Helper method to launch the platform app |extension| with no data. This |
| 103 | // should be called in the fallback case, where it has been impossible to |
| 104 | // load or obtain file launch data. |
| 105 | void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { |
| 106 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 107 | extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension); |
| 108 | } |
| 109 | |
| 110 | // Class to handle launching of platform apps to open a specific path. |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 111 | // An instance of this class is created for each launch. The lifetime of these |
| 112 | // instances is managed by reference counted pointers. As long as an instance |
| 113 | // has outstanding tasks on a message queue it will be retained; once all |
| 114 | // outstanding tasks are completed it will be deleted. |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 115 | class PlatformAppPathLauncher |
| 116 | : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 117 | public: |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 118 | PlatformAppPathLauncher(Profile* profile, |
| 119 | const Extension* extension, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 120 | const base::FilePath& file_path) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 121 | : profile_(profile), extension_(extension), file_path_(file_path) {} |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 122 | |
| 123 | void Launch() { |
| 124 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 125 | if (file_path_.empty()) { |
| 126 | LaunchPlatformAppWithNoData(profile_, extension_); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 130 | DCHECK(file_path_.IsAbsolute()); |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 131 | |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 132 | if (HasFileSystemWritePermission(extension_)) { |
| 133 | std::vector<base::FilePath> paths; |
| 134 | paths.push_back(file_path_); |
| 135 | CheckWritableFiles( |
| 136 | paths, |
| 137 | profile_, |
[email protected] | 6b7ecdd | 2013-08-29 14:16:24 | [diff] [blame] | 138 | false, |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 139 | base::Bind(&PlatformAppPathLauncher::OnFileValid, this), |
| 140 | base::Bind(&PlatformAppPathLauncher::OnFileInvalid, this)); |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 141 | return; |
| 142 | } |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 143 | |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 144 | OnFileValid(); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 147 | void LaunchWithHandler(const std::string& handler_id) { |
| 148 | handler_id_ = handler_id; |
| 149 | Launch(); |
| 150 | } |
| 151 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 152 | private: |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 153 | friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 154 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 155 | virtual ~PlatformAppPathLauncher() {} |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 156 | |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 157 | void OnFileValid() { |
| 158 | #if defined(OS_CHROMEOS) |
| 159 | if (drive::util::IsUnderDriveMountPoint(file_path_)) { |
| 160 | PlatformAppPathLauncher::GetMimeTypeAndLaunchForDriveFile(); |
| 161 | return; |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | BrowserThread::PostTask( |
| 166 | BrowserThread::FILE, |
| 167 | FROM_HERE, |
| 168 | base::Bind(&PlatformAppPathLauncher::GetMimeTypeAndLaunch, this)); |
| 169 | } |
| 170 | |
| 171 | void OnFileInvalid(const base::FilePath& /* error_path */) { |
| 172 | LaunchWithNoLaunchData(); |
| 173 | } |
| 174 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 175 | void GetMimeTypeAndLaunch() { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 176 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 177 | |
| 178 | // If the file doesn't exist, or is a directory, launch with no launch data. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 179 | if (!base::PathExists(file_path_) || |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 180 | base::DirectoryExists(file_path_)) { |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 181 | LOG(WARNING) << "No file exists with path " << file_path_.value(); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 182 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 183 | &PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | std::string mime_type; |
[email protected] | 9be0bad | 2013-04-18 05:51:36 | [diff] [blame] | 188 | if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) |
| 189 | mime_type = kFallbackMimeType; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 190 | |
| 191 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 192 | &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 195 | #if defined(OS_CHROMEOS) |
| 196 | void GetMimeTypeAndLaunchForDriveFile() { |
| 197 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 198 | |
[email protected] | 473a336d | 2013-09-09 10:28:51 | [diff] [blame] | 199 | drive::FileSystemInterface* file_system = |
| 200 | drive::util::GetFileSystemByProfile(profile_); |
| 201 | if (!file_system) { |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 202 | LaunchWithNoLaunchData(); |
| 203 | return; |
| 204 | } |
| 205 | |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 206 | file_system->GetFile( |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 207 | drive::util::ExtractDrivePath(file_path_), |
| 208 | base::Bind(&PlatformAppPathLauncher::OnGotDriveFile, this)); |
| 209 | } |
| 210 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 211 | void OnGotDriveFile(drive::FileError error, |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 212 | const base::FilePath& file_path, |
[email protected] | ce5b515 | 2013-05-08 07:27:10 | [diff] [blame] | 213 | scoped_ptr<drive::ResourceEntry> entry) { |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 214 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 215 | |
[email protected] | ce5b515 | 2013-05-08 07:27:10 | [diff] [blame] | 216 | if (error != drive::FILE_ERROR_OK || |
| 217 | !entry || entry->file_specific_info().is_hosted_document()) { |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 218 | LaunchWithNoLaunchData(); |
| 219 | return; |
| 220 | } |
| 221 | |
[email protected] | ce5b515 | 2013-05-08 07:27:10 | [diff] [blame] | 222 | const std::string& mime_type = |
| 223 | entry->file_specific_info().content_mime_type(); |
[email protected] | 9be0bad | 2013-04-18 05:51:36 | [diff] [blame] | 224 | LaunchWithMimeType(mime_type.empty() ? kFallbackMimeType : mime_type); |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 225 | } |
| 226 | #endif // defined(OS_CHROMEOS) |
| 227 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 228 | void LaunchWithNoLaunchData() { |
| 229 | // This method is required as an entry point on the UI thread. |
| 230 | LaunchPlatformAppWithNoData(profile_, extension_); |
| 231 | } |
| 232 | |
| 233 | void LaunchWithMimeType(const std::string& mime_type) { |
[email protected] | 8427a062 | 2013-02-11 17:00:57 | [diff] [blame] | 234 | // Find file handler from the platform app for the file being opened. |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 235 | const extensions::FileHandlerInfo* handler = NULL; |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 236 | if (!handler_id_.empty()) |
| 237 | handler = FileHandlerForId(*extension_, handler_id_); |
| 238 | else |
[email protected] | 99b9204 | 2013-05-03 11:26:38 | [diff] [blame] | 239 | handler = FirstFileHandlerForFile(*extension_, mime_type, file_path_); |
| 240 | if (handler && !FileHandlerCanHandleFile(*handler, mime_type, file_path_)) { |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 241 | LOG(WARNING) << "Extension does not provide a valid file handler for " |
| 242 | << file_path_.value(); |
| 243 | LaunchWithNoLaunchData(); |
| 244 | return; |
| 245 | } |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 246 | |
[email protected] | 8427a062 | 2013-02-11 17:00:57 | [diff] [blame] | 247 | // If this app doesn't have a file handler that supports the file, launch |
| 248 | // with no launch data. |
| 249 | if (!handler) { |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 250 | LOG(WARNING) << "Extension does not provide a valid file handler for " |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 251 | << file_path_.value(); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 252 | LaunchWithNoLaunchData(); |
| 253 | return; |
| 254 | } |
| 255 | |
[email protected] | 56573d7 | 2013-05-09 06:36:37 | [diff] [blame] | 256 | if (handler_id_.empty()) |
| 257 | handler_id_ = handler->id; |
| 258 | |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 259 | // Access needs to be granted to the file for the process associated with |
| 260 | // the extension. To do this the ExtensionHost is needed. This might not be |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 261 | // available, or it might be in the process of being unloaded, in which case |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 262 | // the lazy background task queue is used to load the extension and then |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 263 | // call back to us. |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 264 | extensions::LazyBackgroundTaskQueue* queue = |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 265 | ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
| 266 | if (queue->ShouldEnqueueTask(profile_, extension_)) { |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 267 | queue->AddPendingTask(profile_, extension_->id(), base::Bind( |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 268 | &PlatformAppPathLauncher::GrantAccessToFileAndLaunch, |
| 269 | this, mime_type)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 273 | extensions::ProcessManager* process_manager = |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 274 | ExtensionSystem::Get(profile_)->process_manager(); |
[email protected] | e3b1938 | 2013-01-12 03:58:14 | [diff] [blame] | 275 | ExtensionHost* host = |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 276 | process_manager->GetBackgroundHostForExtension(extension_->id()); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 277 | DCHECK(host); |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 278 | GrantAccessToFileAndLaunch(mime_type, host); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 281 | void GrantAccessToFileAndLaunch(const std::string& mime_type, |
[email protected] | e3b1938 | 2013-01-12 03:58:14 | [diff] [blame] | 282 | ExtensionHost* host) { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 283 | // If there was an error loading the app page, |host| will be NULL. |
| 284 | if (!host) { |
| 285 | LOG(ERROR) << "Could not load app page for " << extension_->id(); |
| 286 | return; |
| 287 | } |
| 288 | |
[email protected] | 6b7ecdd | 2013-08-29 14:16:24 | [diff] [blame] | 289 | GrantedFileEntry file_entry = |
| 290 | CreateFileEntry(profile_, |
| 291 | extension_, |
| 292 | host->render_process_host()->GetID(), |
| 293 | file_path_, |
| 294 | false); |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 295 | extensions::AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
[email protected] | e58eb5c | 2013-05-13 09:42:28 | [diff] [blame] | 296 | profile_, extension_, handler_id_, mime_type, file_entry); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 299 | // The profile the app should be run in. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 300 | Profile* profile_; |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 301 | // The extension providing the app. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 302 | const Extension* extension_; |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 303 | // The path to be passed through to the app. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 304 | const base::FilePath file_path_; |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 305 | // The ID of the file handler used to launch the app. |
| 306 | std::string handler_id_; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 307 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 308 | DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 309 | }; |
| 310 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 311 | } // namespace |
| 312 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 313 | void LaunchPlatformAppWithCommandLine(Profile* profile, |
| 314 | const Extension* extension, |
| 315 | const CommandLine* command_line, |
| 316 | const base::FilePath& current_directory) { |
[email protected] | bdb8a1e | 2013-10-03 08:14:00 | [diff] [blame] | 317 | if (!AppsClient::Get()->CheckAppLaunch(profile, extension)) |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 318 | return; |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 319 | |
| 320 | // An app with "kiosk_only" should not be installed and launched |
| 321 | // outside of ChromeOS kiosk mode in the first place. This is a defensive |
| 322 | // check in case this scenario does occur. |
| 323 | if (extensions::KioskModeInfo::IsKioskOnly(extension)) { |
| 324 | bool in_kiosk_mode = false; |
| 325 | #if defined(OS_CHROMEOS) |
| 326 | chromeos::UserManager* user_manager = chromeos::UserManager::Get(); |
| 327 | in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp(); |
| 328 | #endif |
| 329 | if (!in_kiosk_mode) { |
| 330 | LOG(ERROR) << "App with 'kiosk_only' attribute must be run in " |
| 331 | << " ChromeOS kiosk mode."; |
| 332 | NOTREACHED(); |
[email protected] | 8a01117 | 2013-08-09 04:29:23 | [diff] [blame] | 333 | return; |
| 334 | } |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 335 | } |
[email protected] | 8a01117 | 2013-08-09 04:29:23 | [diff] [blame] | 336 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 337 | base::FilePath path; |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 338 | if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { |
| 339 | LaunchPlatformAppWithNoData(profile, extension); |
| 340 | return; |
| 341 | } |
| 342 | |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 343 | // TODO(benwells): add a command-line argument to provide a handler ID. |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 344 | LaunchPlatformAppWithPath(profile, extension, path); |
| 345 | } |
| 346 | |
| 347 | void LaunchPlatformAppWithPath(Profile* profile, |
| 348 | const Extension* extension, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 349 | const base::FilePath& file_path) { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 350 | // launcher will be freed when nothing has a reference to it. The message |
| 351 | // queue will retain a reference for any outstanding task, so when the |
| 352 | // launcher has finished it will be freed. |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 353 | scoped_refptr<PlatformAppPathLauncher> launcher = |
| 354 | new PlatformAppPathLauncher(profile, extension, file_path); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 355 | launcher->Launch(); |
| 356 | } |
| 357 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 358 | void LaunchPlatformApp(Profile* profile, const Extension* extension) { |
| 359 | LaunchPlatformAppWithCommandLine(profile, extension, NULL, base::FilePath()); |
| 360 | } |
| 361 | |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 362 | void LaunchPlatformAppWithFileHandler(Profile* profile, |
| 363 | const Extension* extension, |
| 364 | const std::string& handler_id, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 365 | const base::FilePath& file_path) { |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 366 | scoped_refptr<PlatformAppPathLauncher> launcher = |
| 367 | new PlatformAppPathLauncher(profile, extension, file_path); |
| 368 | launcher->LaunchWithHandler(handler_id); |
| 369 | } |
| 370 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 371 | void RestartPlatformApp(Profile* profile, const Extension* extension) { |
[email protected] | af59f32 | 2013-08-07 06:10:10 | [diff] [blame] | 372 | #if defined(OS_WIN) |
| 373 | // On Windows 8's single window Metro mode we can not launch platform apps. |
| 374 | // In restart we are just making sure launch doesn't slip through. |
| 375 | if (win8::IsSingleWindowMetroMode()) |
| 376 | return; |
| 377 | #endif |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 378 | extensions::EventRouter* event_router = |
| 379 | ExtensionSystem::Get(profile)->event_router(); |
| 380 | bool listening_to_restart = event_router-> |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 381 | ExtensionHasEventListener(extension->id(), |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 382 | app_runtime::OnRestarted::kEventName); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 383 | |
| 384 | if (listening_to_restart) { |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 385 | extensions::AppEventRouter::DispatchOnRestartedEvent(profile, extension); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 389 | extensions::ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile)-> |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 390 | extension_service()->extension_prefs(); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 391 | bool had_windows = extension_prefs->IsActive(extension->id()); |
| 392 | extension_prefs->SetIsActive(extension->id(), false); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 393 | bool listening_to_launch = event_router-> |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 394 | ExtensionHasEventListener(extension->id(), |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 395 | app_runtime::OnLaunched::kEventName); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 396 | |
| 397 | if (listening_to_launch && had_windows) |
| 398 | LaunchPlatformAppWithNoData(profile, extension); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | 43197ea2 | 2013-09-10 15:31:56 | [diff] [blame] | 401 | void LaunchPlatformAppWithUrl(Profile* profile, |
| 402 | const Extension* extension, |
| 403 | const std::string& handler_id, |
| 404 | const GURL& url, |
| 405 | const GURL& referrer_url) { |
| 406 | extensions::AppEventRouter::DispatchOnLaunchedEventWithUrl( |
| 407 | profile, extension, handler_id, url, referrer_url); |
| 408 | } |
| 409 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 410 | } // namespace apps |