summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_message_handler.cc
blob: 54474f7033c4b0ef39251da6ba3c84269f3412f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_message_handler.h"

#include <string.h>
#include <algorithm>
#include <map>
#include <sstream>

#include "ppapi/c/pp_var.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/c/ppp_message_handler.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/file_system.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/var.h"
#include "ppapi/cpp/var_array.h"
#include "ppapi/cpp/var_array_buffer.h"
#include "ppapi/cpp/var_dictionary.h"
#include "ppapi/tests/pp_thread.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"

// Windows defines 'PostMessage', so we have to undef it.
#ifdef PostMessage
#undef PostMessage
#endif

REGISTER_TEST_CASE(MessageHandler);

namespace {

// Created and destroyed on the main thread. All public methods should be called
// on the main thread. Most data members are only accessed on the main thread.
// (Though it handles messages on the background thread).
class EchoingMessageHandler {
 public:
  explicit EchoingMessageHandler(PP_Instance instance,
                                 const pp::MessageLoop& loop)
      : pp_instance_(instance),
        message_handler_loop_(loop),
        ppb_messaging_if_(static_cast<const PPB_Messaging_1_1*>(
            pp::Module::Get()->GetBrowserInterface(
                PPB_MESSAGING_INTERFACE_1_1))),
        ppp_message_handler_if_(),
        is_registered_(false),
        test_finished_event_(instance),
        destroy_event_(instance) {
    AssertOnMainThread();
    ppp_message_handler_if_.HandleMessage = &HandleMessage;
    ppp_message_handler_if_.HandleBlockingMessage = &HandleBlockingMessage;
    ppp_message_handler_if_.Destroy = &Destroy;
  }
  void Register() {
    AssertOnMainThread();
    assert(!is_registered_);
    int32_t result = ppb_messaging_if_->RegisterMessageHandler(
        pp_instance_,
        this,
        &ppp_message_handler_if_,
        message_handler_loop_.pp_resource());
    if (result == PP_OK) {
      is_registered_ = true;
    } else {
      std::ostringstream stream;
      stream << "Failed to register message handler; got error " << result;
      AddError(stream.str());
      test_finished_event_.Signal();
    }
    // Note, at this point, we can't safely read or write errors_ until we wait
    // on destroy_event_.
  }
  void Unregister() {
    AssertOnMainThread();
    assert(is_registered_);
    ppb_messaging_if_->UnregisterMessageHandler(pp_instance_);
    is_registered_ = false;
  }
  void WaitForTestFinishedMessage() {
    test_finished_event_.Wait();
    test_finished_event_.Reset();
  }
  // Wait for Destroy() to be called on the MessageHandler thread. When it's
  // done, return any errors that occurred during the time the MessageHandler
  // was getting messages.
  std::string WaitForDestroy() {
    AssertOnMainThread();
    // If we haven't called Unregister, we'll be waiting forever.
    assert(!is_registered_);
    destroy_event_.Wait();
    destroy_event_.Reset();
    // Now that we know Destroy() has been called, we know errors_ isn't being
    // written on the MessageHandler thread anymore. So we can safely read it
    // here on the main thread (since destroy_event_ gave us a memory barrier).
    std::string temp_errors;
    errors_.swap(temp_errors);
    return temp_errors;
  }
 private:
  static void AssertOnMainThread() {
    assert(pp::MessageLoop::GetForMainThread() ==
           pp::MessageLoop::GetCurrent());
  }
  void AddError(const std::string& error) {
    if (!error.empty()) {
      if (!errors_.empty())
        errors_ += "<p>";
      errors_ += error;
    }
  }
  static void HandleMessage(PP_Instance instance,
                            void* user_data,
                            struct PP_Var message_data) {
    EchoingMessageHandler* thiz =
        static_cast<EchoingMessageHandler*>(user_data);
    if (pp::MessageLoop::GetCurrent() != thiz->message_handler_loop_)
      thiz->AddError("HandleMessage was called on the wrong thread!");
    if (instance != thiz->pp_instance_)
      thiz->AddError("HandleMessage was passed the wrong instance!");
    pp::Var var(message_data);
    if (var.is_string() && var.AsString() == "FINISHED_TEST")
      thiz->test_finished_event_.Signal();
    else
      thiz->ppb_messaging_if_->PostMessage(instance, message_data);
  }

  static PP_Var HandleBlockingMessage(PP_Instance instance,
                                      void* user_data,
                                      struct PP_Var message_data) {
    EchoingMessageHandler* thiz =
        static_cast<EchoingMessageHandler*>(user_data);
    if (pp::MessageLoop::GetCurrent() != thiz->message_handler_loop_)
      thiz->AddError("HandleBlockingMessage was called on the wrong thread!");
    if (instance != thiz->pp_instance_)
      thiz->AddError("HandleBlockingMessage was passed the wrong instance!");

    // The PP_Var we are passed is an in-parameter, so the browser is not
    // giving us a ref-count. The ref-count it has will be decremented after we
    // return. But we need to add a ref when returning a PP_Var, to pass to the
    // caller.
    pp::Var take_ref(message_data);
    take_ref.Detach();
    return message_data;
  }

  static void Destroy(PP_Instance instance, void* user_data) {
    EchoingMessageHandler* thiz =
        static_cast<EchoingMessageHandler*>(user_data);
    if (pp::MessageLoop::GetCurrent() != thiz->message_handler_loop_)
      thiz->AddError("Destroy was called on the wrong thread!");
    if (instance != thiz->pp_instance_)
      thiz->AddError("Destroy was passed the wrong instance!");
    thiz->destroy_event_.Signal();
  }

