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 { | |
Avi (use Gerrit)
2011/10/17 14:20:38
nit: no space after )
keishi
2011/10/21 05:05:43
Done.
| |
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]; | |
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.
| |
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 [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
| |
102 break; | |
103 } | |
104 case Value::TYPE_LIST: { | |
105 ListValue* list_value; | |
106 value->GetAsList(&list_value); | |
107 descriptor = [NSAppleEventDescriptor listDescriptor]; | |
108 for (unsigned i = 0; i < list_value->GetSize(); ++i) { | |
109 Value* item; | |
110 list_value->Get(i, &item); | |
111 [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.
| |
112 } | |
113 break; | |
114 } | |
115 } | |
116 return descriptor; | |
117 } | |
118 | |
26 @interface TabAppleScript() | 119 @interface TabAppleScript() |
27 @property (nonatomic, copy) NSString* tempURL; | 120 @property (nonatomic, copy) NSString* tempURL; |
28 @end | 121 @end |
29 | 122 |
30 @implementation TabAppleScript | 123 @implementation TabAppleScript |
31 | 124 |
32 @synthesize tempURL = tempURL_; | 125 @synthesize tempURL = tempURL_; |
33 | 126 |
34 - (id)init { | 127 - (id)init { |
35 if ((self = [super init])) { | 128 if ((self = [super init])) { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 | 387 |
295 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command { | 388 - (id)handlesExecuteJavascriptScriptCommand:(NSScriptCommand*)command { |
296 RenderViewHost* view = tabContents_->render_view_host(); | 389 RenderViewHost* view = tabContents_->render_view_host(); |
297 if (!view) { | 390 if (!view) { |
298 NOTREACHED(); | 391 NOTREACHED(); |
299 return nil; | 392 return nil; |
300 } | 393 } |
301 | 394 |
302 string16 script = base::SysNSStringToUTF16( | 395 string16 script = base::SysNSStringToUTF16( |
303 [[command evaluatedArguments] objectForKey:@"javascript"]); | 396 [[command evaluatedArguments] objectForKey:@"javascript"]); |
304 view->ExecuteJavascriptInWebFrame(string16(), script); | 397 Value* value = view->ExecuteJavascriptAndGetValue(string16(), script); |
305 | 398 NSAppleEventDescriptor* descriptor = valueToDescriptor(value); |
306 // TODO(Shreyas): Figure out a way to get the response back. | 399 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.
| |
307 return nil; | |
308 } | 400 } |
309 | 401 |
310 @end | 402 @end |
OLD | NEW |