Chromium Code Reviews| Index: chrome/browser/web_applications/web_app_mac.mm |
| diff --git a/chrome/browser/web_applications/web_app_mac.mm b/chrome/browser/web_applications/web_app_mac.mm |
| index 00c0b2fa68bbbaf4264406aea6c255aa76d15e41..8ccaa978f8029435947f4732d688a5b121637694 100644 |
| --- a/chrome/browser/web_applications/web_app_mac.mm |
| +++ b/chrome/browser/web_applications/web_app_mac.mm |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/web_applications/web_app.h" |
| +#include "chrome/browser/web_applications/web_app_mac.h" |
| #import <Cocoa/Cocoa.h> |
| @@ -11,23 +11,66 @@ |
| #include "base/mac/foundation_util.h" |
| #include "base/scoped_temp_dir.h" |
| #include "base/sys_string_conversions.h" |
| +#include "chrome/browser/web_applications/web_app.h" |
| #include "chrome/common/mac/app_mode_common.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "grit/chromium_strings.h" |
| #include "ui/base/l10n/l10n_util_mac.h" |
| -namespace { |
| +namespace web_app { |
| + |
| +WebAppShortcutCreator::WebAppShortcutCreator( |
| + const ShellIntegration::ShortcutInfo& shortcut_info) |
| + : info_(shortcut_info) { |
| +} |
| + |
| +WebAppShortcutCreator::~WebAppShortcutCreator() { |
| +} |
| + |
| +bool WebAppShortcutCreator::CreateShortcut() { |
| + 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
|
| + FilePath app_name = internals::GetSanitizedFileName(info_.title); |
| + FilePath app_file_name = app_name.ReplaceExtension("app"); |
| + ScopedTempDir scoped_temp_dir; |
| + if (!scoped_temp_dir.CreateUniqueTempDir()) |
| + return false; |
| + FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); |
| + |
| + // Update the app's plist and icon in a temp directory. This works around |
| + // a Finder bug where the app's icon doesn't properly update. |
| + if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { |
| + LOG(ERROR) << "Copying app to staging path: " << staging_path.value() |
| + << " failed"; |
| + return false; |
| + } |
| + |
| + if (!UpdatePlist(staging_path)) |
| + return false; |
| + |
| + if (!UpdateIcon(staging_path)) |
| + return false; |
| + |
| + FilePath dst_path = GetDestinationPath(app_file_name); |
| + if (!file_util::CopyDirectory(staging_path, dst_path, true)) { |
| + LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; |
| + return false; |
| + } |
| + |
| + [[NSWorkspace sharedWorkspace] |
| + selectFile:base::mac::FilePathToNSString(dst_path) |
| + inFileViewerRootedAtPath:nil]; |
| + return true; |
| +} |
| -// Returns a path to the app loader. |
| -FilePath GetAppLoaderPath() { |
| +FilePath WebAppShortcutCreator::GetAppLoaderPath() const { |
| NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) |
| stringByAppendingString:@" App Mode Loader.app"]; |
| return base::mac::PathForFrameworkBundleResource( |
| base::mac::NSToCFCast(app_loader)); |
| } |
| -// Returns a path to the destination where the app should be written to. |
| -FilePath GetDestinationPath() { |
| +FilePath WebAppShortcutCreator::GetDestinationPath( |
| + const FilePath& app_file_name) const { |
| FilePath path; |
| if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| file_util::PathIsWritable(path)) { |
| @@ -40,24 +83,22 @@ FilePath GetDestinationPath() { |
| return FilePath(); |
| } |
| -// Updates the plist inside |app_path| with the information in |info|. |
| -bool UpdatePlist(const FilePath& app_path, |
| - const ShellIntegration::ShortcutInfo& info) { |
| +bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { |
| NSString* plist_path = base::mac::FilePathToNSString( |
| app_path.Append("Contents").Append("Info.plist")); |
| NSMutableDictionary* dict = |
| [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; |
| - [dict setObject:base::SysUTF8ToNSString(info.extension_id) |
| + [dict setObject:base::SysUTF8ToNSString(info_.extension_id) |
| forKey:app_mode::kCrAppModeShortcutIDKey]; |
| - [dict setObject:base::SysUTF16ToNSString(info.title) |
| + [dict setObject:base::SysUTF16ToNSString(info_.title) |
| forKey:app_mode::kCrAppModeShortcutNameKey]; |
| - [dict setObject:base::SysUTF8ToNSString(info.url.spec()) |
| + [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) |
| forKey:app_mode::kCrAppModeShortcutURLKey]; |
| return [dict writeToFile:plist_path atomically:YES]; |
| } |
| -bool UpdateIcon() { |
| +bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
| // TODO:(sail) Need to implement this. |
| return true; |
| } |
| @@ -70,37 +111,8 @@ namespace internals { |
| void CreateShortcutTask(const FilePath& web_app_path, |
| const FilePath& profile_path, |
| const ShellIntegration::ShortcutInfo& shortcut_info) { |
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| - FilePath app_name = GetSanitizedFileName(shortcut_info.title); |
| - FilePath app_file_name = app_name.ReplaceExtension("app"); |
| - ScopedTempDir scoped_temp_dir; |
| - if (!scoped_temp_dir.CreateUniqueTempDir()) |
| - return; |
| - FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); |
| - |
| - // Update the app's plist and icon in a temp directory. This works around |
| - // a Finder bug where the app's icon doesn't properly update. |
| - if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { |
| - LOG(ERROR) << "Copying app to staging path: " << staging_path.value() |
| - << " failed"; |
| - return; |
| - } |
| - |
| - if (!UpdatePlist(staging_path, shortcut_info)) |
| - return; |
| - |
| - if (!UpdateIcon()) |
| - return; |
| - |
| - FilePath dst_path = GetDestinationPath().Append(app_file_name); |
| - if (!file_util::CopyDirectory(staging_path, dst_path, true)) { |
| - LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; |
| - return; |
| - } |
| - |
| - [[NSWorkspace sharedWorkspace] |
| - selectFile:base::mac::FilePathToNSString(dst_path) |
| - inFileViewerRootedAtPath:nil]; |
| + WebAppShortcutCreator shortcut_creator(shortcut_info); |
| + shortcut_creator.CreateShortcut(); |
| } |
| } // namespace internals |