| 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 "content/public/browser/browser_thread.h" |
| 16 #include "grit/chromium_strings.h" |
| 17 #include "ui/base/l10n/l10n_util_mac.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Returns a path to the app loader. |
| 22 FilePath GetAppLoaderPath() { |
| 23 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) |
| 24 stringByAppendingString:@" App Mode Loader.app"]; |
| 25 return base::mac::PathForFrameworkBundleResource( |
| 26 base::mac::NSToCFCast(app_loader)); |
| 27 } |
| 28 |
| 29 // Returns a path to the destination where the app should be written to. |
| 30 FilePath GetDestinationPath() { |
| 31 FilePath path; |
| 32 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| 33 file_util::PathIsWritable(path)) { |
| 34 return path; |
| 35 } |
| 36 |
| 37 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
| 38 return path; |
| 39 |
| 40 return FilePath(); |
| 41 } |
| 42 |
| 43 // Updates the plist inside |app_path| with the information in |info|. |
| 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:YES]; |
| 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 namespace internals { |
| 69 |
| 70 void CreateShortcutTask(const FilePath& web_app_path, |
| 71 const FilePath& profile_path, |
| 72 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 74 FilePath app_name = GetSanitizedFileName(shortcut_info.title); |
| 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 } |
| 105 |
| 106 } // namespace internals |
| 107 } // namespace web_app |
| OLD | NEW |