  // These data members are initialized on the main thread, but don't change for
  // the life of the object, so are safe to access on the background thread,
  // because there will be a memory barrier before the the MessageHandler calls
  // are invoked.
  const PP_Instance pp_instance_;
  const pp::MessageLoop message_handler_loop_;
  const pp::MessageLoop main_loop_;
  const PPB_Messaging_1_1* const ppb_messaging_if_;
  // Spiritually, this member is const, but we can't initialize it in C++03,
  // so it has to be non-const to be set in the constructor body.
  PPP_MessageHandler_0_1 ppp_message_handler_if_;

  // is_registered_ is only read/written on the main thread.
  bool is_registered_;

  // errors_ is written on the MessageHandler thread. When Destroy() is
  // called, we stop writing to errors_ and signal destroy_event_. This causes
  // a memory barrier, so it's safe to read errors_ after that.
  std::string errors_;
  NestedEvent test_finished_event_;
  NestedEvent destroy_event_;

  // Undefined & private to disallow copy and assign.
  EchoingMessageHandler(const EchoingMessageHandler&);
  EchoingMessageHandler& operator=(const EchoingMessageHandler&);
};

void FakeHandleMessage(PP_Instance instance,
                       void* user_data,
                       struct PP_Var message_data) {}
PP_Var FakeHandleBlockingMessage(PP_Instance instance,
                                 void* user_data,
                                 struct PP_Var message_data) {
  return PP_MakeUndefined();
}
void FakeDestroy(PP_Instance instance, void* user_data) {}

}  // namespace

TestMessageHandler::TestMessageHandler(TestingInstance* instance)
    : TestCase(instance),
      ppb_messaging_if_(NULL),
      handler_thread_(instance) {
}

TestMessageHandler::~TestMessageHandler() {
  handler_thread_.Join();
}

bool TestMessageHandler::Init() {
  ppb_messaging_if_ = static_cast<const PPB_Messaging_1_1*>(
      pp::Module::Get()->GetBrowserInterface(PPB_MESSAGING_INTERFACE_1_1));
  return ppb_messaging_if_ &&
         CheckTestingInterface() &&
         handler_thread_.Start();
}

void TestMessageHandler::RunTests(const std::string& filter) {
  RUN_TEST(RegisterErrorConditions, filter);
  RUN_TEST(PostMessageAndAwaitResponse, filter);
}

void TestMessageHandler::HandleMessage(const pp::Var& message_data) {
  // All messages should go to the background thread message handler.
  assert(false);
}

std::string TestMessageHandler::TestRegisterErrorConditions() {
  {
    // Test registering with the main thread as the message loop.
    PPP_MessageHandler_0_1 fake_ppp_message_handler = {
      &FakeHandleMessage, &FakeHandleBlockingMessage, &FakeDestroy
    };
    pp::MessageLoop main_loop = pp::MessageLoop::GetForMainThread();
    int32_t result = ppb_messaging_if_->RegisterMessageHandler(
        instance()->pp_instance(),
        reinterpret_cast<void*>(0xdeadbeef),
        &fake_ppp_message_handler,
        main_loop.pp_resource());
    ASSERT_EQ(PP_ERROR_WRONG_THREAD, result);
  }
  {
    // Test registering with incomplete PPP_Messaging interface.
    PPP_MessageHandler_0_1 bad_ppp_ifs[] = {
        { NULL, &FakeHandleBlockingMessage, &FakeDestroy },
        { &FakeHandleMessage, NULL, &FakeDestroy },
        { &FakeHandleMessage, &FakeHandleBlockingMessage, NULL }};
    for (size_t i = 0; i < sizeof(bad_ppp_ifs)/sizeof(bad_ppp_ifs[0]); ++i) {
      int32_t result = ppb_messaging_if_->RegisterMessageHandler(
          instance()->pp_instance(),
          reinterpret_cast<void*>(0xdeadbeef),
          &bad_ppp_ifs[i],
          handler_thread_.message_loop().pp_resource());
      ASSERT_EQ(PP_ERROR_BADARGUMENT, result);
    }
  }
  PASS();
}

std::string TestMessageHandler::TestPostMessageAndAwaitResponse() {
  EchoingMessageHandler handler(instance()->pp_instance(),
                                handler_thread_.message_loop());
  handler.Register();
  std::string js_code("var plugin = document.getElementById('plugin');\n");
  js_code += "var result = undefined;\n";
  const char* const values_to_test[] = {
      "5",
      "undefined",
      "1.5",
      "'hello'",
      "{'key': 'value', 'array_key': [1, 2, 3, 4, 5]}",
      NULL
  };
  for (size_t i = 0; values_to_test[i]; ++i) {
    js_code += "result = plugin.postMessageAndAwaitResponse(";
    js_code +=     values_to_test[i];
    js_code += ");\n";
    js_code += "if (!deepCompare(result, ";
    js_code +=     values_to_test[i];
    js_code += "))\n";
    js_code += "  InternalError(\" Failed postMessageAndAwaitResponse for: ";
    js_code +=      values_to_test[i];
    js_code +=    " result: \" + result);\n";
  }
  // TODO(dmichael): Setting a property uses GetInstanceObject, which sends sync
  // message, which can get interrupted with message to eval script, etc.
  // FINISHED_WAITING message can therefore jump ahead. This test is
  // currently carefully crafted to avoid races by doing all the JS in one call.
  // That should be fixed before this API goes to stable. See crbug.com/384528
  js_code += "plugin.postMessage('FINISHED_TEST');\n";
  instance_->EvalScript(js_code);
  handler.WaitForTestFinishedMessage();
  handler.Unregister();
  ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy());

  PASS();
}