Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "chrome/browser/web_applications/web_app.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.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"]; | |
| 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]; | |
|
Mark Mentovai
2012/02/08 05:53:26
atomically:NO is bad.
I don’t like rewriting Info
sail
2012/02/08 06:12:51
Unfortunately I think there are some standard Info
| |
| 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); | |
|
Mark Mentovai
2012/02/08 05:53:26
This would almost certainly be better if you built
sail
2012/02/08 06:12:51
Ahh, I didn't realize I was doing a copy. I think
| |
| 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 | |
| OLD | NEW |