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

Unified Diff: chrome/browser/ui/cocoa/applescript/tab_applescript.mm

Issue 8124024: Applescript: return value from execute javascript command (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed issues Created 9 years, 2 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 side-by-side diff with in-line comments
Download patch
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 8d19f4b491f1e61f9ba7f7b69f34b2fe97fe896a..5fcde167ba5da3c5dd64487e1e1bc2027f91a891 100644
--- a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
+++ b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
@@ -23,6 +23,99 @@
#include "content/common/view_messages.h"
#include "googleurl/src/gurl.h"
+@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 {
Avi (use Gerrit) 2011/10/17 14:20:38 nit: no space after )
keishi 2011/10/21 05:05:43 Done.
+ return descriptor.get();
+}
+
+@end
+
+static NSAppleEventDescriptor* valueToDescriptor(Value* value) {
+ NSAppleEventDescriptor* descriptor = nil;
+ switch (value->GetType()) {
+ case Value::TYPE_NULL:
+ descriptor = [NSAppleEventDescriptor
+ descriptorWithTypeCode:cMissingValue];
+ break;
+ case Value::TYPE_BOOLEAN: {
+ bool bool_value;
+ value->GetAsBoolean(&bool_value);
+ descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value];
+ break;
+ }
+ case Value::TYPE_INTEGER: {
+ int int_value;
+ value->GetAsInteger(&int_value);
+ descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value];
Avi (use Gerrit) 2011/10/17 14:20:38 Why int32? Is this future proof for 64-bits?
keishi 2011/10/21 05:05:43 JS integers are 32 bits.
+ break;
+ }
+ case Value::TYPE_DOUBLE: {
+ double double_value;
+ value->GetAsDouble(&double_value);
+ descriptor = [NSAppleEventDescriptor
+ descriptorWithDescriptorType:typeIEEE64BitFloatingPoint
+ bytes:&double_value
+ length:sizeof(double_value)];
+ break;
+ }
+ case Value::TYPE_STRING: {
+ std::string string_value;
+ value->GetAsString(&string_value);
+ descriptor = [NSAppleEventDescriptor descriptorWithString:
+ base::SysUTF8ToNSString(string_value)];
+ break;
+ }
+ case Value::TYPE_BINARY:
+ NOTREACHED();
+ break;
+ case Value::TYPE_DICTIONARY: {
+ DictionaryValue* dictionary_value = static_cast<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;
+ if (dictionary_value->Get(*iter, &item)) {
+ [userRecord insertDescriptor:[NSAppleEventDescriptor
+ descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0];
+ [userRecord insertDescriptor:valueToDescriptor(item) atIndex:0];
+ }
+ }
+ [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields];
Avi (use Gerrit) 2011/10/17 14:20:38 Can you find a link to put in here for keyASUserRe
keishi 2011/10/21 05:05:43 I can't find the right documentation from Apple so
Avi (use Gerrit) 2011/10/21 11:50:48 A plaintext description? http://www.mail-archive.c
+ break;
+ }
+ case Value::TYPE_LIST: {
+ ListValue* list_value;
+ value->GetAsList(&list_value);
+ descriptor = [NSAppleEventDescriptor listDescriptor];
+ for (unsigned i = 0; i < list_value->GetSize(); ++i) {
+ Value* item;
+ list_value->Get(i, &item);
+ [descriptor insertDescriptor:valueToDescriptor(item) atIndex:0];
Avi (use Gerrit) 2011/10/17 14:20:38 Doesn't this reverse the order of the items in the
keishi 2011/10/21 05:05:43 AppleScript lists are 1 based and specifying index
Avi (use Gerrit) 2011/10/21 11:50:48 Sigh. Been way too long since I've done this.
+ }
+ break;
+ }
+ }
+ return descriptor;
+}
+
@interface TabAppleScript()
@property (nonatomic, copy) NSString* tempURL;
@end
@@ -301,10 +394,9 @@
string16 script = base::SysNSStringToUTF16(
[[command evaluatedArguments] objectForKey:@"javascript"]);
- view->ExecuteJavascriptInWebFrame(string16(), script);
-
- // TODO(Shreyas): Figure out a way to get the response back.
- return nil;
+ Value* value = view->ExecuteJavascriptAndGetValue(string16(), script);
+ NSAppleEventDescriptor* descriptor = valueToDescriptor(value);
+ return [[[AnyResultValue alloc] initWithDescriptor:descriptor] autorelease];
Avi (use Gerrit) 2011/10/17 14:20:38 I'm a bit lost. We're returning a custom object? H
keishi 2011/10/21 05:05:43 The AppleScript calls selectors on the object base
Avi (use Gerrit) 2011/10/21 11:50:48 Gotcha.
}
@end
« no previous file with comments | « no previous file | content/browser/renderer_host/render_view_host.h » ('j') | content/browser/renderer_host/render_view_host.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698