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 "content/test/plugin/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 maximum chunk size of stream data. 16 #define STREAM_CHUNK 197 17 18 #if defined(OS_WIN) 19 const int kNPNEvaluateTimerID = 100; 20 const int kNPNEvaluateTimerElapse = 50; 21 #endif 22 23 namespace NPAPIClient { 24 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) { 33 } 34 35 NPError ExecuteGetJavascriptUrlTest::SetWindow(NPWindow* pNPWindow) { 36 #if !defined(OS_MACOSX) 37 if (pNPWindow->window == NULL) 38 return NPERR_NO_ERROR; 39 #endif 40 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; 46 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); 57 } 58 window_ = window_handle; 59 #endif 60 } 61 62 return NPERR_NO_ERROR; 63 } 64 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); 72 73 ::RemoveProp(window, L"Plugin_Instance"); 74 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; 83 } 84 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; 90 91 this_instance->npn_evaluate_context_ = true; 92 NPError result = this_instance->HostFunctions()->evaluate( 93 this_instance->id(), window_obj, &script_string, &result_var); 94 this_instance->npn_evaluate_context_ = false; 95 } 96 #endif 97 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; 105 } 106 107 if (npn_evaluate_context_) { 108 SetError("NewStream received in context of NPN_Evaluate"); 109 return NPERR_NO_ERROR; 110 } 111 112 COMPILE_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; 121 } 122 return NPERR_NO_ERROR; 123 } 124 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; 129 } 130 return STREAM_CHUNK; 131 } 132 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; 138 } 139 if (len < 0 || len > STREAM_CHUNK) { 140 SetError("Write got bogus stream chunk size"); 141 return -1; 142 } 143 144 if (npn_evaluate_context_) { 145 SetError("Write received in context of NPN_Evaluate"); 146 return len; 147 } 148 149 COMPILE_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; 159 } 160 // Pretend that we took all the data. 161 return len; 162 } 163 164 165 NPError ExecuteGetJavascriptUrlTest::DestroyStream(NPStream *stream, 166 NPError reason) { 167 if (stream == NULL) { 168 SetError("NewStream got null stream"); 169 return NPERR_INVALID_PARAM; 170 } 171 172 #if defined(OS_WIN) 173 KillTimer(window_, kNPNEvaluateTimerID); 174 #endif 175 176 if (npn_evaluate_context_) { 177 SetError("DestroyStream received in context of NPN_Evaluate"); 178 return NPERR_NO_ERROR; 179 } 180 181 COMPILE_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; 191 } 192 return NPERR_NO_ERROR; 193 } 194 195 void ExecuteGetJavascriptUrlTest::URLNotify(const char* url, NPReason reason, 196 void* data) { 197 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(data), 198 cast_validity_check); 199 200 if (npn_evaluate_context_) { 201 SetError("URLNotify received in context of NPN_Evaluate"); 202 return; 203 } 204 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; 217 } 218 } 219 220 } // namespace NPAPIClient 221