Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / content / test / plugin / plugin_get_javascript_url_test.cc
blob13ccdcd155536fd857f9a15228ee0406d4e248d2
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.
5 #include "content/test/plugin/plugin_get_javascript_url_test.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
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
15 // The maximum chunk size of stream data.
16 #define STREAM_CHUNK 197
18 #if defined(OS_WIN)
19 const int kNPNEvaluateTimerID = 100;
20 const int kNPNEvaluateTimerElapse = 50;
21 #endif
23 namespace NPAPIClient {
25 ExecuteGetJavascriptUrlTest::ExecuteGetJavascriptUrlTest(
26 NPP id, NPNetscapeFuncs *host_functions)
27 : PluginTest(id, host_functions),
28 test_started_(false),
29 #if defined(OS_WIN)
30 window_(NULL),
31 #endif
32 npn_evaluate_context_(false) {
35 NPError ExecuteGetJavascriptUrlTest::SetWindow(NPWindow* pNPWindow) {
36 #if !defined(OS_MACOSX)
37 if (pNPWindow->window == NULL)
38 return NPERR_NO_ERROR;
39 #endif
41 if (!test_started_) {
42 std::string url = SELF_URL;
43 HostFunctions()->geturlnotify(id(), url.c_str(), "_top",
44 reinterpret_cast<void*>(SELF_URL_STREAM_ID));
45 test_started_ = true;
47 #if defined(OS_WIN)
48 HWND window_handle = reinterpret_cast<HWND>(pNPWindow->window);
49 if (!::GetProp(window_handle, L"Plugin_Instance")) {
50 // TODO: this propery leaks.
51 ::SetProp(window_handle, L"Plugin_Instance", this);
52 // We attempt to retreive the NPObject for the plugin instance identified
53 // by the NPObjectLifetimeTestInstance2 class as it may not have been
54 // instantiated yet.
55 SetTimer(window_handle, kNPNEvaluateTimerID, kNPNEvaluateTimerElapse,
56 TimerProc);
58 window_ = window_handle;
59 #endif
62 return NPERR_NO_ERROR;
65 #if defined(OS_WIN)
66 void CALLBACK ExecuteGetJavascriptUrlTest::TimerProc(
67 HWND window, UINT message, UINT_PTR timer_id, DWORD elapsed_time) {
68 ExecuteGetJavascriptUrlTest* this_instance =
69 reinterpret_cast<ExecuteGetJavascriptUrlTest*>
70 (::GetProp(window, L"Plugin_Instance"));
71 CHECK(this_instance);
73 ::RemoveProp(window, L"Plugin_Instance");
75 NPObject *window_obj = NULL;
76 this_instance->HostFunctions()->getvalue(this_instance->id(),
77 NPNVWindowNPObject,
78 &window_obj);
79 if (!window_obj) {
80 this_instance->SetError("Failed to get NPObject for plugin instance2");
81 this_instance->SignalTestCompleted();
82 return;
85 std::string script = "javascript:window.location";
86 NPString script_string;
87 script_string.UTF8Characters = script.c_str();
88 script_string.UTF8Length = static_cast<unsigned int>(script.length());
89 NPVariant result_var;
91 this_instance->npn_evaluate_context_ = true;
92 this_instance->HostFunctions()->evaluate(
93 this_instance->id(), window_obj, &script_string, &result_var);
94 this_instance->npn_evaluate_context_ = false;
96 #endif
98 NPError ExecuteGetJavascriptUrlTest::NewStream(NPMIMEType type,
99 NPStream* stream,
100 NPBool seekable,
101 uint16* stype) {
102 if (stream == NULL) {
103 SetError("NewStream got null stream");
104 return NPERR_INVALID_PARAM;
107 if (npn_evaluate_context_) {
108 SetError("NewStream received in context of NPN_Evaluate");
109 return NPERR_NO_ERROR;
112 static_assert(sizeof(unsigned long) <= sizeof(stream->notifyData),
113 "cast validity check");
114 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData);
115 switch (stream_id) {
116 case SELF_URL_STREAM_ID:
117 break;
118 default:
119 SetError("Unexpected NewStream callback");
120 break;
122 return NPERR_NO_ERROR;
125 int32 ExecuteGetJavascriptUrlTest::WriteReady(NPStream *stream) {
126 if (npn_evaluate_context_) {
127 SetError("WriteReady received in context of NPN_Evaluate");
128 return NPERR_NO_ERROR;
130 return STREAM_CHUNK;
133 int32 ExecuteGetJavascriptUrlTest::Write(NPStream *stream, int32 offset,
134 int32 len, void *buffer) {
135 if (stream == NULL) {
136 SetError("Write got null stream");
137 return -1;
139 if (len < 0 || len > STREAM_CHUNK) {
140 SetError("Write got bogus stream chunk size");
141 return -1;
144 if (npn_evaluate_context_) {
145 SetError("Write received in context of NPN_Evaluate");
146 return len;
149 static_assert(sizeof(unsigned long) <= sizeof(stream->notifyData),
150 "cast validity check");
151 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData);
152 switch (stream_id) {
153 case SELF_URL_STREAM_ID:
154 self_url_.append(static_cast<char*>(buffer), len);
155 break;
156 default:
157 SetError("Unexpected write callback");
158 break;
160 // Pretend that we took all the data.
161 return len;
165 NPError ExecuteGetJavascriptUrlTest::DestroyStream(NPStream *stream,
166 NPError reason) {
167 if (stream == NULL) {
168 SetError("NewStream got null stream");
169 return NPERR_INVALID_PARAM;
172 #if defined(OS_WIN)
173 KillTimer(window_, kNPNEvaluateTimerID);
174 #endif
176 if (npn_evaluate_context_) {
177 SetError("DestroyStream received in context of NPN_Evaluate");
178 return NPERR_NO_ERROR;
181 static_assert(sizeof(unsigned long) <= sizeof(stream->notifyData),
182 "cast validity check");
183 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData);
184 switch (stream_id) {
185 case SELF_URL_STREAM_ID:
186 // don't care
187 break;
188 default:
189 SetError("Unexpected NewStream callback");
190 break;
192 return NPERR_NO_ERROR;
195 void ExecuteGetJavascriptUrlTest::URLNotify(const char* url, NPReason reason,
196 void* data) {
197 static_assert(sizeof(unsigned long) <= sizeof(data),
198 "cast validity check");
200 if (npn_evaluate_context_) {
201 SetError("URLNotify received in context of NPN_Evaluate");
202 return;
205 unsigned long stream_id = reinterpret_cast<unsigned long>(data);
206 switch (stream_id) {
207 case SELF_URL_STREAM_ID:
208 if (strcmp(url, SELF_URL) != 0)
209 SetError("URLNotify reported incorrect url for SELF_URL");
210 if (self_url_.empty())
211 SetError("Failed to obtain window location.");
212 SignalTestCompleted();
213 break;
214 default:
215 SetError("Unexpected NewStream callback");
216 break;
220 } // namespace NPAPIClient