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.
5 #import "chrome/browser/ui/cocoa/applescript/tab_applescript.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "chrome/browser/printing/print_view_manager.h"
13 #include "chrome/browser/sessions/session_tab_helper.h"
14 #include "chrome/browser/ui/cocoa/applescript/apple_event_util.h"
15 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
16 #include "chrome/browser/ui/cocoa/applescript/metrics_applescript.h"
17 #include "chrome/common/chrome_isolated_world_ids.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/sessions/session_id.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/save_page_type.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_delegate.h"
29 using content::NavigationController;
30 using content::NavigationEntry;
31 using content::OpenURLParams;
32 using content::RenderFrameHost;
33 using content::RenderViewHost;
34 using content::Referrer;
35 using content::WebContents;
39 void ResumeAppleEventAndSendReply(NSAppleEventManagerSuspensionID suspension_id,
40 const base::Value* result_value) {
41 NSAppleEventDescriptor* result_descriptor =
42 chrome::mac::ValueToAppleEventDescriptor(result_value);
44 NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
45 NSAppleEventDescriptor* reply_event =
46 [manager replyAppleEventForSuspensionID:suspension_id];
47 [reply_event setParamDescriptor:result_descriptor
48 forKeyword:keyDirectObject];
49 [manager resumeWithSuspensionID:suspension_id];
54 @interface TabAppleScript()
55 @property (nonatomic, copy) NSString* tempURL;
58 @implementation TabAppleScript
60 @synthesize tempURL = tempURL_;
63 if ((self = [super init])) {
65 SessionID::id_type futureSessionIDOfTab = session.id() + 1;
66 // Holds the SessionID that the new tab is going to get.
67 base::scoped_nsobject<NSNumber> numID(
68 [[NSNumber alloc] initWithInt:futureSessionIDOfTab]);
69 [self setUniqueID:numID];
79 - (id)initWithWebContents:(content::WebContents*)webContents {
85 if ((self = [super init])) {
86 // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
87 // the AppleScript runtime calls tabs in AppleScriptWindow and this
88 // particular tab is never returned.
89 webContents_ = webContents;
90 SessionTabHelper* session_tab_helper =
91 SessionTabHelper::FromWebContents(webContents);
92 base::scoped_nsobject<NSNumber> numID(
93 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]);
94 [self setUniqueID:numID];
99 - (void)setWebContents:(content::WebContents*)webContents {
101 // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
102 // the AppleScript runtime calls tabs in AppleScriptWindow and this
103 // particular tab is never returned.
104 webContents_ = webContents;
105 SessionTabHelper* session_tab_helper =
106 SessionTabHelper::FromWebContents(webContents);
107 base::scoped_nsobject<NSNumber> numID(
108 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]);
109 [self setUniqueID:numID];
112 [self setURL:[self tempURL]];
120 NavigationEntry* entry = webContents_->GetController().GetActiveEntry();
124 const GURL& url = entry->GetVirtualURL();
125 return base::SysUTF8ToNSString(url.spec());
128 - (void)setURL:(NSString*)aURL {
129 // If a scripter sets a URL before the node is added save it at a temporary
132 [self setTempURL:aURL];
136 GURL url(base::SysNSStringToUTF8(aURL));
137 // check for valid url.
138 if (!url.is_empty() && !url.is_valid()) {
139 AppleScript::SetError(AppleScript::errInvalidURL);
143 NavigationEntry* entry = webContents_->GetController().GetActiveEntry();
147 const GURL& previousURL = entry->GetVirtualURL();
148 webContents_->OpenURL(OpenURLParams(
150 content::Referrer(previousURL, blink::WebReferrerPolicyDefault),
152 ui::PAGE_TRANSITION_TYPED,
157 NavigationEntry* entry = webContents_->GetController().GetActiveEntry();
161 base::string16 title = entry ? entry->GetTitle() : base::string16();
162 return base::SysUTF16ToNSString(title);
165 - (NSNumber*)loading {
166 BOOL loadingValue = webContents_->IsLoading() ? YES : NO;
167 return [NSNumber numberWithBool:loadingValue];
170 - (void)handlesUndoScriptCommand:(NSScriptCommand*)command {
171 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_UNDO);
172 webContents_->Undo();
175 - (void)handlesRedoScriptCommand:(NSScriptCommand*)command {
176 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_REDO);
177 webContents_->Redo();
180 - (void)handlesCutScriptCommand:(NSScriptCommand*)command {
181 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_CUT);
185 - (void)handlesCopyScriptCommand:(NSScriptCommand*)command {
186 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_COPY);
187 webContents_->Copy();
190 - (void)handlesPasteScriptCommand:(NSScriptCommand*)command {
191 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_PASTE);
192 webContents_->Paste();
195 - (void)handlesSelectAllScriptCommand:(NSScriptCommand*)command {
196 AppleScript::LogAppleScriptUMA(
197 AppleScript::AppleScriptCommand::TAB_SELECT_ALL);
198 webContents_->SelectAll();
201 - (void)handlesGoBackScriptCommand:(NSScriptCommand*)command {
202 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_GO_BACK);
203 NavigationController& navigationController = webContents_->GetController();
204 if (navigationController.CanGoBack())
205 navigationController.GoBack();
208 - (void)handlesGoForwardScriptCommand:(NSScriptCommand*)command {
209 AppleScript::LogAppleScriptUMA(
210 AppleScript::AppleScriptCommand::TAB_GO_FORWARD);
211 NavigationController& navigationController = webContents_->GetController();
212 if (navigationController.CanGoForward())
213 navigationController.GoForward();
216 - (void)handlesReloadScriptCommand:(NSScriptCommand*)command {
217 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_RELOAD);
218 NavigationController& navigationController = webContents_->GetController();
219 const bool checkForRepost = true;
220 navigationController.Reload(checkForRepost);
223 - (void)handlesStopScriptCommand:(NSScriptCommand*)command {
224 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_STOP);
225 webContents_->Stop();
228 - (void)handlesPrintScriptCommand:(NSScriptCommand*)command {
229 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_PRINT);
231 printing::PrintViewManager::FromWebContents(webContents_)->PrintNow();
233 AppleScript::SetError(AppleScript::errInitiatePrinting);
237 - (void)handlesSaveScriptCommand:(NSScriptCommand*)command {
238 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_SAVE);
239 NSDictionary* dictionary = [command evaluatedArguments];
241 NSURL* fileURL = [dictionary objectForKey:@"File"];
242 // Scripter has not specifed the location at which to save, so we prompt for
245 webContents_->OnSavePage();
249 base::FilePath mainFile(base::SysNSStringToUTF8([fileURL path]));
250 // We create a directory path at the folder within which the file exists.
251 // Eg. if main_file = '/Users/Foo/Documents/Google.html'
252 // then directory_path = '/Users/Foo/Documents/Google_files/'.
253 base::FilePath directoryPath = mainFile.RemoveExtension();
254 directoryPath = directoryPath.InsertBeforeExtension(std::string("_files/"));
256 NSString* saveType = [dictionary objectForKey:@"FileType"];
258 content::SavePageType savePageType = content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML;
260 if ([saveType isEqualToString:@"only html"]) {
261 savePageType = content::SAVE_PAGE_TYPE_AS_ONLY_HTML;
262 } else if ([saveType isEqualToString:@"complete html"]) {
263 savePageType = content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML;
265 AppleScript::SetError(AppleScript::errInvalidSaveType);
270 webContents_->SavePage(mainFile, directoryPath, savePageType);
273 - (void)handlesCloseScriptCommand:(NSScriptCommand*)command {
274 AppleScript::LogAppleScriptUMA(AppleScript::AppleScriptCommand::TAB_CLOSE);
275 webContents_->GetDelegate()->CloseContents(webContents_);
278 - (void)handlesViewSourceScriptCommand:(NSScriptCommand*)command {
279 AppleScript::LogAppleScriptUMA(
280 AppleScript::AppleScriptCommand::TAB_VIEW_SOURCE);
281 NavigationEntry* entry =
282 webContents_->GetController().GetLastCommittedEntry();
284 webContents_->OpenURL(
285 OpenURLParams(GURL(content::kViewSourceScheme + std::string(":") +
286 entry->GetURL().spec()),
289 ui::PAGE_TRANSITION_LINK,
294 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command {
295 AppleScript::LogAppleScriptUMA(
296 AppleScript::AppleScriptCommand::TAB_EXECUTE_JAVASCRIPT);
297 content::RenderFrameHost* frame = webContents_->GetMainFrame();
303 NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
304 NSAppleEventManagerSuspensionID suspensionID =
305 [manager suspendCurrentAppleEvent];
306 content::RenderFrameHost::JavaScriptResultCallback callback =
307 base::Bind(&ResumeAppleEventAndSendReply, suspensionID);
309 base::string16 script = base::SysNSStringToUTF16(
310 [[command evaluatedArguments] objectForKey:@"javascript"]);
311 frame->ExecuteJavaScriptInIsolatedWorld(
312 script, callback, chrome::ISOLATED_WORLD_ID_APPLESCRIPT);