OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "apps/app_shim/app_shim_util.h" | |
6 | |
7 #include "apps/app_shim/switches.h" | |
8 #include "base/command_line.h" | |
9 #include "base/file_util.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/mac/foundation_util.h" | |
12 #include "base/mac/launch_services_util.h" | |
13 #include "base/strings/sys_string_conversions.h" | |
14 #include "chrome/common/mac/app_mode_common.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 namespace { | |
20 | |
21 const char kAppDirName[] = "Chrome Apps.localized"; | |
tapted
2013/05/16 04:51:00
maybe now is a good time to discuss with benwells
| |
22 | |
23 base::FilePath GetWritableApplicationsDirectory() { | |
24 base::FilePath path; | |
25 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | |
26 file_util::PathIsWritable(path)) { | |
27 return path; | |
28 } | |
29 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | |
30 return path; | |
31 return base::FilePath(); | |
32 } | |
33 | |
34 base::FilePath GetPathToShim(const std::string& app_id, | |
tapted
2013/05/16 04:51:00
Maybe we should consider calling this extension_id
| |
35 const std::string& profile_name) { | |
36 base::FilePath app_dir = app_shim::GetAppShimFolder(); | |
37 if (app_dir.empty()) | |
38 return base::FilePath(); | |
39 | |
40 file_util::FileEnumerator enumerator(app_dir, | |
tapted
2013/05/16 04:51:00
perhaps a note to say why it's iterating (i.e. bec
| |
41 false /* recursive */, | |
42 file_util::FileEnumerator::DIRECTORIES); | |
43 base::FilePath shim_path = enumerator.Next(); | |
44 for (; !shim_path.empty(); shim_path = enumerator.Next()) { | |
45 base::FilePath plist_path = | |
46 shim_path.Append("Contents").Append("Info.plist"); | |
47 NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile: | |
48 base::mac::FilePathToNSString(plist_path)]; | |
49 std::string plist_id = base::SysNSStringToUTF8( | |
50 [plist objectForKey:app_mode::kCrAppModeShortcutIDKey]); | |
51 std::string plist_profile = base::SysNSStringToUTF8( | |
52 [plist objectForKey:app_mode::kCrAppModeProfileDirKey]); | |
53 | |
54 if (plist_id == app_id && plist_profile == profile_name) { | |
55 break; | |
56 } | |
tapted
2013/05/16 04:51:00
nit: doesn't need curlies
| |
57 } | |
58 return shim_path; | |
59 } | |
60 | |
61 void FindAndLaunchShimOnFileThread( | |
62 const std::string& app_id, | |
tapted
2013/05/16 04:51:00
nit: align these with the open paren (like for Fin
| |
63 const std::string& profile_name, | |
64 base::Callback<void(bool)> completed) { | |
65 base::FilePath shim_path = GetPathToShim(app_id, profile_name); | |
tapted
2013/05/16 04:51:00
nit: before this,
DCHECK(content::BrowserThread
| |
66 if (shim_path.empty()) { | |
67 BrowserThread::PostTask( | |
68 BrowserThread::UI, FROM_HERE, base::Bind(completed, false)); | |
69 return; | |
70 } | |
tapted
2013/05/16 04:51:00
nit: blank line after for early return
| |
71 CommandLine command_line(CommandLine::NO_PROGRAM); | |
72 command_line.AppendSwitch(app_shim::switches::kNoLaunchApp); | |
73 base::mac::OpenApplicationWithPath(shim_path, command_line, NULL); | |
tapted
2013/05/16 04:51:00
Do you see a reason why shim_path can't be derived
| |
74 BrowserThread::PostTask( | |
75 BrowserThread::UI, FROM_HERE, base::Bind(completed, true)); | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 namespace app_shim { | |
81 | |
82 base::FilePath GetAppShimFolder() { | |
83 base::FilePath path = GetWritableApplicationsDirectory(); | |
tapted
2013/05/16 04:51:00
GetWritableApplicationsDirectory might need to be
| |
84 return path.empty() ? path : path.Append(kAppDirName); | |
85 } | |
86 | |
87 void FindAndLaunchShimForApp( | |
88 const std::string& app_id, | |
tapted
2013/05/16 04:51:00
nit: align these like the header
| |
89 const std::string& profile_name, | |
90 base::Callback<void(bool)> completed) { | |
91 BrowserThread::PostTask( | |
92 BrowserThread::FILE, FROM_HERE, | |
93 base::Bind(&FindAndLaunchShimOnFileThread, | |
94 app_id, profile_name, completed)); | |
95 } | |
96 | |
97 } // namespace app_shim | |
OLD | NEW |