OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/plugins/npapi/test/plugin_get_javascript_url_test.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | |
9 | |
10 // url for "self". | |
11 #define SELF_URL "javascript:window.location+\"\"" | |
12 // The identifier for the self url stream. | |
13 #define SELF_URL_STREAM_ID 1 | |
14 | |
15 // The identifier for the fetched url stream. | |
16 #define FETCHED_URL_STREAM_ID 2 | |
17 | |
18 // The maximum chunk size of stream data. | |
19 #define STREAM_CHUNK 197 | |
20 | |
21 const int kNPNEvaluateTimerID = 100; | |
22 const int kNPNEvaluateTimerElapse = 50; | |
23 | |
24 | |
25 namespace NPAPIClient { | |
26 | |
27 ExecuteGetJavascriptUrlTest::ExecuteGetJavascriptUrlTest( | |
28 NPP id, NPNetscapeFuncs *host_functions) | |
29 : PluginTest(id, host_functions), | |
30 test_started_(false), | |
31 #if defined(OS_WIN) | |
32 window_(NULL), | |
33 #endif | |
34 npn_evaluate_context_(false) { | |
35 } | |
36 | |
37 NPError ExecuteGetJavascriptUrlTest::SetWindow(NPWindow* pNPWindow) { | |
38 #if !defined(OS_MACOSX) | |
39 if (pNPWindow->window == NULL) | |
40 return NPERR_NO_ERROR; | |
41 #endif | |
42 | |
43 if (!test_started_) { | |
44 std::string url = SELF_URL; | |
45 HostFunctions()->geturlnotify(id(), url.c_str(), "_top", | |
46 reinterpret_cast<void*>(SELF_URL_STREAM_ID)); | |
47 test_started_ = true; | |
48 | |
49 #if defined(OS_WIN) | |
50 HWND window_handle = reinterpret_cast<HWND>(pNPWindow->window); | |
51 if (!::GetProp(window_handle, L"Plugin_Instance")) { | |
52 // TODO: this propery leaks. | |
53 ::SetProp(window_handle, L"Plugin_Instance", this); | |
54 // We attempt to retreive the NPObject for the plugin instance identified | |
55 // by the NPObjectLifetimeTestInstance2 class as it may not have been | |
56 // instantiated yet. | |
57 SetTimer(window_handle, kNPNEvaluateTimerID, kNPNEvaluateTimerElapse, | |
58 TimerProc); | |
59 } | |
60 window_ = window_handle; | |
61 #endif | |
62 } | |
63 | |
64 return NPERR_NO_ERROR; | |
65 } | |
66 | |
67 #if defined(OS_WIN) | |
68 void CALLBACK ExecuteGetJavascriptUrlTest::TimerProc( | |
69 HWND window, UINT message, UINT_PTR timer_id, DWORD elapsed_time) { | |
70 ExecuteGetJavascriptUrlTest* this_instance = | |
71 reinterpret_cast<ExecuteGetJavascriptUrlTest*> | |
72 (::GetProp(window, L"Plugin_Instance")); | |
73 CHECK(this_instance); | |
74 | |
75 ::RemoveProp(window, L"Plugin_Instance"); | |
76 | |
77 NPObject *window_obj = NULL; | |
78 this_instance->HostFunctions()->getvalue(this_instance->id(), | |
79 NPNVWindowNPObject, | |
80 &window_obj); | |
81 if (!window_obj) { | |
82 this_instance->SetError("Failed to get NPObject for plugin instance2"); | |
83 this_instance->SignalTestCompleted(); | |
84 return; | |
85 } | |
86 | |
87 std::string script = "javascript:window.location"; | |
88 NPString script_string; | |
89 script_string.UTF8Characters = script.c_str(); | |
90 script_string.UTF8Length = static_cast<unsigned int>(script.length()); | |
91 NPVariant result_var; | |
92 | |
93 this_instance->npn_evaluate_context_ = true; | |
94 NPError result = this_instance->HostFunctions()->evaluate( | |
95 this_instance->id(), window_obj, &script_string, &result_var); | |
96 this_instance->npn_evaluate_context_ = false; | |
97 } | |
98 #endif | |
99 | |
100 NPError ExecuteGetJavascriptUrlTest::NewStream(NPMIMEType type, | |
101 NPStream* stream, | |
102 NPBool seekable, | |
103 uint16* stype) { | |
104 if (stream == NULL) { | |
105 SetError("NewStream got null stream"); | |
106 return NPERR_INVALID_PARAM; | |
107 } | |
108 | |
109 if (npn_evaluate_context_) { | |
110 SetError("NewStream received in context of NPN_Evaluate"); | |
111 return NPERR_NO_ERROR; | |
112 } | |
113 | |
114 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), | |
115 cast_validity_check); | |
116 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); | |
117 switch (stream_id) { | |
118 case SELF_URL_STREAM_ID: | |
119 break; | |
120 default: | |
121 SetError("Unexpected NewStream callback"); | |
122 break; | |
123 } | |
124 return NPERR_NO_ERROR; | |
125 } | |
126 | |
127 int32 ExecuteGetJavascriptUrlTest::WriteReady(NPStream *stream) { | |
128 if (npn_evaluate_context_) { | |
129 SetError("WriteReady received in context of NPN_Evaluate"); | |
130 return NPERR_NO_ERROR; | |
131 } | |
132 return STREAM_CHUNK; | |
133 } | |
134 | |
135 int32 ExecuteGetJavascriptUrlTest::Write(NPStream *stream, int32 offset, | |
136 int32 len, void *buffer) { | |
137 if (stream == NULL) { | |
138 SetError("Write got null stream"); | |
139 return -1; | |
140 } | |
141 if (len < 0 || len > STREAM_CHUNK) { | |
142 SetError("Write got bogus stream chunk size"); | |
143 return -1; | |
144 } | |
145 | |
146 if (npn_evaluate_context_) { | |
147 SetError("Write received in context of NPN_Evaluate"); | |
148 return len; | |
149 } | |
150 | |
151 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), | |
152 cast_validity_check); | |
153 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); | |
154 switch (stream_id) { | |
155 case SELF_URL_STREAM_ID: | |
156 self_url_.append(static_cast<char*>(buffer), len); | |
157 break; | |
158 default: | |
159 SetError("Unexpected write callback"); | |
160 break; | |
161 } | |
162 // Pretend that we took all the data. | |
163 return len; | |
164 } | |
165 | |
166 | |
167 NPError ExecuteGetJavascriptUrlTest::DestroyStream(NPStream *stream, | |
168 NPError reason) { | |
169 if (stream == NULL) { | |
170 SetError("NewStream got null stream"); | |
171 return NPERR_INVALID_PARAM; | |
172 } | |
173 | |
174 #if defined(OS_WIN) | |
175 KillTimer(window_, kNPNEvaluateTimerID); | |
176 #endif | |
177 | |
178 if (npn_evaluate_context_) { | |
179 SetError("DestroyStream received in context of NPN_Evaluate"); | |
180 return NPERR_NO_ERROR; | |
181 } | |
182 | |
183 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), | |
184 cast_validity_check); | |
185 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); | |
186 switch (stream_id) { | |
187 case SELF_URL_STREAM_ID: | |
188 // don't care | |
189 break; | |
190 default: | |
191 SetError("Unexpected NewStream callback"); | |
192 break; | |
193 } | |
194 return NPERR_NO_ERROR; | |
195 } | |
196 | |
197 void ExecuteGetJavascriptUrlTest::URLNotify(const char* url, NPReason reason, | |
198 void* data) { | |
199 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(data), | |
200 cast_validity_check); | |
201 | |
202 if (npn_evaluate_context_) { | |
203 SetError("URLNotify received in context of NPN_Evaluate"); | |
204 return; | |
205 } | |
206 | |
207 unsigned long stream_id = reinterpret_cast<unsigned long>(data); | |
208 switch (stream_id) { | |
209 case SELF_URL_STREAM_ID: | |
210 if (strcmp(url, SELF_URL) != 0) | |
211 SetError("URLNotify reported incorrect url for SELF_URL"); | |
212 if (self_url_.empty()) | |
213 SetError("Failed to obtain window location."); | |
214 SignalTestCompleted(); | |
215 break; | |
216 default: | |
217 SetError("Unexpected NewStream callback"); | |
218 break; | |
219 } | |
220 } | |
221 | |
222 } // namespace NPAPIClient | |
OLD | NEW |