forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_message_sender.cc
53 lines (41 loc) · 1.91 KB
/
post_message_sender.cc
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
// Copyright 2021 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 "pdf/post_message_sender.h"
#include <memory>
#include "base/check_op.h"
#include "base/values.h"
#include "content/public/renderer/v8_value_converter.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_dom_message_event.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_plugin_container.h"
#include "third_party/blink/public/web/web_serialized_script_value.h"
#include "v8/include/v8.h"
namespace chrome_pdf {
PostMessageSender::PostMessageSender() : isolate_(blink::MainThreadIsolate()) {}
PostMessageSender::~PostMessageSender() = default;
// TODO(crbug.com/1109796): This method is currently called from the renderer
// main thread, but will be called from a dedicated plugin thread in the future.
// When that happens, the body of this method needs to be posted to the main
// thread as a task because that's where the Blink and V8 interactions need to
// occur.
void PostMessageSender::Post(base::Value message) {
// Drop messages if there is no container.
if (!container_)
return;
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
container_->GetDocument().GetFrame()->MainWorldScriptContext();
DCHECK_EQ(isolate_, context->GetIsolate());
v8::Context::Scope context_scope(context);
if (!v8_value_converter_)
v8_value_converter_ = content::V8ValueConverter::Create();
v8::Local<v8::Value> converted_message =
v8_value_converter_->ToV8Value(&message, context);
container_->EnqueueMessageEvent(
blink::WebSerializedScriptValue::Serialize(isolate_, converted_message));
}
} // namespace chrome_pdf