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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2d0e7a4c59db6afc892c58479dd075bf9d2bd30 |
| --- /dev/null |
| +++ b/chrome/browser/web_applications/web_app_mac.mm |
| @@ -0,0 +1,109 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// 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" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "base/file_util.h" |
| +#include "base/mac/bundle_locations.h" |
| +#include "base/mac/foundation_util.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "base/sys_string_conversions.h" |
| +#include "chrome/common/mac/app_mode_common.h" |
| +#include "grit/chromium_strings.h" |
| +#include "ui/base/l10n/l10n_util_mac.h" |
| + |
| +namespace { |
| + |
| +FilePath GetAppLoaderPath() { |
| + NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) |
| + stringByAppendingString:@" App Mode Loader.app"]; |
|
jeremy
2012/02/08 09:10:45
IMHO this should be indented 4 past [l10n_util , s
---DO-NOT-USE---rsesek1
2012/02/08 12:24:33
This is correct as-is.
|
| + return base::mac::PathForFrameworkBundleResource( |
| + base::mac::NSToCFCast(app_loader)); |
| +} |
| + |
| +bool IsWritable(const FilePath& path) { |
|
jeremy
2012/02/08 09:10:45
Should this live in base/file_util.h ?
sail
2012/02/08 19:09:15
Removed, this was already in base.
|
| + return [[NSFileManager defaultManager] isWritableFileAtPath: |
| + base::mac::FilePathToNSString(path)]; |
| +} |
| + |
| +FilePath GetDestinationPath() { |
|
jeremy
2012/02/08 09:10:45
Comment for this function.
sail
2012/02/08 19:09:15
Done.
|
| + FilePath path; |
| + if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| + IsWritable(path)) { |
| + return path; |
| + } |
| + |
| + if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
| + return path; |
| + |
| + return FilePath(); |
| +} |
| + |
| +bool UpdatePlist(const FilePath& app_path, |
| + const ShellIntegration::ShortcutInfo& info) { |
| + NSString* plist_path = base::mac::FilePathToNSString( |
| + app_path.Append("Contents").Append("Info.plist")); |
|
jeremy
2012/02/08 09:10:45
nit: IMHO splitting this into 2 lines would make i
---DO-NOT-USE---rsesek1
2012/02/08 12:24:33
I think this is OK.
|
| + NSMutableDictionary* dict = |
| + [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; |
| + |
| + [dict setObject:base::SysUTF8ToNSString(info.extension_id) |
| + forKey:app_mode::kCrAppModeShortcutIDKey]; |
| + [dict setObject:base::SysUTF16ToNSString(info.title) |
| + forKey:app_mode::kCrAppModeShortcutNameKey]; |
| + [dict setObject:base::SysUTF8ToNSString(info.url.spec()) |
| + forKey:app_mode::kCrAppModeShortcutURLKey]; |
| + return [dict writeToFile:plist_path atomically:YES]; |
| +} |
| + |
| +bool UpdateIcon() { |
| + // TODO:(sail) Need to implement this. |
| + return true; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace web_app { |
|
jeremy
2012/02/08 09:10:45
no newline between nested namespaces.
sail
2012/02/08 19:09:15
Done.
|
| + |
| +namespace internals { |
| + |
| +void CreateShortcutTask(const FilePath& web_app_path, |
| + const FilePath& profile_path, |
| + const ShellIntegration::ShortcutInfo& shortcut_info) { |
| + 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]; |
| +} |
| + |
| +} // namespace internals |
| + |
| +} // namespace web_app |