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

Side by Side Diff: chrome/browser/web_applications/web_app_mac.mm

Issue 9346013: Publish app shortcuts on Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove logging Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "chrome/browser/web_applications/web_app.h"
8
9 #include "base/file_util.h"
10 #include "base/mac/bundle_locations.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/sys_string_conversions.h"
14 #include "chrome/common/mac/app_mode_common.h"
15 #include "grit/chromium_strings.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
17
18 namespace {
19
20 FilePath GetAppLoaderPath() {
21 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME)
22 stringByAppendingString:@" App Mode Loader.app"];
jeremy 2012/02/07 08:29:27 This CL is based on top of the temporary CL I sent
sail 2012/02/07 18:17:06 Yea, currently this code will fail but it should b
23 return base::mac::PathForFrameworkBundleResource((CFStringRef)app_loader);
24 }
25
26 NSString* FilePathToNSString(const FilePath& file_path) {
jeremy 2012/02/07 08:29:27 Can this live somewhere in base/mac/ ? I've got an
sail 2012/02/07 18:17:06 Done.
27 return [[NSFileManager defaultManager]
28 stringWithFileSystemRepresentation:file_path.value().c_str()
29 length:file_path.value().size()];
30 }
31
32 bool IsWritable(const FilePath& path) {
33 return [[NSFileManager defaultManager] isWritableFileAtPath:
34 FilePathToNSString(path)];
35 }
36
37 FilePath GetDstPath() {
38 FilePath path;
39 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) &&
40 IsWritable(path)) {
41 return path;
42 }
43
44 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path))
45 return path;
46
47 return FilePath();
48 }
49
50 bool UpdatePlist(const FilePath& app_path,
51 const ShellIntegration::ShortcutInfo& info) {
52 NSString* plist_path =
53 FilePathToNSString(app_path.Append("Contents").Append("Info.plist"));
54 NSMutableDictionary* dict =
55 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path];
56
57 [dict setObject:base::SysUTF8ToNSString(info.extension_id)
58 forKey:app_mode::kCrAppModeShortcutIDKey];
59 [dict setObject:base::SysUTF16ToNSString(info.title)
60 forKey:app_mode::kCrAppModeShortcutNameKey];
61 [dict setObject:base::SysUTF8ToNSString(info.url.spec())
62 forKey:app_mode::kCrAppModeShortcutURLKey];
63 return [dict writeToFile:plist_path atomically:NO];
64 }
65
66 bool UpdateIcon() {
67 // TODO:(sail) Need to implement this.
68 return true;
69 }
70
71 } // namespace
72
73 namespace web_app {
74
75 namespace internals {
76
77 void CreateShortcutTask(const FilePath& web_app_path,
78 const FilePath& profile_path,
79 const ShellIntegration::ShortcutInfo& shortcut_info) {
80 FilePath app_name = GetSanitizedFileName(shortcut_info.title);
81 FilePath app_file_name = app_name.ReplaceExtension("app");
82 ScopedTempDir scoped_temp_dir;
83 if (!scoped_temp_dir.CreateUniqueTempDir())
84 return;
85 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name);
86
87 // Update the app's plist and icon in a temp directory. This works around
88 // a Finder bug where the app's icon doesn't properly update.
89 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) {
90 LOG(ERROR) << "Copying app to staging path: " << staging_path.value()
91 << " failed";
92 return;
93 }
94
95 if (!UpdatePlist(staging_path, shortcut_info))
96 return;
97
98 if (!UpdateIcon())
99 return;
100
101 FilePath dst_path = GetDstPath().Append(app_file_name);
102 if (!file_util::CopyDirectory(staging_path, dst_path, true)) {
103 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed";
104 return;
105 }
106
107 [[NSWorkspace sharedWorkspace]
108 selectFile:FilePathToNSString(dst_path)
109 inFileViewerRootedAtPath:nil];
110 }
111
112 } // namespace internals
113
114 } // namespace web_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698