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