Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1354)

Side by Side Diff: chrome/browser/extensions/platform_app_launcher.cc

Issue 10919058: Refactor PlatformAppCommandLineLauncher into PlatformAppPathLauncher (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: web_intents::action::k* => web_intents::kAction* Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" 14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
15 #include "chrome/browser/extensions/extension_host.h" 15 #include "chrome/browser/extensions/extension_host.h"
16 #include "chrome/browser/extensions/extension_process_manager.h" 16 #include "chrome/browser/extensions/extension_process_manager.h"
17 #include "chrome/browser/extensions/extension_system.h" 17 #include "chrome/browser/extensions/extension_system.h"
18 #include "chrome/browser/extensions/lazy_background_task_queue.h" 18 #include "chrome/browser/extensions/lazy_background_task_queue.h"
19 #include "chrome/browser/intents/web_intents_util.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/extensions/extension.h" 21 #include "chrome/common/extensions/extension.h"
21 #include "chrome/common/extensions/extension_messages.h" 22 #include "chrome/common/extensions/extension_messages.h"
22 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/child_process_security_policy.h" 24 #include "content/public/browser/child_process_security_policy.h"
24 #include "content/public/browser/render_process_host.h" 25 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_intents_dispatcher.h" 27 #include "content/public/browser/web_intents_dispatcher.h"
27 #include "net/base/mime_util.h" 28 #include "net/base/mime_util.h"
28 #include "net/base/net_util.h" 29 #include "net/base/net_util.h"
29 #include "webkit/fileapi/file_system_types.h" 30 #include "webkit/fileapi/file_system_types.h"
30 #include "webkit/fileapi/isolated_context.h" 31 #include "webkit/fileapi/isolated_context.h"
31 #include "webkit/glue/web_intent_data.h" 32 #include "webkit/glue/web_intent_data.h"
32 #include "webkit/glue/web_intent_service_data.h" 33 #include "webkit/glue/web_intent_service_data.h"
33 34
34 using content::BrowserThread; 35 using content::BrowserThread;
35 36
36 namespace extensions { 37 namespace extensions {
37 38
38 namespace { 39 namespace {
39 40
40 const char kViewIntent[] = "http://webintents.org/view";
41
42 bool MakePathAbsolute(const FilePath& current_directory, 41 bool MakePathAbsolute(const FilePath& current_directory,
43 FilePath* file_path) { 42 FilePath* file_path) {
44 DCHECK(file_path); 43 DCHECK(file_path);
45 if (file_path->IsAbsolute()) 44 if (file_path->IsAbsolute())
46 return true; 45 return true;
47 46
48 if (current_directory.empty()) 47 if (current_directory.empty())
49 return file_util::AbsolutePath(file_path); 48 return file_util::AbsolutePath(file_path);
50 49
51 if (!current_directory.IsAbsolute()) 50 if (!current_directory.IsAbsolute())
52 return false; 51 return false;
53 52
54 *file_path = current_directory.Append(*file_path); 53 *file_path = current_directory.Append(*file_path);
55 return true; 54 return true;
56 } 55 }
57 56
58 // Class to handle launching of platform apps with command line information. 57 bool GetAbsolutePathFromCommandLine(const CommandLine* command_line,
58 const FilePath& current_directory,
59 FilePath* path) {
60 if (!command_line || !command_line->GetArgs().size())
61 return false;
62
63 FilePath relative_path(command_line->GetArgs()[0]);
64 FilePath absolute_path(relative_path);
65 if (!MakePathAbsolute(current_directory, &absolute_path)) {
66 LOG(WARNING) << "Cannot make absolute path from " << relative_path.value();
67 return false;
68 }
69 *path = absolute_path;
70 return true;
71 }
72
73 // Helper method to launches the platform app |extension| with no data. This
benwells 2012/09/04 04:41:32 launches => launch
thorogood 2012/09/04 05:26:00 Done.
74 // should be called in the fallback case for the launchers above.
benwells 2012/09/04 04:41:32 Above?
thorogood 2012/09/04 05:26:00 I lazily moved this method to the top of the file
75 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
78 }
79
80 // Class to handle launching of platform apps to open a specific path.
59 // An instance of this class is created for each launch. The lifetime of these 81 // An instance of this class is created for each launch. The lifetime of these
60 // instances is managed by reference counted pointers. As long as an instance 82 // instances is managed by reference counted pointers. As long as an instance
61 // has outstanding tasks on a message queue it will be retained; once all 83 // has outstanding tasks on a message queue it will be retained; once all
62 // outstanding tasks are completed it will be deleted. 84 // outstanding tasks are completed it will be deleted.
63 class PlatformAppCommandLineLauncher 85 class PlatformAppPathLauncher
64 : public base::RefCountedThreadSafe<PlatformAppCommandLineLauncher> { 86 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
65 public: 87 public:
66 PlatformAppCommandLineLauncher(Profile* profile, 88 PlatformAppPathLauncher(Profile* profile,
67 const Extension* extension, 89 const Extension* extension,
68 const CommandLine* command_line, 90 const FilePath& file_path)
69 const FilePath& current_directory)
70 : profile_(profile), 91 : profile_(profile),
71 extension_(extension), 92 extension_(extension),
72 command_line_(command_line), 93 file_path_(file_path) {}
73 current_directory_(current_directory) {}
74 94
75 void Launch() { 95 void Launch() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 if (!command_line_ || !command_line_->GetArgs().size()) { 97 if (file_path_.empty()) {
78 LaunchWithNoLaunchData(); 98 LaunchPlatformAppWithNoData(profile_, extension_);
79 return; 99 return;
80 } 100 }
81 101
82 FilePath file_path(command_line_->GetArgs()[0]); 102 DCHECK(file_path_.IsAbsolute());
83 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 103 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
84 &PlatformAppCommandLineLauncher::GetMimeTypeAndLaunch, 104 &PlatformAppPathLauncher::GetMimeTypeAndLaunch, this));
85 this, file_path));
86 } 105 }
87 106
88 private: 107 private:
89 friend class base::RefCountedThreadSafe<PlatformAppCommandLineLauncher>; 108 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
90 109
91 virtual ~PlatformAppCommandLineLauncher() {} 110 virtual ~PlatformAppPathLauncher() {}
92 111
93 void LaunchWithNoLaunchData() { 112 void GetMimeTypeAndLaunch() {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile_, extension_);
96 }
97
98 void GetMimeTypeAndLaunch(const FilePath& file_path) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
100 114
101 // If we cannot construct an absolute path, launch with no launch data. 115 // If the file doesn't exist, or is a directory, launch with no launch data.
102 FilePath absolute_path(file_path); 116 if (!file_util::PathExists(file_path_) ||
103 if (!MakePathAbsolute(current_directory_, &absolute_path)) { 117 file_util::DirectoryExists(file_path_)) {
104 LOG(WARNING) << "Cannot make absolute path from " << file_path.value(); 118 LOG(WARNING) << "No file exists with path " << file_path_.value();
105 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( 119 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
106 &PlatformAppCommandLineLauncher::LaunchWithNoLaunchData, this)); 120 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this));
107 return;
108 }
109
110 // If the file doesn't exist, or is a directory, launch with no launch data.
111 if (!file_util::PathExists(absolute_path) ||
112 file_util::DirectoryExists(absolute_path)) {
113 LOG(WARNING) << "No file exists with path " << absolute_path.value();
114 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
115 &PlatformAppCommandLineLauncher::LaunchWithNoLaunchData, this));
116 return; 121 return;
117 } 122 }
118 123
119 std::string mime_type; 124 std::string mime_type;
120 // If we cannot obtain the MIME type, launch with no launch data. 125 // If we cannot obtain the MIME type, launch with no launch data.
121 if (!net::GetMimeTypeFromFile(absolute_path, &mime_type)) { 126 if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) {
122 LOG(WARNING) << "Could not obtain MIME type for " 127 LOG(WARNING) << "Could not obtain MIME type for "
123 << absolute_path.value(); 128 << file_path_.value();
124 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( 129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
125 &PlatformAppCommandLineLauncher::LaunchWithNoLaunchData, this)); 130 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this));
126 return; 131 return;
127 } 132 }
128 133
129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
130 &PlatformAppCommandLineLauncher::LaunchWithMimeTypeAndPath, 135 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type));
131 this, absolute_path, mime_type));
132 } 136 }
133 137
134 void LaunchWithMimeTypeAndPath(const FilePath& file_path, 138 void LaunchWithNoLaunchData() {
135 const std::string& mime_type) { 139 // This method is required as an entry point on the UI thread.
140 LaunchPlatformAppWithNoData(profile_, extension_);
141 }
142
143 void LaunchWithMimeType(const std::string& mime_type) {
136 // Find the intent service from the platform app for the file being opened. 144 // Find the intent service from the platform app for the file being opened.
137 webkit_glue::WebIntentServiceData service; 145 webkit_glue::WebIntentServiceData service;
138 bool found_service = false; 146 bool found_service = false;
139 147
140 std::vector<webkit_glue::WebIntentServiceData> services = 148 std::vector<webkit_glue::WebIntentServiceData> services =
141 extension_->intents_services(); 149 extension_->intents_services();
142 for (size_t i = 0; i < services.size(); i++) { 150 for (size_t i = 0; i < services.size(); i++) {
143 std::string service_type_ascii = UTF16ToASCII(services[i].type); 151 std::string service_type_ascii = UTF16ToASCII(services[i].type);
144 if (services[i].action == ASCIIToUTF16(kViewIntent) && 152 if (services[i].action == ASCIIToUTF16(web_intents::kActionView) &&
145 net::MatchesMimeType(service_type_ascii, mime_type)) { 153 net::MatchesMimeType(service_type_ascii, mime_type)) {
146 service = services[i]; 154 service = services[i];
147 found_service = true; 155 found_service = true;
148 break; 156 break;
149 } 157 }
150 } 158 }
151 159
152 // If this app doesn't have an intent that supports the file, launch with 160 // If this app doesn't have an intent that supports the file, launch with
153 // no launch data. 161 // no launch data.
154 if (!found_service) { 162 if (!found_service) {
155 LOG(WARNING) << "Extension does not provide a valid intent for " 163 LOG(WARNING) << "Extension does not provide a valid intent for "
156 << file_path.value(); 164 << file_path_.value();
157 LaunchWithNoLaunchData(); 165 LaunchWithNoLaunchData();
158 return; 166 return;
159 } 167 }
160 168
161 // Access needs to be granted to the file for the process associated with 169 // Access needs to be granted to the file for the process associated with
162 // the extension. To do this the ExtensionHost is needed. This might not be 170 // the extension. To do this the ExtensionHost is needed. This might not be
163 // available, or it might be in the process of being unloaded, in which case 171 // available, or it might be in the process of being unloaded, in which case
164 // the lazy background task queue is used to load the extension and then 172 // the lazy background task queue is used to load the extension and then
165 // call back to us. 173 // call back to us.
166 extensions::LazyBackgroundTaskQueue* queue = 174 extensions::LazyBackgroundTaskQueue* queue =
167 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); 175 ExtensionSystem::Get(profile_)->lazy_background_task_queue();
168 if (queue->ShouldEnqueueTask(profile_, extension_)) { 176 if (queue->ShouldEnqueueTask(profile_, extension_)) {
169 queue->AddPendingTask(profile_, extension_->id(), base::Bind( 177 queue->AddPendingTask(profile_, extension_->id(), base::Bind(
170 &PlatformAppCommandLineLauncher::GrantAccessToFileAndLaunch, 178 &PlatformAppPathLauncher::GrantAccessToFileAndLaunch,
171 this, file_path, mime_type)); 179 this, mime_type));
172 return; 180 return;
173 } 181 }
174 182
175 ExtensionProcessManager* process_manager = 183 ExtensionProcessManager* process_manager =
176 ExtensionSystem::Get(profile_)->process_manager(); 184 ExtensionSystem::Get(profile_)->process_manager();
177 extensions::ExtensionHost* host = 185 extensions::ExtensionHost* host =
178 process_manager->GetBackgroundHostForExtension(extension_->id()); 186 process_manager->GetBackgroundHostForExtension(extension_->id());
179 DCHECK(host); 187 DCHECK(host);
180 GrantAccessToFileAndLaunch(file_path, mime_type, host); 188 GrantAccessToFileAndLaunch(mime_type, host);
181 } 189 }
182 190
183 void GrantAccessToFileAndLaunch(const FilePath& file_path, 191 void GrantAccessToFileAndLaunch(const std::string& mime_type,
184 const std::string& mime_type,
185 extensions::ExtensionHost* host) { 192 extensions::ExtensionHost* host) {
186 // If there was an error loading the app page, |host| will be NULL. 193 // If there was an error loading the app page, |host| will be NULL.
187 if (!host) { 194 if (!host) {
188 LOG(ERROR) << "Could not load app page for " << extension_->id(); 195 LOG(ERROR) << "Could not load app page for " << extension_->id();
189 return; 196 return;
190 } 197 }
191 198
192 content::ChildProcessSecurityPolicy* policy = 199 content::ChildProcessSecurityPolicy* policy =
193 content::ChildProcessSecurityPolicy::GetInstance(); 200 content::ChildProcessSecurityPolicy::GetInstance();
194 int renderer_id = host->render_process_host()->GetID(); 201 int renderer_id = host->render_process_host()->GetID();
195 202
196 // Granting read file permission to allow reading file content. 203 // Granting read file permission to allow reading file content.
197 // If the renderer already has permission to read these paths, it is not 204 // If the renderer already has permission to read these paths, it is not
198 // regranted, as this would overwrite any other permissions which the 205 // regranted, as this would overwrite any other permissions which the
199 // renderer may already have. 206 // renderer may already have.
200 if (!policy->CanReadFile(renderer_id, file_path)) 207 if (!policy->CanReadFile(renderer_id, file_path_))
201 policy->GrantReadFile(renderer_id, file_path); 208 policy->GrantReadFile(renderer_id, file_path_);
202 209
203 std::string registered_name; 210 std::string registered_name;
204 fileapi::IsolatedContext* isolated_context = 211 fileapi::IsolatedContext* isolated_context =
205 fileapi::IsolatedContext::GetInstance(); 212 fileapi::IsolatedContext::GetInstance();
206 DCHECK(isolated_context); 213 DCHECK(isolated_context);
207 std::string filesystem_id = isolated_context->RegisterFileSystemForPath( 214 std::string filesystem_id = isolated_context->RegisterFileSystemForPath(
208 fileapi::kFileSystemTypeNativeLocal, file_path, &registered_name); 215 fileapi::kFileSystemTypeNativeLocal, file_path_, &registered_name);
209 // Granting read file system permission as well to allow file-system 216 // Granting read file system permission as well to allow file-system
210 // read operations. 217 // read operations.
211 policy->GrantReadFileSystem(renderer_id, filesystem_id); 218 policy->GrantReadFileSystem(renderer_id, filesystem_id);
212 219
213 extensions::AppEventRouter::DispatchOnLaunchedEventWithFileEntry( 220 extensions::AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
214 profile_, extension_, ASCIIToUTF16(kViewIntent), filesystem_id, 221 profile_, extension_, ASCIIToUTF16(web_intents::kActionView),
215 registered_name); 222 filesystem_id, registered_name);
216 } 223 }
217 224
218 // The profile the app should be run in. 225 // The profile the app should be run in.
219 Profile* profile_; 226 Profile* profile_;
220 // The extension providing the app. 227 // The extension providing the app.
221 const Extension* extension_; 228 const Extension* extension_;
222 // The command line to be passed through to the app, or NULL. 229 // The path to be passed through to the app. This may be the empty path.
benwells 2012/09/04 04:41:32 It can't be empty any more.
thorogood 2012/09/04 05:26:00 Done.
223 const CommandLine* command_line_; 230 const FilePath file_path_;
224 // If non-empty, this is used to expand relative paths.
225 const FilePath current_directory_;
226 231
227 DISALLOW_COPY_AND_ASSIGN(PlatformAppCommandLineLauncher); 232 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
228 }; 233 };
229 234
230 // Class to handle launching of platform apps with WebIntent data. 235 // Class to handle launching of platform apps with WebIntent data.
231 // An instance of this class is created for each launch. The lifetime of these 236 // An instance of this class is created for each launch. The lifetime of these
232 // instances is managed by reference counted pointers. As long as an instance 237 // instances is managed by reference counted pointers. As long as an instance
233 // has outstanding tasks on a message queue it will be retained; once all 238 // has outstanding tasks on a message queue it will be retained; once all
234 // outstanding tasks are completed it will be deleted. 239 // outstanding tasks are completed it will be deleted.
235 class PlatformAppWebIntentLauncher 240 class PlatformAppWebIntentLauncher
236 : public base::RefCountedThreadSafe<PlatformAppWebIntentLauncher> { 241 : public base::RefCountedThreadSafe<PlatformAppWebIntentLauncher> {
237 public: 242 public:
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 338
334 DISALLOW_COPY_AND_ASSIGN(PlatformAppWebIntentLauncher); 339 DISALLOW_COPY_AND_ASSIGN(PlatformAppWebIntentLauncher);
335 }; 340 };
336 341
337 } // namespace 342 } // namespace
338 343
339 void LaunchPlatformApp(Profile* profile, 344 void LaunchPlatformApp(Profile* profile,
340 const Extension* extension, 345 const Extension* extension,
341 const CommandLine* command_line, 346 const CommandLine* command_line,
342 const FilePath& current_directory) { 347 const FilePath& current_directory) {
348 FilePath path;
349 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) {
350 LaunchPlatformAppWithNoData(profile, extension);
351 return;
352 }
353
354 LaunchPlatformAppWithPath(profile, extension, path);
355 }
356
357 void LaunchPlatformAppWithPath(Profile* profile,
358 const Extension* extension,
359 const FilePath& file_path) {
343 // launcher will be freed when nothing has a reference to it. The message 360 // launcher will be freed when nothing has a reference to it. The message
344 // queue will retain a reference for any outstanding task, so when the 361 // queue will retain a reference for any outstanding task, so when the
345 // launcher has finished it will be freed. 362 // launcher has finished it will be freed.
346 scoped_refptr<PlatformAppCommandLineLauncher> launcher = 363 scoped_refptr<PlatformAppPathLauncher> launcher =
347 new PlatformAppCommandLineLauncher(profile, extension, command_line, 364 new PlatformAppPathLauncher(profile, extension, file_path);
348 current_directory);
349 launcher->Launch(); 365 launcher->Launch();
350 } 366 }
351 367
352 void LaunchPlatformAppWithWebIntent( 368 void LaunchPlatformAppWithWebIntent(
353 Profile* profile, 369 Profile* profile,
354 const Extension* extension, 370 const Extension* extension,
355 content::WebIntentsDispatcher* intents_dispatcher, 371 content::WebIntentsDispatcher* intents_dispatcher,
356 content::WebContents* source) { 372 content::WebContents* source) {
357 scoped_refptr<PlatformAppWebIntentLauncher> launcher = 373 scoped_refptr<PlatformAppWebIntentLauncher> launcher =
358 new PlatformAppWebIntentLauncher( 374 new PlatformAppWebIntentLauncher(
359 profile, extension, intents_dispatcher, source); 375 profile, extension, intents_dispatcher, source);
360 launcher->Launch(); 376 launcher->Launch();
361 } 377 }
362 378
363 } // namespace extensions 379 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698