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