| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 #import "chrome/browser/cocoa/applescript/tab_applescript.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" |
| 9 #import "base/scoped_nsobject.h" |
| 10 #include "base/sys_string_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/cocoa/applescript/error_applescript.h" |
| 13 #include "chrome/browser/download/save_package.h" |
| 14 #include "chrome/browser/renderer_host/render_view_host.h" |
| 15 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 16 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 17 #include "chrome/browser/tab_contents/tab_contents.h" |
| 18 #include "chrome/browser/sessions/session_id.h" |
| 19 #include "chrome/common/url_constants.h" |
| 20 #include "googleurl/src/gurl.h" |
| 21 |
| 22 @interface TabAppleScript() |
| 23 @property (nonatomic, copy) NSString* tempURL; |
| 24 @end |
| 25 |
| 26 @implementation TabAppleScript |
| 27 |
| 28 @synthesize tempURL = tempURL_; |
| 29 |
| 30 - (id)init { |
| 31 if ((self = [super init])) { |
| 32 [self setTempURL:@""]; |
| 33 } |
| 34 return self; |
| 35 } |
| 36 |
| 37 - (void)dealloc { |
| 38 [tempURL_ release]; |
| 39 [super dealloc]; |
| 40 } |
| 41 |
| 42 - (id)initWithTabContent:(TabContents*)aTabContent { |
| 43 if (!aTabContent) { |
| 44 [self release]; |
| 45 return nil; |
| 46 } |
| 47 |
| 48 if ((self = [super init])) { |
| 49 // It is safe to be weak, if a tab goes away (eg user closing a tab) |
| 50 // the applescript runtime calls tabs in AppleScriptWindow and this |
| 51 // particular tab is never returned. |
| 52 tabContents_ = aTabContent; |
| 53 scoped_nsobject<NSNumber> numID( |
| 54 [[NSNumber alloc] |
| 55 initWithInt:tabContents_->controller().session_id().id()]); |
| 56 [self setUniqueID:numID]; |
| 57 } |
| 58 return self; |
| 59 } |
| 60 |
| 61 - (void)setTabContent:(TabContents*)aTabContent { |
| 62 DCHECK(aTabContent); |
| 63 // It is safe to be weak, if a tab goes away (eg user closing a tab) |
| 64 // the applescript runtime calls tabs in AppleScriptWindow and this |
| 65 // particular tab is never returned. |
| 66 tabContents_ = aTabContent; |
| 67 scoped_nsobject<NSNumber> numID( |
| 68 [[NSNumber alloc] |
| 69 initWithInt:tabContents_->controller().session_id().id()]); |
| 70 [self setUniqueID:numID]; |
| 71 |
| 72 [self setURL:[self tempURL]]; |
| 73 } |
| 74 |
| 75 - (NSString*)URL { |
| 76 if (!tabContents_) { |
| 77 return nil; |
| 78 } |
| 79 |
| 80 NavigationEntry* entry = tabContents_->controller().GetActiveEntry(); |
| 81 if (!entry) { |
| 82 return nil; |
| 83 } |
| 84 const GURL& url = entry->virtual_url(); |
| 85 return base::SysUTF8ToNSString(url.spec()); |
| 86 } |
| 87 |
| 88 - (void)setURL:(NSString*)aURL { |
| 89 // If a scripter sets a URL before the node is added save it at a temporary |
| 90 // location. |
| 91 if (!tabContents_) { |
| 92 [self setTempURL:aURL]; |
| 93 return; |
| 94 } |
| 95 |
| 96 GURL url(base::SysNSStringToUTF8(aURL)); |
| 97 // check for valid url. |
| 98 if (!url.is_empty() && !url.is_valid()) { |
| 99 AppleScript::SetError(AppleScript::errInvalidURL); |
| 100 return; |
| 101 } |
| 102 |
| 103 NavigationEntry* entry = tabContents_->controller().GetActiveEntry(); |
| 104 if (!entry) |
| 105 return; |
| 106 |
| 107 const GURL& previousURL = entry->virtual_url(); |
| 108 tabContents_->OpenURL(url, |
| 109 previousURL, |
| 110 CURRENT_TAB, |
| 111 PageTransition::TYPED); |
| 112 } |
| 113 |
| 114 - (NSString*)title { |
| 115 NavigationEntry* entry = tabContents_->controller().GetActiveEntry(); |
| 116 if (!entry) |
| 117 return nil; |
| 118 |
| 119 std::wstring title; |
| 120 if (entry != NULL) { |
| 121 title = UTF16ToWideHack(entry->title()); |
| 122 } |
| 123 |
| 124 return base::SysWideToNSString(title); |
| 125 } |
| 126 |
| 127 - (NSNumber*)loading { |
| 128 BOOL loadingValue = tabContents_->is_loading() ? YES : NO; |
| 129 return [NSNumber numberWithBool:loadingValue]; |
| 130 } |
| 131 |
| 132 - (void)handlesUndoScriptCommand:(NSScriptCommand*)command { |
| 133 RenderViewHost* view = tabContents_->render_view_host(); |
| 134 if (!view) { |
| 135 NOTREACHED(); |
| 136 return; |
| 137 } |
| 138 |
| 139 view->Undo(); |
| 140 } |
| 141 |
| 142 - (void)handlesRedoScriptCommand:(NSScriptCommand*)command { |
| 143 RenderViewHost* view = tabContents_->render_view_host(); |
| 144 if (!view) { |
| 145 NOTREACHED(); |
| 146 return; |
| 147 } |
| 148 |
| 149 view->Redo(); |
| 150 } |
| 151 |
| 152 - (void)handlesCutScriptCommand:(NSScriptCommand*)command { |
| 153 RenderViewHost* view = tabContents_->render_view_host(); |
| 154 if (!view) { |
| 155 NOTREACHED(); |
| 156 return; |
| 157 } |
| 158 |
| 159 view->Cut(); |
| 160 } |
| 161 |
| 162 - (void)handlesCopyScriptCommand:(NSScriptCommand*)command { |
| 163 RenderViewHost* view = tabContents_->render_view_host(); |
| 164 if (!view) { |
| 165 NOTREACHED(); |
| 166 return; |
| 167 } |
| 168 |
| 169 view->Copy(); |
| 170 } |
| 171 |
| 172 - (void)handlesPasteScriptCommand:(NSScriptCommand*)command { |
| 173 RenderViewHost* view = tabContents_->render_view_host(); |
| 174 if (!view) { |
| 175 NOTREACHED(); |
| 176 return; |
| 177 } |
| 178 |
| 179 view->Paste(); |
| 180 } |
| 181 |
| 182 - (void)handlesSelectAllScriptCommand:(NSScriptCommand*)command { |
| 183 RenderViewHost* view = tabContents_->render_view_host(); |
| 184 if (!view) { |
| 185 NOTREACHED(); |
| 186 return; |
| 187 } |
| 188 |
| 189 view->SelectAll(); |
| 190 } |
| 191 |
| 192 - (void)handlesGoBackScriptCommand:(NSScriptCommand*)command { |
| 193 NavigationController& navigationController = tabContents_->controller(); |
| 194 if (navigationController.CanGoBack()) |
| 195 navigationController.GoBack(); |
| 196 } |
| 197 |
| 198 - (void)handlesGoForwardScriptCommand:(NSScriptCommand*)command { |
| 199 NavigationController& navigationController = tabContents_->controller(); |
| 200 if (navigationController.CanGoForward()) |
| 201 navigationController.GoForward(); |
| 202 } |
| 203 |
| 204 - (void)handlesReloadScriptCommand:(NSScriptCommand*)command { |
| 205 NavigationController& navigationController = tabContents_->controller(); |
| 206 const bool checkForRepost = true; |
| 207 navigationController.Reload(checkForRepost); |
| 208 } |
| 209 |
| 210 - (void)handlesStopScriptCommand:(NSScriptCommand*)command { |
| 211 RenderViewHost* view = tabContents_->render_view_host(); |
| 212 if (!view) { |
| 213 // We tolerate Stop being called even before a view has been created. |
| 214 // So just log a warning instead of a NOTREACHED(). |
| 215 DLOG(WARNING) << "Stop: no view for handle "; |
| 216 return; |
| 217 } |
| 218 |
| 219 view->Stop(); |
| 220 } |
| 221 |
| 222 - (void)handlesPrintScriptCommand:(NSScriptCommand*)command { |
| 223 bool initiateStatus = tabContents_->PrintNow(); |
| 224 if (initiateStatus == false) { |
| 225 AppleScript::SetError(AppleScript::errInitiatePrinting); |
| 226 } |
| 227 } |
| 228 |
| 229 - (void)handlesSaveScriptCommand:(NSScriptCommand*)command { |
| 230 NSDictionary* dictionary = [command evaluatedArguments]; |
| 231 |
| 232 NSURL* fileURL = [dictionary objectForKey:@"File"]; |
| 233 // Scripter has not specifed the location at which to save, so we prompt for |
| 234 // it. |
| 235 if (!fileURL) { |
| 236 tabContents_->OnSavePage(); |
| 237 return; |
| 238 } |
| 239 |
| 240 FilePath mainFile(base::SysNSStringToUTF8([fileURL path])); |
| 241 // We create a directory path at the folder within which the file exists. |
| 242 // Eg. if main_file = '/Users/Foo/Documents/Google.html' |
| 243 // then directory_path = '/Users/Foo/Documents/Google_files/'. |
| 244 FilePath directoryPath = mainFile.RemoveExtension(); |
| 245 directoryPath = directoryPath.InsertBeforeExtension(std::string("_files/")); |
| 246 |
| 247 NSString* saveType = [dictionary objectForKey:@"FileType"]; |
| 248 |
| 249 SavePackage::SavePackageType savePackageType = |
| 250 SavePackage::SAVE_AS_COMPLETE_HTML; |
| 251 if (saveType) { |
| 252 if ([saveType isEqualToString:@"only html"]) { |
| 253 savePackageType = SavePackage::SAVE_AS_ONLY_HTML; |
| 254 } else if ([saveType isEqualToString:@"complete html"]) { |
| 255 savePackageType = SavePackage::SAVE_AS_COMPLETE_HTML; |
| 256 } else { |
| 257 AppleScript::SetError(AppleScript::errInvalidSaveType); |
| 258 return; |
| 259 } |
| 260 } |
| 261 |
| 262 tabContents_->SavePage(mainFile, directoryPath, savePackageType); |
| 263 } |
| 264 |
| 265 |
| 266 - (void)handlesViewSourceScriptCommand:(NSScriptCommand*)command { |
| 267 NavigationEntry* entry = tabContents_->controller().GetLastCommittedEntry(); |
| 268 if (entry) { |
| 269 tabContents_->OpenURL(GURL(chrome::kViewSourceScheme + std::string(":") + |
| 270 entry->url().spec()), GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); |
| 271 } |
| 272 } |
| 273 |
| 274 @end |
| OLD | NEW |