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

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: fix rebase mistake so it compiles 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
« no previous file with comments | « no previous file | content/browser/renderer_host/render_view_host.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..98fb76e5afca122900227fac5b913f2914139f92 100644
--- a/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
+++ b/chrome/browser/ui/cocoa/applescript/tab_applescript.mm
@@ -23,6 +23,101 @@
#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 {
+ 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];
+ 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];
+ }
+ }
+ // Description of what keyASUserRecordFields does.
+ // http://www.mail-archive.com/cocoa-dev%40lists.apple.com/msg40149.html
+ [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields];
+ 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];
+ }
+ break;
+ }
+ }
+ return descriptor;
+}
+
@interface TabAppleScript()
@property (nonatomic, copy) NSString* tempURL;
@end
@@ -301,10 +396,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];
}
@end
« no previous file with comments | « no previous file | content/browser/renderer_host/render_view_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698