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