Index: chrome/browser/ui/cocoa/applescript/tab_applescript.mm |
diff --git a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm |
index 99c1b323bf149e7eb58c815e326afcd47f3304d9..5e806f9d0314ad3b59ebd7df7d4d0b7961c1013e 100644 |
--- a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm |
+++ b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm |
@@ -7,6 +7,7 @@ |
#import <Carbon/Carbon.h> |
#import <Foundation/NSAppleEventDescriptor.h> |
+#include "base/bind.h" |
#include "base/file_path.h" |
#include "base/logging.h" |
#import "base/memory/scoped_nsobject.h" |
@@ -32,49 +33,28 @@ using content::RenderViewHost; |
using content::Referrer; |
using content::WebContents; |
-@interface AnyResultValue : NSObject { |
- @private |
- scoped_nsobject<NSAppleEventDescriptor> descriptor; |
-} |
-- (id)initWithDescriptor:(NSAppleEventDescriptor*)desc; |
-- (NSAppleEventDescriptor *)scriptingAnyDescriptor; |
-@end |
- |
-@implementation AnyResultValue |
- |
-- (id)initWithDescriptor:(NSAppleEventDescriptor*)desc { |
- if (self = [super init]) { |
- descriptor.reset([desc retain]); |
- } |
- return self; |
-} |
- |
-- (NSAppleEventDescriptor *)scriptingAnyDescriptor { |
- return descriptor.get(); |
-} |
+namespace { |
-@end |
- |
-static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
+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
|
NSAppleEventDescriptor* descriptor = nil; |
switch (value->GetType()) { |
- case Value::TYPE_NULL: |
+ case base::Value::TYPE_NULL: |
descriptor = [NSAppleEventDescriptor |
descriptorWithTypeCode:cMissingValue]; |
break; |
- case Value::TYPE_BOOLEAN: { |
+ case base::Value::TYPE_BOOLEAN: { |
bool bool_value; |
value->GetAsBoolean(&bool_value); |
descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value]; |
break; |
} |
- case Value::TYPE_INTEGER: { |
+ case base::Value::TYPE_INTEGER: { |
int int_value; |
value->GetAsInteger(&int_value); |
descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value]; |
break; |
} |
- case Value::TYPE_DOUBLE: { |
+ case base::Value::TYPE_DOUBLE: { |
double double_value; |
value->GetAsDouble(&double_value); |
descriptor = [NSAppleEventDescriptor |
@@ -83,24 +63,25 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
length:sizeof(double_value)]; |
break; |
} |
- case Value::TYPE_STRING: { |
+ case base::Value::TYPE_STRING: { |
std::string string_value; |
value->GetAsString(&string_value); |
descriptor = [NSAppleEventDescriptor descriptorWithString: |
base::SysUTF8ToNSString(string_value)]; |
break; |
} |
- case Value::TYPE_BINARY: |
+ case base::Value::TYPE_BINARY: |
NOTREACHED(); |
break; |
- case Value::TYPE_DICTIONARY: { |
- DictionaryValue* dictionary_value = static_cast<DictionaryValue*>(value); |
+ case base::Value::TYPE_DICTIONARY: { |
+ const base::DictionaryValue* dictionary_value = |
+ static_cast<const base::DictionaryValue*>(value); |
descriptor = [NSAppleEventDescriptor recordDescriptor]; |
NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor |
listDescriptor]; |
for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys()); |
iter != dictionary_value->end_keys(); ++iter) { |
- Value* item; |
+ const base::Value* item; |
if (dictionary_value->Get(*iter, &item)) { |
[userRecord insertDescriptor:[NSAppleEventDescriptor |
descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0]; |
@@ -112,12 +93,12 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
[descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields]; |
break; |
} |
- case Value::TYPE_LIST: { |
- ListValue* list_value; |
+ case base::Value::TYPE_LIST: { |
+ const base::ListValue* list_value; |
value->GetAsList(&list_value); |
descriptor = [NSAppleEventDescriptor listDescriptor]; |
for (unsigned i = 0; i < list_value->GetSize(); ++i) { |
- Value* item; |
+ const base::Value* item; |
list_value->Get(i, &item); |
[descriptor insertDescriptor:valueToDescriptor(item) atIndex:0]; |
} |
@@ -127,6 +108,20 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
return descriptor; |
} |
+void ResumeAppleEventAndSendReply(NSAppleEventManagerSuspensionID suspension_id, |
+ const base::Value* result_value) { |
+ NSAppleEventDescriptor* result_descriptor = valueToDescriptor(result_value); |
+ |
+ NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager]; |
+ NSAppleEventDescriptor* reply_event = |
+ [manager replyAppleEventForSuspensionID:suspension_id]; |
+ [reply_event setParamDescriptor:result_descriptor |
+ forKeyword:keyDirectObject]; |
+ [manager resumeWithSuspensionID:suspension_id]; |
+} |
+ |
+} // namespace |
+ |
@interface TabAppleScript() |
@property (nonatomic, copy) NSString* tempURL; |
@end |
@@ -141,8 +136,7 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
SessionID::id_type futureSessionIDOfTab = session.id() + 1; |
// Holds the SessionID that the new tab is going to get. |
scoped_nsobject<NSNumber> numID( |
- [[NSNumber alloc] |
- initWithInt:futureSessionIDOfTab]); |
+ [[NSNumber alloc] initWithInt:futureSessionIDOfTab]); |
[self setUniqueID:numID]; |
} |
return self; |
@@ -160,8 +154,8 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
} |
if ((self = [super init])) { |
- // It is safe to be weak, if a tab goes away (eg user closing a tab) |
- // the applescript runtime calls tabs in AppleScriptWindow and this |
+ // It is safe to be weak; if a tab goes away (e.g. the user closes a tab) |
+ // the AppleScript runtime calls tabs in AppleScriptWindow and this |
// particular tab is never returned. |
webContents_ = webContents; |
SessionTabHelper* session_tab_helper = |
@@ -175,8 +169,8 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
- (void)setWebContents:(content::WebContents*)webContents { |
DCHECK(webContents); |
- // It is safe to be weak, if a tab goes away (eg user closing a tab) |
- // the applescript runtime calls tabs in AppleScriptWindow and this |
+ // It is safe to be weak; if a tab goes away (e.g. the user closes a tab) |
+ // the AppleScript runtime calls tabs in AppleScriptWindow and this |
// particular tab is never returned. |
webContents_ = webContents; |
SessionTabHelper* session_tab_helper = |
@@ -402,11 +396,19 @@ static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
return nil; |
} |
+ NSAppleEventManager* manager = [NSAppleEventManager sharedAppleEventManager]; |
+ NSAppleEventManagerSuspensionID suspensionID = |
+ [manager suspendCurrentAppleEvent]; |
+ content::RenderViewHost::JavascriptResultCallback callback = |
+ base::Bind(&ResumeAppleEventAndSendReply, suspensionID); |
+ |
string16 script = base::SysNSStringToUTF16( |
[[command evaluatedArguments] objectForKey:@"javascript"]); |
- Value* value = view->ExecuteJavascriptAndGetValue(string16(), script); |
- NSAppleEventDescriptor* descriptor = valueToDescriptor(value); |
- return [[[AnyResultValue alloc] initWithDescriptor:descriptor] autorelease]; |
+ view->ExecuteJavascriptInWebFrameCallbackResult(string16(), // frame_xpath |
+ script, |
+ callback); |
+ |
+ return nil; |
} |
@end |