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"
11 #define SELF_URL "javascript:window.location+\"\""
12 // The identifier for the self url stream.
13 #define SELF_URL_STREAM_ID 1
15 // The identifier for the fetched url stream.
16 #define FETCHED_URL_STREAM_ID 2
18 // The maximum chunk size of stream data.
19 #define STREAM_CHUNK 197
21 const int kNPNEvaluateTimerID
= 100;
22 const int kNPNEvaluateTimerElapse
= 50;
25 namespace NPAPIClient
{
27 ExecuteGetJavascriptUrlTest::ExecuteGetJavascriptUrlTest(
28 NPP id
, NPNetscapeFuncs
*host_functions
)
29 : PluginTest(id
, host_functions
),
34 npn_evaluate_context_(false) {
37 NPError
ExecuteGetJavascriptUrlTest::SetWindow(NPWindow
* pNPWindow
) {
38 #if !defined(OS_MACOSX)
39 if (pNPWindow
->window
== NULL
)
40 return NPERR_NO_ERROR
;
44 std::string url
= SELF_URL
;
45 HostFunctions()->geturlnotify(id(), url
.c_str(), "_top",
46 reinterpret_cast<void*>(SELF_URL_STREAM_ID
));
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
57 SetTimer(window_handle
, kNPNEvaluateTimerID
, kNPNEvaluateTimerElapse
,
60 window_
= window_handle
;
64 return NPERR_NO_ERROR
;
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"));
75 ::RemoveProp(window
, L
"Plugin_Instance");
77 NPObject
*window_obj
= NULL
;
78 this_instance
->HostFunctions()->getvalue(this_instance
->id(),
82 this_instance
->SetError("Failed to get NPObject for plugin instance2");
83 this_instance
->SignalTestCompleted();
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());
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;
100 NPError
ExecuteGetJavascriptUrlTest::NewStream(NPMIMEType type
,
104 if (stream
== NULL
) {
105 SetError("NewStream got null stream");
106 return NPERR_INVALID_PARAM
;
109 if (npn_evaluate_context_
) {
110 SetError("NewStream received in context of NPN_Evaluate");
111 return NPERR_NO_ERROR
;
114 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream
->notifyData
),
115 cast_validity_check
);
116 unsigned long stream_id
= reinterpret_cast<unsigned long>(stream
->notifyData
);
118 case SELF_URL_STREAM_ID
:
121 SetError("Unexpected NewStream callback");
124 return NPERR_NO_ERROR
;
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
;
135 int32
ExecuteGetJavascriptUrlTest::Write(NPStream
*stream
, int32 offset
,
136 int32 len
, void *buffer
) {
137 if (stream
== NULL
) {
138 SetError("Write got null stream");
141 if (len
< 0 || len
> STREAM_CHUNK
) {
142 SetError("Write got bogus stream chunk size");
146 if (npn_evaluate_context_
) {
147 SetError("Write received in context of NPN_Evaluate");
151 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream
->notifyData
),
152 cast_validity_check
);
153 unsigned long stream_id
= reinterpret_cast<unsigned long>(stream
->notifyData
);
155 case SELF_URL_STREAM_ID
:
156 self_url_
.append(static_cast<char*>(buffer
), len
);
159 SetError("Unexpected write callback");
162 // Pretend that we took all the data.
167 NPError
ExecuteGetJavascriptUrlTest::DestroyStream(NPStream
*stream
,
169 if (stream
== NULL
) {
170 SetError("NewStream got null stream");
171 return NPERR_INVALID_PARAM
;
175 KillTimer(window_
, kNPNEvaluateTimerID
);
178 if (npn_evaluate_context_
) {
179 SetError("DestroyStream received in context of NPN_Evaluate");
180 return NPERR_NO_ERROR
;
183 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream
->notifyData
),
184 cast_validity_check
);
185 unsigned long stream_id
= reinterpret_cast<unsigned long>(stream
->notifyData
);
187 case SELF_URL_STREAM_ID
:
191 SetError("Unexpected NewStream callback");
194 return NPERR_NO_ERROR
;
197 void ExecuteGetJavascriptUrlTest::URLNotify(const char* url
, NPReason reason
,
199 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(data
),
200 cast_validity_check
);
202 if (npn_evaluate_context_
) {
203 SetError("URLNotify received in context of NPN_Evaluate");
207 unsigned long stream_id
= reinterpret_cast<unsigned long>(data
);
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();
217 SetError("Unexpected NewStream callback");
222 } // namespace NPAPIClient