OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" | 5 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | |
12 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
13 #include "base/string16.h" | 12 #include "base/string16.h" |
14 #include "base/string_split.h" | 13 #include "base/string_split.h" |
15 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
16 #include "content/renderer/browser_plugin/browser_plugin.h" | 15 #include "content/renderer/browser_plugin/browser_plugin.h" |
17 #include "third_party/npapi/bindings/npapi.h" | 16 #include "third_party/npapi/bindings/npapi.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
(...skipping 10 matching lines...) Expand all Loading... |
32 using WebKit::WebDOMMessageEvent; | 31 using WebKit::WebDOMMessageEvent; |
33 using WebKit::WebPluginContainer; | 32 using WebKit::WebPluginContainer; |
34 using WebKit::WebSerializedScriptValue; | 33 using WebKit::WebSerializedScriptValue; |
35 using WebKit::WebString; | 34 using WebKit::WebString; |
36 | 35 |
37 namespace content { | 36 namespace content { |
38 | 37 |
39 namespace { | 38 namespace { |
40 | 39 |
41 const char kAddEventListener[] = "addEventListener"; | 40 const char kAddEventListener[] = "addEventListener"; |
| 41 const char kBackMethod[] = "back"; |
| 42 const char kForwardMethod[] = "forward"; |
42 const char kGetProcessId[] = "getProcessId"; | 43 const char kGetProcessId[] = "getProcessId"; |
| 44 const char kGoMethod[] = "go"; |
43 const char kPartitionAttribute[] = "partition"; | 45 const char kPartitionAttribute[] = "partition"; |
44 const char kReloadMethod[] = "reload"; | 46 const char kReloadMethod[] = "reload"; |
45 const char kRemoveEventListener[] = "removeEventListener"; | 47 const char kRemoveEventListener[] = "removeEventListener"; |
46 const char kSrcAttribute[] = "src"; | 48 const char kSrcAttribute[] = "src"; |
47 const char kStopMethod[] = "stop"; | 49 const char kStopMethod[] = "stop"; |
48 | 50 |
49 BrowserPluginBindings* GetBindings(NPObject* object) { | 51 BrowserPluginBindings* GetBindings(NPObject* object) { |
50 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> | 52 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> |
51 message_channel; | 53 message_channel; |
52 } | 54 } |
53 | 55 |
54 bool IdentifierIsReload(NPIdentifier identifier) { | 56 bool IdentifierIsReload(NPIdentifier identifier) { |
55 return WebBindings::getStringIdentifier(kReloadMethod) == identifier; | 57 return WebBindings::getStringIdentifier(kReloadMethod) == identifier; |
56 } | 58 } |
57 | 59 |
58 bool IdentifierIsStop(NPIdentifier identifier) { | 60 bool IdentifierIsStop(NPIdentifier identifier) { |
59 return WebBindings::getStringIdentifier(kStopMethod) == identifier; | 61 return WebBindings::getStringIdentifier(kStopMethod) == identifier; |
60 } | 62 } |
61 | 63 |
62 bool IdentifierIsAddEventListener(NPIdentifier identifier) { | 64 bool IdentifierIsAddEventListener(NPIdentifier identifier) { |
63 return WebBindings::getStringIdentifier(kAddEventListener) == identifier; | 65 return WebBindings::getStringIdentifier(kAddEventListener) == identifier; |
64 } | 66 } |
65 | 67 |
| 68 bool IdentifierIsBackMethod(NPIdentifier identifier) { |
| 69 return WebBindings::getStringIdentifier(kBackMethod) == identifier; |
| 70 } |
| 71 |
| 72 bool IdentifierIsForwardMethod(NPIdentifier identifier) { |
| 73 return WebBindings::getStringIdentifier(kForwardMethod) == identifier; |
| 74 } |
| 75 |
| 76 bool IdentifierIsGoMethod(NPIdentifier identifier) { |
| 77 return WebBindings::getStringIdentifier(kGoMethod) == identifier; |
| 78 } |
| 79 |
66 bool IdentifierIsRemoveEventListener(NPIdentifier identifier) { | 80 bool IdentifierIsRemoveEventListener(NPIdentifier identifier) { |
67 return WebBindings::getStringIdentifier(kRemoveEventListener) == identifier; | 81 return WebBindings::getStringIdentifier(kRemoveEventListener) == identifier; |
68 } | 82 } |
69 | 83 |
70 bool IdentifierIsGetProcessID(NPIdentifier identifier) { | 84 bool IdentifierIsGetProcessID(NPIdentifier identifier) { |
71 return WebBindings::getStringIdentifier(kGetProcessId) == identifier; | 85 return WebBindings::getStringIdentifier(kGetProcessId) == identifier; |
72 } | 86 } |
73 | 87 |
74 bool IdentifierIsSrcAttribute(NPIdentifier identifier) { | 88 bool IdentifierIsSrcAttribute(NPIdentifier identifier) { |
75 return WebBindings::getStringIdentifier(kSrcAttribute) == identifier; | 89 return WebBindings::getStringIdentifier(kSrcAttribute) == identifier; |
76 } | 90 } |
77 | 91 |
78 bool IdentifierIsPartitionAttribute(NPIdentifier identifier) { | 92 bool IdentifierIsPartitionAttribute(NPIdentifier identifier) { |
79 return WebBindings::getStringIdentifier(kPartitionAttribute) == identifier; | 93 return WebBindings::getStringIdentifier(kPartitionAttribute) == identifier; |
80 } | 94 } |
81 | 95 |
| 96 int Int32FromNPVariant(const NPVariant& variant) { |
| 97 if (NPVARIANT_IS_INT32(variant)) |
| 98 return NPVARIANT_TO_INT32(variant); |
| 99 |
| 100 if (NPVARIANT_IS_DOUBLE(variant)) |
| 101 return NPVARIANT_TO_DOUBLE(variant); |
| 102 |
| 103 return 0; |
| 104 } |
| 105 |
82 std::string StringFromNPVariant(const NPVariant& variant) { | 106 std::string StringFromNPVariant(const NPVariant& variant) { |
83 if (!NPVARIANT_IS_STRING(variant)) | 107 if (!NPVARIANT_IS_STRING(variant)) |
84 return std::string(); | 108 return std::string(); |
85 const NPString& np_string = NPVARIANT_TO_STRING(variant); | 109 const NPString& np_string = NPVARIANT_TO_STRING(variant); |
86 return std::string(np_string.UTF8Characters, np_string.UTF8Length); | 110 return std::string(np_string.UTF8Characters, np_string.UTF8Length); |
87 } | 111 } |
88 | 112 |
89 string16 String16FromNPVariant(const NPVariant& variant) { | 113 string16 String16FromNPVariant(const NPVariant& variant) { |
90 if (!NPVARIANT_IS_STRING(variant)) | 114 if (!NPVARIANT_IS_STRING(variant)) |
91 return string16(); | 115 return string16(); |
(...skipping 30 matching lines...) Expand all Loading... |
122 delete instance; | 146 delete instance; |
123 } | 147 } |
124 | 148 |
125 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { | 149 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { |
126 if (!np_obj) | 150 if (!np_obj) |
127 return false; | 151 return false; |
128 | 152 |
129 if (IdentifierIsAddEventListener(name)) | 153 if (IdentifierIsAddEventListener(name)) |
130 return true; | 154 return true; |
131 | 155 |
| 156 if (IdentifierIsBackMethod(name)) |
| 157 return true; |
| 158 |
| 159 if (IdentifierIsForwardMethod(name)) |
| 160 return true; |
| 161 |
132 if (IdentifierIsGetProcessID(name)) | 162 if (IdentifierIsGetProcessID(name)) |
133 return true; | 163 return true; |
134 | 164 |
| 165 if (IdentifierIsGoMethod(name)) |
| 166 return true; |
| 167 |
135 if (IdentifierIsReload(name)) | 168 if (IdentifierIsReload(name)) |
136 return true; | 169 return true; |
137 | 170 |
138 if (IdentifierIsRemoveEventListener(name)) | 171 if (IdentifierIsRemoveEventListener(name)) |
139 return true; | 172 return true; |
140 | 173 |
141 if (IdentifierIsStop(name)) | 174 if (IdentifierIsStop(name)) |
142 return true; | 175 return true; |
143 | 176 |
144 return false; | 177 return false; |
(...skipping 16 matching lines...) Expand all Loading... |
161 | 194 |
162 v8::Local<v8::Value> value = | 195 v8::Local<v8::Value> value = |
163 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); | 196 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); |
164 if (value.IsEmpty() || !value->IsFunction()) | 197 if (value.IsEmpty() || !value->IsFunction()) |
165 return false; | 198 return false; |
166 | 199 |
167 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | 200 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
168 return bindings->instance()->AddEventListener(event_name, function); | 201 return bindings->instance()->AddEventListener(event_name, function); |
169 } | 202 } |
170 | 203 |
| 204 if (IdentifierIsBackMethod(name) && !arg_count) { |
| 205 bindings->instance()->Back(); |
| 206 return true; |
| 207 } |
| 208 |
| 209 if (IdentifierIsForwardMethod(name) && !arg_count) { |
| 210 bindings->instance()->Forward(); |
| 211 return true; |
| 212 } |
| 213 |
171 if (IdentifierIsGetProcessID(name) && !arg_count) { | 214 if (IdentifierIsGetProcessID(name) && !arg_count) { |
172 int process_id = bindings->instance()->process_id(); | 215 int process_id = bindings->instance()->process_id(); |
173 result->type = NPVariantType_Int32; | 216 result->type = NPVariantType_Int32; |
174 result->value.intValue = process_id; | 217 result->value.intValue = process_id; |
175 return true; | 218 return true; |
176 } | 219 } |
177 | 220 |
| 221 if (IdentifierIsGoMethod(name) && arg_count == 1) { |
| 222 bindings->instance()->Go(Int32FromNPVariant(args[0])); |
| 223 return true; |
| 224 } |
| 225 |
178 if (IdentifierIsReload(name) && !arg_count) { | 226 if (IdentifierIsReload(name) && !arg_count) { |
179 bindings->instance()->Reload(); | 227 bindings->instance()->Reload(); |
180 return true; | 228 return true; |
181 } | 229 } |
182 | 230 |
183 if (IdentifierIsRemoveEventListener(name) && arg_count == 2) { | 231 if (IdentifierIsRemoveEventListener(name) && arg_count == 2) { |
184 std::string event_name = StringFromNPVariant(args[0]); | 232 std::string event_name = StringFromNPVariant(args[0]); |
185 if (event_name.empty()) | 233 if (event_name.empty()) |
186 return false; | 234 return false; |
187 | 235 |
(...skipping 29 matching lines...) Expand all Loading... |
217 } | 265 } |
218 | 266 |
219 bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name, | 267 bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name, |
220 NPVariant* result) { | 268 NPVariant* result) { |
221 if (!np_obj) | 269 if (!np_obj) |
222 return false; | 270 return false; |
223 | 271 |
224 if (!result) | 272 if (!result) |
225 return false; | 273 return false; |
226 | 274 |
227 if (IdentifierIsAddEventListener(name) || | |
228 IdentifierIsRemoveEventListener(name)) | |
229 return false; | |
230 | |
231 // All attributes from here on rely on the bindings, so retrieve it once and | 275 // All attributes from here on rely on the bindings, so retrieve it once and |
232 // return on failure. | 276 // return on failure. |
233 BrowserPluginBindings* bindings = GetBindings(np_obj); | 277 BrowserPluginBindings* bindings = GetBindings(np_obj); |
234 if (!bindings) | 278 if (!bindings) |
235 return false; | 279 return false; |
236 | 280 |
237 if (IdentifierIsSrcAttribute(name)) { | 281 if (IdentifierIsSrcAttribute(name)) { |
238 std::string src = bindings->instance()->GetSrcAttribute(); | 282 std::string src = bindings->instance()->GetSrcAttribute(); |
239 return StringToNPVariant(src, result); | 283 return StringToNPVariant(src, result); |
240 } | 284 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 WebBindings::createObject(NULL, &browser_plugin_message_class); | 364 WebBindings::createObject(NULL, &browser_plugin_message_class); |
321 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | 365 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
322 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | 366 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
323 } | 367 } |
324 | 368 |
325 BrowserPluginBindings::~BrowserPluginBindings() { | 369 BrowserPluginBindings::~BrowserPluginBindings() { |
326 WebBindings::releaseObject(np_object_); | 370 WebBindings::releaseObject(np_object_); |
327 } | 371 } |
328 | 372 |
329 } // namespace content | 373 } // namespace content |
OLD | NEW |