Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/web_applications/web_app_mac.mm

Issue 9362001: Unit tests for publishing app shortcuts on Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 FilePath app_name = internals::GetSanitizedFileName(info_.title);
32 FilePath app_file_name = app_name.ReplaceExtension("app");
33 ScopedTempDir scoped_temp_dir;
34 if (!scoped_temp_dir.CreateUniqueTempDir())
35 return false;
36 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name);
37
38 // Update the app's plist and icon in a temp directory. This works around
39 // a Finder bug where the app's icon doesn't properly update.
40 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) {
41 LOG(ERROR) << "Copying app to staging path: " << staging_path.value()
42 << " failed";
43 return false;
44 }
45
46 if (!UpdatePlist(staging_path))
47 return false;
48
49 if (!UpdateIcon(staging_path))
50 return false;
51
52 FilePath dst_path = GetDestinationPath(app_file_name);
53 if (!file_util::CopyDirectory(staging_path, dst_path, true)) {
54 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed";
55 return false;
56 }
57
58 [[NSWorkspace sharedWorkspace]
59 selectFile:base::mac::FilePathToNSString(dst_path)
60 inFileViewerRootedAtPath:nil];
61 return true;
62 }
63
64 FilePath WebAppShortcutCreator::GetAppLoaderPath() const {
23 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) 65 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME)
24 stringByAppendingString:@" App Mode Loader.app"]; 66 stringByAppendingString:@" App Mode Loader.app"];
25 return base::mac::PathForFrameworkBundleResource( 67 return base::mac::PathForFrameworkBundleResource(
26 base::mac::NSToCFCast(app_loader)); 68 base::mac::NSToCFCast(app_loader));
27 } 69 }
28 70
29 // Returns a path to the destination where the app should be written to. 71 FilePath WebAppShortcutCreator::GetDestinationPath(
30 FilePath GetDestinationPath() { 72 const FilePath& app_file_name) const {
31 FilePath path; 73 FilePath path;
32 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && 74 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) &&
33 file_util::PathIsWritable(path)) { 75 file_util::PathIsWritable(path)) {
34 return path; 76 return path;
35 } 77 }
36 78
37 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) 79 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path))
38 return path; 80 return path;
39 81
40 return FilePath(); 82 return FilePath();
41 } 83 }
42 84
43 // Updates the plist inside |app_path| with the information in |info|. 85 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( 86 NSString* plist_path = base::mac::FilePathToNSString(
47 app_path.Append("Contents").Append("Info.plist")); 87 app_path.Append("Contents").Append("Info.plist"));
48 NSMutableDictionary* dict = 88 NSMutableDictionary* dict =
49 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; 89 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path];
50 90
51 [dict setObject:base::SysUTF8ToNSString(info.extension_id) 91 [dict setObject:base::SysUTF8ToNSString(info_.extension_id)
52 forKey:app_mode::kCrAppModeShortcutIDKey]; 92 forKey:app_mode::kCrAppModeShortcutIDKey];
53 [dict setObject:base::SysUTF16ToNSString(info.title) 93 [dict setObject:base::SysUTF16ToNSString(info_.title)
54 forKey:app_mode::kCrAppModeShortcutNameKey]; 94 forKey:app_mode::kCrAppModeShortcutNameKey];
55 [dict setObject:base::SysUTF8ToNSString(info.url.spec()) 95 [dict setObject:base::SysUTF8ToNSString(info_.url.spec())
56 forKey:app_mode::kCrAppModeShortcutURLKey]; 96 forKey:app_mode::kCrAppModeShortcutURLKey];
57 return [dict writeToFile:plist_path atomically:YES]; 97 return [dict writeToFile:plist_path atomically:YES];
58 } 98 }
59 99
60 bool UpdateIcon() { 100 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const {
61 // TODO:(sail) Need to implement this. 101 // TODO:(sail) Need to implement this.
62 return true; 102 return true;
63 } 103 }
64 104
65 } // namespace 105 } // namespace
66 106
67 namespace web_app { 107 namespace web_app {
68 namespace internals { 108 namespace internals {
69 109
70 void CreateShortcutTask(const FilePath& web_app_path, 110 void CreateShortcutTask(const FilePath& web_app_path,
71 const FilePath& profile_path, 111 const FilePath& profile_path,
72 const ShellIntegration::ShortcutInfo& shortcut_info) { 112 const ShellIntegration::ShortcutInfo& shortcut_info) {
73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
74 FilePath app_name = GetSanitizedFileName(shortcut_info.title); 114 WebAppShortcutCreator shortcut_creator(shortcut_info);
75 FilePath app_file_name = app_name.ReplaceExtension("app"); 115 shortcut_creator.CreateShortcut();
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
OLDNEW
« no previous file with comments | « chrome/browser/web_applications/web_app_mac.h ('k') | chrome/browser/web_applications/web_app_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698