1 // Copyright (c) 2010 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_url2_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 namespace NPAPIClient { 24 25 ExecuteGetJavascriptUrl2Test::ExecuteGetJavascriptUrl2Test( 26 NPP id, NPNetscapeFuncs *host_functions) 27 : PluginTest(id, host_functions), 28 test_started_(false) { 29 } 30 31 NPError ExecuteGetJavascriptUrl2Test::SetWindow(NPWindow* pNPWindow) { 32 #if !defined(OS_MACOSX) 33 if (pNPWindow->window == NULL) 34 return NPERR_NO_ERROR; 35 #endif 36 37 if (!test_started_) { 38 std::string url = SELF_URL; 39 HostFunctions()->geturlnotify(id(), url.c_str(), "_self", 40 reinterpret_cast<void*>(SELF_URL_STREAM_ID)); 41 test_started_ = true; 42 } 43 return NPERR_NO_ERROR; 44 } 45 46 NPError ExecuteGetJavascriptUrl2Test::NewStream(NPMIMEType type, NPStream* stream, 47 NPBool seekable, uint16* stype) { 48 if (stream == NULL) { 49 SetError("NewStream got null stream"); 50 return NPERR_INVALID_PARAM; 51 } 52 53 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), 54 cast_validity_check); 55 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); 56 switch (stream_id) { 57 case SELF_URL_STREAM_ID: 58 break; 59 default: 60 SetError("Unexpected NewStream callback"); 61 break; 62 } 63 return NPERR_NO_ERROR; 64 } 65 66 int32 ExecuteGetJavascriptUrl2Test::WriteReady(NPStream *stream) { 67 return STREAM_CHUNK; 68 } 69 70 int32 ExecuteGetJavascriptUrl2Test::Write(NPStream *stream, int32 offset, int32 len, 71 void *buffer) { 72 if (stream == NULL) { 73 SetError("Write got null stream"); 74 return -1; 75 } 76 if (len < 0 || len > STREAM_CHUNK) { 77 SetError("Write got bogus stream chunk size"); 78 return -1; 79 } 80 81 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), 82 cast_validity_check); 83 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); 84 switch (stream_id) { 85 case SELF_URL_STREAM_ID: 86 self_url_.append(static_cast<char*>(buffer), len); 87 break; 88 default: 89 SetError("Unexpected write callback"); 90 break; 91 } 92 // Pretend that we took all the data. 93 return len; 94 } 95 96 97 NPError ExecuteGetJavascriptUrl2Test::DestroyStream(NPStream *stream, NPError reason) { 98 if (stream == NULL) { 99 SetError("NewStream got null stream"); 100 return NPERR_INVALID_PARAM; 101 } 102 103 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(stream->notifyData), 104 cast_validity_check); 105 unsigned long stream_id = reinterpret_cast<unsigned long>(stream->notifyData); 106 switch (stream_id) { 107 case SELF_URL_STREAM_ID: 108 // don't care 109 break; 110 default: 111 SetError("Unexpected NewStream callback"); 112 break; 113 } 114 return NPERR_NO_ERROR; 115 } 116 117 void ExecuteGetJavascriptUrl2Test::URLNotify(const char* url, NPReason reason, void* data) { 118 COMPILE_ASSERT(sizeof(unsigned long) <= sizeof(data), 119 cast_validity_check); 120 121 unsigned long stream_id = reinterpret_cast<unsigned long>(data); 122 switch (stream_id) { 123 case SELF_URL_STREAM_ID: 124 if (strcmp(url, SELF_URL) != 0) 125 SetError("URLNotify reported incorrect url for SELF_URL"); 126 if (self_url_.empty()) 127 SetError("Failed to obtain window location."); 128 SignalTestCompleted(); 129 break; 130 default: 131 SetError("Unexpected NewStream callback"); 132 break; 133 } 134 } 135 136 } // namespace NPAPIClient 137