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

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: address review comments 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"
Mark Mentovai 2012/02/08 04:29:04 Shouldn’t this come first, before <Cocoa/Cocoa.h>?
sail 2012/02/08 05:21:38 Done.
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"];
23 return base::mac::PathForFrameworkBundleResource(
24 base::mac::NSToCFCast(app_loader));
25 }
26
27 bool IsWritable(const FilePath& path) {
28 return [[NSFileManager defaultManager] isWritableFileAtPath:
29 base::mac::FilePathToNSString(path)];
30 }
31
32 FilePath GetDestinationPath() {
33 FilePath path;
34 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) &&
35 IsWritable(path)) {
36 return path;
37 }
38
39 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path))
40 return path;
41
42 return FilePath();
43 }
44
45 bool UpdatePlist(const FilePath& app_path,
46 const ShellIntegration::ShortcutInfo& info) {
47 NSString* plist_path = base::mac::FilePathToNSString(
48 app_path.Append("Contents").Append("Info.plist"));
49 NSMutableDictionary* dict =
50 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path];
51
52 [dict setObject:base::SysUTF8ToNSString(info.extension_id)
53 forKey:app_mode::kCrAppModeShortcutIDKey];
54 [dict setObject:base::SysUTF16ToNSString(info.title)
55 forKey:app_mode::kCrAppModeShortcutNameKey];
56 [dict setObject:base::SysUTF8ToNSString(info.url.spec())
57 forKey:app_mode::kCrAppModeShortcutURLKey];
58 return [dict writeToFile:plist_path atomically:NO];
59 }
60
61 bool UpdateIcon() {
62 // TODO:(sail) Need to implement this.
63 return true;
64 }
65
66 } // namespace
67
68 namespace web_app {
69
70 namespace internals {
71
72 void CreateShortcutTask(const FilePath& web_app_path,
73 const FilePath& profile_path,
74 const ShellIntegration::ShortcutInfo& shortcut_info) {
75 FilePath app_name = GetSanitizedFileName(shortcut_info.title);
76 FilePath app_file_name = app_name.ReplaceExtension("app");
77 ScopedTempDir scoped_temp_dir;
78 if (!scoped_temp_dir.CreateUniqueTempDir())
79 return;
80 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name);
81
82 // Update the app's plist and icon in a temp directory. This works around
83 // a Finder bug where the app's icon doesn't properly update.
84 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) {
85 LOG(ERROR) << "Copying app to staging path: " << staging_path.value()
86 << " failed";
87 return;
88 }
89
90 if (!UpdatePlist(staging_path, shortcut_info))
91 return;
92
93 if (!UpdateIcon())
94 return;
95
96 FilePath dst_path = GetDestinationPath().Append(app_file_name);
97 if (!file_util::CopyDirectory(staging_path, dst_path, true)) {
98 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed";
99 return;
100 }
101
102 [[NSWorkspace sharedWorkspace]
103 selectFile:base::mac::FilePathToNSString(dst_path)
104 inFileViewerRootedAtPath:nil];
105 }
106
107 } // namespace internals
108
109 } // namespace web_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698