OLD | NEW |
| (Empty) |
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 "webkit/glue/plugins/test/plugin_thread_async_call_test.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/thread.h" | |
10 #include "webkit/glue/plugins/test/plugin_client.h" | |
11 | |
12 namespace NPAPIClient { | |
13 | |
14 namespace { | |
15 | |
16 // There are two plugin instances in this test. The long lived instance is used | |
17 // for reporting errors and signalling test completion. The short lived one is | |
18 // used to verify that async callbacks are not invoked after NPP_Destroy. | |
19 PluginThreadAsyncCallTest* g_short_lived_instance; | |
20 PluginThreadAsyncCallTest* g_long_lived_instance; | |
21 | |
22 void OnCallSucceededHelper(void* data) { | |
23 static_cast<PluginThreadAsyncCallTest*>(data)->OnCallSucceeded(); | |
24 } | |
25 | |
26 class AsyncCallTask : public Task { | |
27 public: | |
28 AsyncCallTask(PluginThreadAsyncCallTest* test_class) | |
29 : test_class_(test_class) {} | |
30 | |
31 void Run() { | |
32 test_class_->AsyncCall(); | |
33 } | |
34 | |
35 private: | |
36 PluginThreadAsyncCallTest* test_class_; | |
37 }; | |
38 | |
39 void OnCallFailed(void* data) { | |
40 g_long_lived_instance->SetError("Async callback invoked after NPP_Destroy"); | |
41 } | |
42 | |
43 void OnCallCompletedHelper(void* data) { | |
44 static_cast<PluginThreadAsyncCallTest*>(data)->OnCallCompleted(); | |
45 } | |
46 } | |
47 | |
48 PluginThreadAsyncCallTest::PluginThreadAsyncCallTest( | |
49 NPP id, NPNetscapeFuncs *host_functions) | |
50 : PluginTest(id, host_functions) { | |
51 } | |
52 | |
53 NPError PluginThreadAsyncCallTest::New( | |
54 uint16 mode, int16 argc, const char* argn[], const char* argv[], | |
55 NPSavedData* saved) { | |
56 NPError error = PluginTest::New(mode, argc, argn, argv, saved); | |
57 if (error != NPERR_NO_ERROR) | |
58 return error; | |
59 | |
60 // Determine whether this is the short lived instance. | |
61 for (int i = 0; i < argc; ++i) { | |
62 if (base::strcasecmp(argn[i], "short_lived") == 0) { | |
63 if (base::strcasecmp(argv[i], "true") == 0) { | |
64 g_short_lived_instance = this; | |
65 } else { | |
66 g_long_lived_instance = this; | |
67 } | |
68 } | |
69 } | |
70 | |
71 // Schedule an async call that will succeed. Make sure to call that API from | |
72 // a different thread to fully test it. | |
73 if (this == g_short_lived_instance) { | |
74 at_exit_manager_.reset(new base::AtExitManager()); | |
75 base::Thread random_thread("random_thread"); | |
76 random_thread.Start(); | |
77 random_thread.message_loop()->PostTask(FROM_HERE, new AsyncCallTask(this)); | |
78 } | |
79 | |
80 return NPERR_NO_ERROR; | |
81 } | |
82 | |
83 void PluginThreadAsyncCallTest::AsyncCall() { | |
84 HostFunctions()->pluginthreadasynccall(id(), OnCallSucceededHelper, this); | |
85 } | |
86 | |
87 void PluginThreadAsyncCallTest::OnCallSucceeded() { | |
88 // Delete the short lived instance. | |
89 NPIdentifier delete_id = HostFunctions()->getstringidentifier( | |
90 "deleteShortLivedInstance"); | |
91 | |
92 NPObject *window_obj = NULL; | |
93 HostFunctions()->getvalue(id(), NPNVWindowNPObject, &window_obj); | |
94 | |
95 NPVariant result; | |
96 HostFunctions()->invoke(id(), window_obj, delete_id, NULL, 0, &result); | |
97 } | |
98 | |
99 NPError PluginThreadAsyncCallTest::Destroy() { | |
100 if (this == g_short_lived_instance) { | |
101 // Schedule an async call that should not be called. | |
102 HostFunctions()->pluginthreadasynccall(id(), OnCallFailed, NULL); | |
103 | |
104 // Schedule an async call to end the test using the long lived instance. | |
105 HostFunctions()->pluginthreadasynccall(g_long_lived_instance->id(), | |
106 OnCallCompletedHelper, | |
107 g_long_lived_instance); | |
108 } | |
109 | |
110 return NPERR_NO_ERROR; | |
111 } | |
112 | |
113 void PluginThreadAsyncCallTest::OnCallCompleted() { | |
114 SignalTestCompleted(); | |
115 } | |
116 | |
117 } // namespace NPAPIClient | |
OLD | NEW |