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