| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #import "base/memory/scoped_nsobject.h" | 9 #import "base/memory/scoped_nsobject.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/printing/print_view_manager.h" | 12 #include "chrome/browser/printing/print_view_manager.h" |
| 13 #include "chrome/browser/sessions/restore_tab_helper.h" | 13 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 14 #include "chrome/browser/sessions/session_id.h" | 14 #include "chrome/browser/sessions/session_id.h" |
| 15 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h" | 15 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/browser/download/save_package.h" | 18 #include "content/browser/download/save_package.h" |
| 19 #include "content/browser/renderer_host/render_view_host.h" | 19 #include "content/browser/renderer_host/render_view_host.h" |
| 20 #include "content/browser/tab_contents/navigation_controller.h" | 20 #include "content/browser/tab_contents/navigation_controller.h" |
| 21 #include "content/browser/tab_contents/navigation_entry.h" | 21 #include "content/browser/tab_contents/navigation_entry.h" |
| 22 #include "content/browser/tab_contents/tab_contents_delegate.h" | 22 #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 23 #include "content/common/view_messages.h" | 23 #include "content/common/view_messages.h" |
| 24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 25 | 25 |
| 26 @interface AnyResultValue : NSObject { |
| 27 @private |
| 28 scoped_nsobject<NSAppleEventDescriptor> descriptor; |
| 29 } |
| 30 - (id)initWithDescriptor:(NSAppleEventDescriptor*)desc; |
| 31 - (NSAppleEventDescriptor *)scriptingAnyDescriptor; |
| 32 @end |
| 33 |
| 34 @implementation AnyResultValue |
| 35 |
| 36 - (id)initWithDescriptor:(NSAppleEventDescriptor*)desc { |
| 37 if (self = [super init]) { |
| 38 descriptor.reset([desc retain]); |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (NSAppleEventDescriptor *)scriptingAnyDescriptor { |
| 44 return descriptor.get(); |
| 45 } |
| 46 |
| 47 @end |
| 48 |
| 49 static NSAppleEventDescriptor* valueToDescriptor(Value* value) { |
| 50 NSAppleEventDescriptor* descriptor = nil; |
| 51 switch (value->GetType()) { |
| 52 case Value::TYPE_NULL: |
| 53 descriptor = [NSAppleEventDescriptor |
| 54 descriptorWithTypeCode:cMissingValue]; |
| 55 break; |
| 56 case Value::TYPE_BOOLEAN: { |
| 57 bool bool_value; |
| 58 value->GetAsBoolean(&bool_value); |
| 59 descriptor = [NSAppleEventDescriptor descriptorWithBoolean:bool_value]; |
| 60 break; |
| 61 } |
| 62 case Value::TYPE_INTEGER: { |
| 63 int int_value; |
| 64 value->GetAsInteger(&int_value); |
| 65 descriptor = [NSAppleEventDescriptor descriptorWithInt32:int_value]; |
| 66 break; |
| 67 } |
| 68 case Value::TYPE_DOUBLE: { |
| 69 double double_value; |
| 70 value->GetAsDouble(&double_value); |
| 71 descriptor = [NSAppleEventDescriptor |
| 72 descriptorWithDescriptorType:typeIEEE64BitFloatingPoint |
| 73 bytes:&double_value |
| 74 length:sizeof(double_value)]; |
| 75 break; |
| 76 } |
| 77 case Value::TYPE_STRING: { |
| 78 std::string string_value; |
| 79 value->GetAsString(&string_value); |
| 80 descriptor = [NSAppleEventDescriptor descriptorWithString: |
| 81 base::SysUTF8ToNSString(string_value)]; |
| 82 break; |
| 83 } |
| 84 case Value::TYPE_BINARY: |
| 85 NOTREACHED(); |
| 86 break; |
| 87 case Value::TYPE_DICTIONARY: { |
| 88 DictionaryValue* dictionary_value = static_cast<DictionaryValue*>(value); |
| 89 descriptor = [NSAppleEventDescriptor recordDescriptor]; |
| 90 NSAppleEventDescriptor* userRecord = [NSAppleEventDescriptor |
| 91 listDescriptor]; |
| 92 for (DictionaryValue::key_iterator iter(dictionary_value->begin_keys()); |
| 93 iter != dictionary_value->end_keys(); ++iter) { |
| 94 Value* item; |
| 95 if (dictionary_value->Get(*iter, &item)) { |
| 96 [userRecord insertDescriptor:[NSAppleEventDescriptor |
| 97 descriptorWithString:base::SysUTF8ToNSString(*iter)] atIndex:0]; |
| 98 [userRecord insertDescriptor:valueToDescriptor(item) atIndex:0]; |
| 99 } |
| 100 } |
| 101 // Description of what keyASUserRecordFields does. |
| 102 // http://www.mail-archive.com/cocoa-dev%40lists.apple.com/msg40149.html |
| 103 [descriptor setDescriptor:userRecord forKeyword:keyASUserRecordFields]; |
| 104 break; |
| 105 } |
| 106 case Value::TYPE_LIST: { |
| 107 ListValue* list_value; |
| 108 value->GetAsList(&list_value); |
| 109 descriptor = [NSAppleEventDescriptor listDescriptor]; |
| 110 for (unsigned i = 0; i < list_value->GetSize(); ++i) { |
| 111 Value* item; |
| 112 list_value->Get(i, &item); |
| 113 [descriptor insertDescriptor:valueToDescriptor(item) atIndex:0]; |
| 114 } |
| 115 break; |
| 116 } |
| 117 } |
| 118 return descriptor; |
| 119 } |
| 120 |
| 26 @interface TabAppleScript() | 121 @interface TabAppleScript() |
| 27 @property (nonatomic, copy) NSString* tempURL; | 122 @property (nonatomic, copy) NSString* tempURL; |
| 28 @end | 123 @end |
| 29 | 124 |
| 30 @implementation TabAppleScript | 125 @implementation TabAppleScript |
| 31 | 126 |
| 32 @synthesize tempURL = tempURL_; | 127 @synthesize tempURL = tempURL_; |
| 33 | 128 |
| 34 - (id)init { | 129 - (id)init { |
| 35 if ((self = [super init])) { | 130 if ((self = [super init])) { |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 389 |
| 295 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command { | 390 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command { |
| 296 RenderViewHost* view = tabContents_->render_view_host(); | 391 RenderViewHost* view = tabContents_->render_view_host(); |
| 297 if (!view) { | 392 if (!view) { |
| 298 NOTREACHED(); | 393 NOTREACHED(); |
| 299 return nil; | 394 return nil; |
| 300 } | 395 } |
| 301 | 396 |
| 302 string16 script = base::SysNSStringToUTF16( | 397 string16 script = base::SysNSStringToUTF16( |
| 303 [[command evaluatedArguments] objectForKey:@"javascript"]); | 398 [[command evaluatedArguments] objectForKey:@"javascript"]); |
| 304 view->ExecuteJavascriptInWebFrame(string16(), script); | 399 Value* value = view->ExecuteJavascriptAndGetValue(string16(), script); |
| 305 | 400 NSAppleEventDescriptor* descriptor = valueToDescriptor(value); |
| 306 // TODO(Shreyas): Figure out a way to get the response back. | 401 return [[[AnyResultValue alloc] initWithDescriptor:descriptor] autorelease]; |
| 307 return nil; | |
| 308 } | 402 } |
| 309 | 403 |
| 310 @end | 404 @end |
| OLD | NEW |