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

Side by Side Diff: chrome/browser/ui/cocoa/applescript/tab_applescript.mm

Issue 11742028: Suspend Apple Events during their processing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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
« no previous file with comments | « chrome/browser/ui/cocoa/applescript/tab_applescript.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #import "chrome/browser/ui/cocoa/applescript/tab_applescript.h" 5 #import "chrome/browser/ui/cocoa/applescript/tab_applescript.h"
6 6
7 #import <Carbon/Carbon.h> 7 #import <Carbon/Carbon.h>
8 #import <Foundation/NSAppleEventDescriptor.h> 8 #import <Foundation/NSAppleEventDescriptor.h>
9 9
10 #include "base/bind.h"
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #import "base/memory/scoped_nsobject.h" 13 #import "base/memory/scoped_nsobject.h"
13 #include "base/sys_string_conversions.h" 14 #include "base/sys_string_conversions.h"
14 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/printing/print_view_manager.h" 16 #include "chrome/browser/printing/print_view_manager.h"
16 #include "chrome/browser/sessions/session_id.h" 17 #include "chrome/browser/sessions/session_id.h"
17 #include "chrome/browser/sessions/session_tab_helper.h" 18 #include "chrome/browser/sessions/session_tab_helper.h"
18 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h" 19 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
19 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/navigation_controller.h" 21 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h" 22 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/render_view_host.h" 23 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/save_page_type.h" 24 #include "content/public/browser/save_page_type.h"
24 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_contents_delegate.h" 26 #include "content/public/browser/web_contents_delegate.h"
26 #include "googleurl/src/gurl.h" 27 #include "googleurl/src/gurl.h"
27 28
28 using content::NavigationController; 29 using content::NavigationController;
29 using content::NavigationEntry; 30 using content::NavigationEntry;
30 using content::OpenURLParams; 31 using content::OpenURLParams;
31 using content::RenderViewHost; 32 using content::RenderViewHost;
32 using content::Referrer; 33 using content::Referrer;
33 using content::WebContents; 34 using content::WebContents;
34 35
35 @interface AnyResultValue : NSObject { 36 namespace {
36 @private
37 scoped_nsobject<NSAppleEventDescriptor> descriptor;
38 }
39 - (id)initWithDescriptor:(NSAppleEventDescriptor*)desc;
40 - (NSAppleEventDescriptor *)scriptingAnyDescriptor;
41 @end
42 37
43 @implementation AnyResultValue 38 NSAppleEventDescriptor* valueToDescriptor(const base::Value* value) {
Avi (use Gerrit) 2013/01/03 22:31:43 A future CL (ETA a few days) will bring this funct
44
45 - (id)initWithDescriptor:(NSAppleEventDescriptor*)desc {
46 if (self = [super init]) {
47 descriptor.reset([desc retain]);
48 }
49 return self;
50 }
51
52 - (NSAppleEventDescriptor *)scriptingAnyDescriptor {
53 return descriptor.get();
54 }
55
56 @end
57
58 static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
59 NSAppleEventDescriptor* descriptor = nil; 39 NSAppleEventDescriptor* descriptor = nil;
60 switch (value->GetType()) { 40 switch (value->GetType()) {
61 case Value::TYPE_NULL: 41 case base::Value::TYPE_NULL:
62 descriptor = [NSAppleEventDescriptor 42 descriptor = [NSAppleEventDescriptor
63 descriptorWithTypeCode:cMissingValue]; 43 descriptorWithTypeCode:cMissingValue];
64 break; 44 break;
65 case Value::TYPE_BOOLEAN: { 45 case base::Value::TYPE_BOOLEAN: {
66 bool bool_value; 46 bool bool_value;
67 value->GetAsBoolean(&bool_value); 47 value->GetAsBoolean(&bool_value);
68 descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value]; 48 descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value];
69 break; 49 break;
70 } 50 }
71 case Value::TYPE_INTEGER: { 51 case base::Value::TYPE_INTEGER: {
72 int int_value; 52 int int_value;
73 value->GetAsInteger(&int_value); 53 value->GetAsInteger(&int_value);
74 descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value]; 54 descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value];
75 break; 55 break;
76 } 56 }
77 case Value::TYPE_DOUBLE: { 57 case base::Value::TYPE_DOUBLE: {
78 double double_value; 58 double double_value;
79 value->GetAsDouble(&double_value); 59 value->GetAsDouble(&double_value);
80 descriptor = [NSAppleEventDescriptor 60 descriptor = [NSAppleEventDescriptor
81 descriptorWithDescriptorType:typeIEEE64BitFloatingPoint 61 descriptorWithDescriptorType:typeIEEE64BitFloatingPoint
82 bytes:&double_value 62 bytes:&double_value
83 length:sizeof(double_value)]; 63 length:sizeof(double_value)];
84 break; 64 break;
85 } 65 }
86 case Value::TYPE_STRING: { 66 case base::Value::TYPE_STRING: {
87 std::string string_value; 67 std::string string_value;
88 value->GetAsString(&string_value); 68 value->GetAsString(&string_value);
89 descriptor = [NSAppleEventDescriptor descriptorWithString: 69 descriptor = [NSAppleEventDescriptor descriptorWithString:
90 base::SysUTF8ToNSString(string_value)]; 70 base::SysUTF8ToNSString(string_value)];
91 break; 71 break;
92 } 72 }
93 case Value::TYPE_BINARY: 73 case base::Value::TYPE_BINARY:
94 NOTREACHED(); 74 NOTREACHED();
95 break; 75 break;
96 case Value::TYPE_DICTIONARY: { 76 case base::Value::TYPE_DICTIONARY: {
97 DictionaryValue* dictionary_value = static_cast<DictionaryValue*>(value); 77 const base::DictionaryValue* dictionary_value =
78 static_cast<const base::DictionaryValue*>(value);
98 descriptor = [NSAppleEventDescriptor recordDescriptor]; 79 descriptor = [NSAppleEventDescriptor recordDescriptor];
99 NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor 80 NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor
100 listDescriptor]; 81 listDescriptor];
101 for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys()); 82 for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys());
102 iter != dictionary_value->end_keys(); ++iter) { 83 iter != dictionary_value->end_keys(); ++iter) {
103 Value* item; 84 const base::Value* item;
104 if (dictionary_value->Get(*iter, &item)) { 85 if (dictionary_value->Get(*iter, &item)) {
105 [userRecord insertDescriptor:[NSAppleEventDescriptor 86 [userRecord insertDescriptor:[NSAppleEventDescriptor
106 descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0]; 87 descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0];
107 [userRecord insertDescriptor:valueToDescriptor(item) atIndex:0]; 88 [userRecord insertDescriptor:valueToDescriptor(item) atIndex:0];
108 } 89 }
109 } 90 }
110 // Description of what keyASUserRecordFields does. 91 // Description of what keyASUserRecordFields does.
111 // http://www.mail-archive.com/cocoa-dev%40lists.apple.com/msg40149.html 92 // http://www.mail-archive.com/cocoa-dev%40lists.apple.com/msg40149.html
112 [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields]; 93 [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields];
113 break; 94 break;
114 } 95 }
115 case Value::TYPE_LIST: { 96 case base::Value::TYPE_LIST: {
116 ListValue* list_value; 97 const base::ListValue* list_value;
117 value->GetAsList(&list_value); 98 value->GetAsList(&list_value);
118 descriptor = [NSAppleEventDescriptor listDescriptor]; 99 descriptor = [NSAppleEventDescriptor listDescriptor];
119 for (unsigned i = 0; i < list_value->GetSize(); ++i) { 100 for (unsigned i = 0; i < list_value->GetSize(); ++i) {
120 Value* item; 101 const base::Value* item;
121 list_value->Get(i, &item); 102 list_value->Get(i, &item);
122 [descriptor insertDescriptor:valueToDescriptor(item) atIndex:0]; 103 [descriptor insertDescriptor:valueToDescriptor(item) atIndex:0];
123 } 104 }
124 break; 105 break;
125 } 106 }
126 } 107 }
127 return descriptor; 108 return descriptor;
128 } 109 }
129 110
111 void ResumeAppleEventAndSendReply(NSAppleEventManagerSuspensionID suspension_id,
112 const base::Value* result_value) {
113 NSAppleEventDescriptor* result_descriptor = valueToDescriptor(result_value);
114
115 NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
116 NSAppleEventDescriptor* reply_event =
117 [manager replyAppleEventForSuspensionID:suspension_id];
118 [reply_event setParamDescriptor:result_descriptor
119 forKeyword:keyDirectObject];
120 [manager resumeWithSuspensionID:suspension_id];
121 }
122
123 } // namespace
124
130 @interface TabAppleScript() 125 @interface TabAppleScript()
131 @property (nonatomic, copy) NSString* tempURL; 126 @property (nonatomic, copy) NSString* tempURL;
132 @end 127 @end
133 128
134 @implementation TabAppleScript 129 @implementation TabAppleScript
135 130
136 @synthesize tempURL = tempURL_; 131 @synthesize tempURL = tempURL_;
137 132
138 - (id)init { 133 - (id)init {
139 if ((self = [super init])) { 134 if ((self = [super init])) {
140 SessionID session; 135 SessionID session;
141 SessionID::id_type futureSessionIDOfTab = session.id() + 1; 136 SessionID::id_type futureSessionIDOfTab = session.id() + 1;
142 // Holds the SessionID that the new tab is going to get. 137 // Holds the SessionID that the new tab is going to get.
143 scoped_nsobject<NSNumber> numID( 138 scoped_nsobject<NSNumber> numID(
144 [[NSNumber alloc] 139 [[NSNumber alloc] initWithInt:futureSessionIDOfTab]);
145 initWithInt:futureSessionIDOfTab]);
146 [self setUniqueID:numID]; 140 [self setUniqueID:numID];
147 } 141 }
148 return self; 142 return self;
149 } 143 }
150 144
151 - (void)dealloc { 145 - (void)dealloc {
152 [tempURL_ release]; 146 [tempURL_ release];
153 [super dealloc]; 147 [super dealloc];
154 } 148 }
155 149
156 - (id)initWithWebContents:(content::WebContents*)webContents { 150 - (id)initWithWebContents:(content::WebContents*)webContents {
157 if (!webContents) { 151 if (!webContents) {
158 [self release]; 152 [self release];
159 return nil; 153 return nil;
160 } 154 }
161 155
162 if ((self = [super init])) { 156 if ((self = [super init])) {
163 // It is safe to be weak, if a tab goes away (eg user closing a tab) 157 // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
164 // the applescript runtime calls tabs in AppleScriptWindow and this 158 // the AppleScript runtime calls tabs in AppleScriptWindow and this
165 // particular tab is never returned. 159 // particular tab is never returned.
166 webContents_ = webContents; 160 webContents_ = webContents;
167 SessionTabHelper* session_tab_helper = 161 SessionTabHelper* session_tab_helper =
168 SessionTabHelper::FromWebContents(webContents); 162 SessionTabHelper::FromWebContents(webContents);
169 scoped_nsobject<NSNumber> numID( 163 scoped_nsobject<NSNumber> numID(
170 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]); 164 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]);
171 [self setUniqueID:numID]; 165 [self setUniqueID:numID];
172 } 166 }
173 return self; 167 return self;
174 } 168 }
175 169
176 - (void)setWebContents:(content::WebContents*)webContents { 170 - (void)setWebContents:(content::WebContents*)webContents {
177 DCHECK(webContents); 171 DCHECK(webContents);
178 // It is safe to be weak, if a tab goes away (eg user closing a tab) 172 // It is safe to be weak; if a tab goes away (e.g. the user closes a tab)
179 // the applescript runtime calls tabs in AppleScriptWindow and this 173 // the AppleScript runtime calls tabs in AppleScriptWindow and this
180 // particular tab is never returned. 174 // particular tab is never returned.
181 webContents_ = webContents; 175 webContents_ = webContents;
182 SessionTabHelper* session_tab_helper = 176 SessionTabHelper* session_tab_helper =
183 SessionTabHelper::FromWebContents(webContents); 177 SessionTabHelper::FromWebContents(webContents);
184 scoped_nsobject<NSNumber> numID( 178 scoped_nsobject<NSNumber> numID(
185 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]); 179 [[NSNumber alloc] initWithInt:session_tab_helper->session_id().id()]);
186 [self setUniqueID:numID]; 180 [self setUniqueID:numID];
187 181
188 if ([self tempURL]) 182 if ([self tempURL])
189 [self setURL:[self tempURL]]; 183 [self setURL:[self tempURL]];
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 } 389 }
396 } 390 }
397 391
398 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command { 392 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command {
399 RenderViewHost* view = webContents_->GetRenderViewHost(); 393 RenderViewHost* view = webContents_->GetRenderViewHost();
400 if (!view) { 394 if (!view) {
401 NOTREACHED(); 395 NOTREACHED();
402 return nil; 396 return nil;
403 } 397 }
404 398
399 NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager];
400 NSAppleEventManagerSuspensionID suspensionID =
401 [manager suspendCurrentAppleEvent];
402 content::RenderViewHost::JavascriptResultCallback callback =
403 base::Bind(&ResumeAppleEventAndSendReply, suspensionID);
404
405 string16 script = base::SysNSStringToUTF16( 405 string16 script = base::SysNSStringToUTF16(
406 [[command evaluatedArguments] objectForKey:@"javascript"]); 406 [[command evaluatedArguments] objectForKey:@"javascript"]);
407 Value* value = view->ExecuteJavascriptAndGetValue(string16(), script); 407 view->ExecuteJavascriptInWebFrameCallbackResult(string16(), // frame_xpath
408 NSAppleEventDescriptor* descriptor = valueToDescriptor(value); 408 script,
409 return [[[AnyResultValue alloc] initWithDescriptor:descriptor] autorelease]; 409 callback);
410
411 return nil;
410 } 412 }
411 413
412 @end 414 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/applescript/tab_applescript.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698