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 maximum chunk size of stream data.
16 #define STREAM_CHUNK 197
19 const int kNPNEvaluateTimerID
= 100;
20 const int kNPNEvaluateTimerElapse
= 50;
23 namespace NPAPIClient
{
25 ExecuteGetJavascriptUrlTest::ExecuteGetJavascriptUrlTest(
26 NPP id
, NPNetscapeFuncs
*host_functions
)
27 : PluginTest(id
, host_functions
),
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
;
42 std::string url
= SELF_URL
;
43 HostFunctions()->geturlnotify(id(), url
.c_str(), "_top",
44 reinterpret_cast<void*>(SELF_URL_STREAM_ID
));
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
55 SetTimer(window_handle
, kNPNEvaluateTimerID
, kNPNEvaluateTimerElapse
,
58 window_
= window_handle
;
62 return NPERR_NO_ERROR
;
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"));
73 ::RemoveProp(window
, L
"Plugin_Instance");
75 NPObject
*window_obj
= NULL
;
76 this_instance
->HostFunctions()->getvalue(this_instance
->id(),
80 this_instance
->SetError("Failed to get NPObject for plugin instance2");
81 this_instance
->SignalTestCompleted();
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());
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;
98 NPError
ExecuteGetJavascriptUrlTest::NewStream(NPMIMEType type
,
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
);
116 case SELF_URL_STREAM_ID
:
119 SetError("Unexpected NewStream callback");
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
;
133 int32
ExecuteGetJavascriptUrlTest::Write(NPStream
*stream
, int32 offset
,
134 int32 len
, void *buffer
) {
135 if (stream
== NULL
) {
136 SetError("Write got null stream");
139 if (len
< 0 || len
> STREAM_CHUNK
) {
140 SetError("Write got bogus stream chunk size");
144 if (npn_evaluate_context_
) {
145 SetError("Write received in context of NPN_Evaluate");
149 static_assert(sizeof(unsigned long) <= sizeof(stream
->notifyData
),
150 "cast validity check");
151 unsigned long stream_id
= reinterpret_cast<unsigned long>(stream
->notifyData
);
153 case SELF_URL_STREAM_ID
:
154 self_url_
.append(static_cast<char*>(buffer
), len
);
157 SetError("Unexpected write callback");
160 // Pretend that we took all the data.
165 NPError
ExecuteGetJavascriptUrlTest::DestroyStream(NPStream
*stream
,
167 if (stream
== NULL
) {
168 SetError("NewStream got null stream");
169 return NPERR_INVALID_PARAM
;
173 KillTimer(window_
, kNPNEvaluateTimerID
);
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
);
185 case SELF_URL_STREAM_ID
:
189 SetError("Unexpected NewStream callback");
192 return NPERR_NO_ERROR
;
195 void ExecuteGetJavascriptUrlTest::URLNotify(const char* url
, NPReason reason
,
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");
205 unsigned long stream_id
= reinterpret_cast<unsigned long>(data
);
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();
215 SetError("Unexpected NewStream callback");
220 } // namespace NPAPIClient