| OLD | NEW |
| 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "chrome/browser/web_applications/web_app.h" | 7 #include "chrome/browser/web_applications/web_app_mac.h" |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/mac/bundle_locations.h" | 10 #include "base/mac/bundle_locations.h" |
| 11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
| 14 #include "chrome/browser/web_applications/web_app.h" |
| 14 #include "chrome/common/mac/app_mode_common.h" | 15 #include "chrome/common/mac/app_mode_common.h" |
| 15 #include "grit/chromium_strings.h" | 16 #include "grit/chromium_strings.h" |
| 16 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace web_app { |
| 19 | 20 |
| 20 FilePath GetAppLoaderPath() { | 21 WebAppShortcutCreator::WebAppShortcutCreator( |
| 21 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) | 22 const ShellIntegration::ShortcutInfo& shortcut_info) |
| 22 stringByAppendingString:@" App Mode Loader.app"]; | 23 : info_(shortcut_info) { |
| 23 return base::mac::PathForFrameworkBundleResource((CFStringRef)app_loader); | |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool IsWritable(const FilePath& path) { | 26 WebAppShortcutCreator::~WebAppShortcutCreator() { |
| 27 return [[NSFileManager defaultManager] isWritableFileAtPath: | |
| 28 base::mac::FilePathToNSString(path)]; | |
| 29 } | 27 } |
| 30 | 28 |
| 31 FilePath GetDstPath() { | 29 bool WebAppShortcutCreator::CreateShortcut() { |
| 32 FilePath path; | 30 FilePath app_name = internals::GetSanitizedFileName(info_.title); |
| 33 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | |
| 34 IsWritable(path)) { | |
| 35 return path; | |
| 36 } | |
| 37 | |
| 38 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | |
| 39 return path; | |
| 40 | |
| 41 return FilePath(); | |
| 42 } | |
| 43 | |
| 44 bool UpdatePlist(const FilePath& app_path, | |
| 45 const ShellIntegration::ShortcutInfo& info) { | |
| 46 NSString* plist_path = base::mac::FilePathToNSString( | |
| 47 app_path.Append("Contents").Append("Info.plist")); | |
| 48 NSMutableDictionary* dict = | |
| 49 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; | |
| 50 | |
| 51 [dict setObject:base::SysUTF8ToNSString(info.extension_id) | |
| 52 forKey:app_mode::kCrAppModeShortcutIDKey]; | |
| 53 [dict setObject:base::SysUTF16ToNSString(info.title) | |
| 54 forKey:app_mode::kCrAppModeShortcutNameKey]; | |
| 55 [dict setObject:base::SysUTF8ToNSString(info.url.spec()) | |
| 56 forKey:app_mode::kCrAppModeShortcutURLKey]; | |
| 57 return [dict writeToFile:plist_path atomically:NO]; | |
| 58 } | |
| 59 | |
| 60 bool UpdateIcon() { | |
| 61 // TODO:(sail) Need to implement this. | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 namespace web_app { | |
| 68 | |
| 69 namespace internals { | |
| 70 | |
| 71 void CreateShortcutTask(const FilePath& web_app_path, | |
| 72 const FilePath& profile_path, | |
| 73 const ShellIntegration::ShortcutInfo& shortcut_info) { | |
| 74 FilePath app_name = GetSanitizedFileName(shortcut_info.title); | |
| 75 FilePath app_file_name = app_name.ReplaceExtension("app"); | 31 FilePath app_file_name = app_name.ReplaceExtension("app"); |
| 76 ScopedTempDir scoped_temp_dir; | 32 ScopedTempDir scoped_temp_dir; |
| 77 if (!scoped_temp_dir.CreateUniqueTempDir()) | 33 if (!scoped_temp_dir.CreateUniqueTempDir()) |
| 78 return; | 34 return false; |
| 79 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); | 35 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); |
| 80 | 36 |
| 81 // Update the app's plist and icon in a temp directory. This works around | 37 // Update the app's plist and icon in a temp directory. This works around |
| 82 // a Finder bug where the app's icon doesn't properly update. | 38 // a Finder bug where the app's icon doesn't properly update. |
| 83 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { | 39 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { |
| 84 LOG(ERROR) << "Copying app to staging path: " << staging_path.value() | 40 LOG(ERROR) << "Copying app to staging path: " << staging_path.value() |
| 85 << " failed"; | 41 << " failed"; |
| 86 return; | 42 return false; |
| 87 } | 43 } |
| 88 | 44 |
| 89 if (!UpdatePlist(staging_path, shortcut_info)) | 45 if (!UpdatePlist(staging_path)) |
| 90 return; | 46 return false; |
| 91 | 47 |
| 92 if (!UpdateIcon()) | 48 if (!UpdateIcon(staging_path)) |
| 93 return; | 49 return false; |
| 94 | 50 |
| 95 FilePath dst_path = GetDstPath().Append(app_file_name); | 51 FilePath dst_path = GetDstPath(app_file_name); |
| 96 if (!file_util::CopyDirectory(staging_path, dst_path, true)) { | 52 if (!file_util::CopyDirectory(staging_path, dst_path, true)) { |
| 97 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; | 53 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; |
| 98 return; | 54 return false; |
| 99 } | 55 } |
| 100 | 56 |
| 101 [[NSWorkspace sharedWorkspace] | 57 [[NSWorkspace sharedWorkspace] |
| 102 selectFile:base::mac::FilePathToNSString(dst_path) | 58 selectFile:base::mac::FilePathToNSString(dst_path) |
| 103 inFileViewerRootedAtPath:nil]; | 59 inFileViewerRootedAtPath:nil]; |
| 60 return true; |
| 61 } |
| 62 |
| 63 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { |
| 64 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) |
| 65 stringByAppendingString:@" App Mode Loader.app"]; |
| 66 return base::mac::PathForFrameworkBundleResource((CFStringRef)app_loader); |
| 67 } |
| 68 |
| 69 FilePath WebAppShortcutCreator::GetDstPath( |
| 70 const FilePath& app_file_name) const { |
| 71 FilePath path; |
| 72 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| 73 file_util::PathIsWritable(path)) { |
| 74 return path.Append(app_file_name); |
| 75 } |
| 76 |
| 77 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
| 78 return path.Append(app_file_name); |
| 79 |
| 80 return FilePath(); |
| 81 } |
| 82 |
| 83 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { |
| 84 NSString* plist_path = base::mac::FilePathToNSString( |
| 85 app_path.Append("Contents").Append("Info.plist")); |
| 86 NSMutableDictionary* dict = |
| 87 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; |
| 88 |
| 89 [dict setObject:base::SysUTF8ToNSString(info_.extension_id) |
| 90 forKey:app_mode::kCrAppModeShortcutIDKey]; |
| 91 [dict setObject:base::SysUTF16ToNSString(info_.title) |
| 92 forKey:app_mode::kCrAppModeShortcutNameKey]; |
| 93 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) |
| 94 forKey:app_mode::kCrAppModeShortcutURLKey]; |
| 95 return [dict writeToFile:plist_path atomically:NO]; |
| 96 } |
| 97 |
| 98 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
| 99 // TODO:(sail) Need to implement this. |
| 100 return true; |
| 101 } |
| 102 |
| 103 namespace internals { |
| 104 |
| 105 void CreateShortcutTask(const FilePath& web_app_path, |
| 106 const FilePath& profile_path, |
| 107 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 108 WebAppShortcutCreator shortcut_creator(shortcut_info); |
| 109 shortcut_creator.CreateShortcut(); |
| 104 } | 110 } |
| 105 | 111 |
| 106 } // namespace internals | 112 } // namespace internals |
| 107 | 113 |
| 108 } // namespace web_app | 114 } // namespace web_app |
| OLD | NEW |