Chromium Code Reviews| Index: headless/lib/renderer/headless_content_renderer_client.cc |
| diff --git a/headless/lib/renderer/headless_content_renderer_client.cc b/headless/lib/renderer/headless_content_renderer_client.cc |
| index c4c263e62a728378c6b8c298ffc0ae40b55c1dc7..2d3e34548acc779b09503a0dee45cb8fe99ce78f 100644 |
| --- a/headless/lib/renderer/headless_content_renderer_client.cc |
| +++ b/headless/lib/renderer/headless_content_renderer_client.cc |
| @@ -5,6 +5,9 @@ |
| #include "headless/lib/renderer/headless_content_renderer_client.h" |
| #include "base/memory/ptr_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/public/common/bindings_policy.h" |
| +#include "content/public/renderer/render_frame.h" |
| #include "printing/features/features.h" |
| #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
| @@ -26,4 +29,60 @@ void HeadlessContentRendererClient::RenderFrameCreated( |
| #endif |
| } |
| +void HeadlessContentRendererClient::RunScriptsAtDocumentStart( |
| + content::RenderFrame* render_frame) { |
| + if (render_frame->GetEnabledBindings() & |
|
Sami
2017/04/12 14:18:46
nit: Could be an early out to reduce the indentati
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + content::BindingsPolicy::BINDINGS_POLICY_HEADLESS) { |
| + render_frame->ExecuteJavaScript(base::UTF8ToUTF16(R"( |
| + (function() { |
| + let tabSocket_ = null; |
| + let messages_to_send_ = []; |
|
Sami
2017/04/12 14:18:46
nit: messagesToSend
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + let listener_ = null; |
| + |
| + function getNextEmbedderMessage() { |
| + tabSocket_.awaitNextMessageFromEmbedder().then( |
| + function(result) { |
| + listener_(result.message); |
| + getNextEmbedderMessage(); |
| + }); |
| + }; |
| + |
| + window.TabSocket = {}; |
| + window.TabSocket.SendMessageToEmbedder = function(message) { |
|
Sami
2017/04/12 14:18:46
Maybe this should just also be send() to match Web
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + if (tabSocket_) { |
| + tabSocket_.sendMessageToEmbedder(message); |
| + } else { |
| + messages_to_send_.push(message); |
| + } |
| + }; |
| + |
| + window.TabSocket.SetListener = function(listener) { |
|
Sami
2017/04/12 14:18:46
Could we make the caller set "TabSocket.onmessage
alex clarke (OOO till 29th)
2017/04/12 15:47:46
Done.
|
| + listener_ = listener; |
| + if (tabSocket_) |
| + getNextEmbedderMessage(); |
| + }; |
| + |
| + // Note define() defines a module in the mojo module dependency |
| + // system. While we don't expose our module, the callback below only |
| + // fires after the requested modules have been loaded. |
| + define([ |
| + 'headless/lib/tab_socket.mojom', |
| + 'content/public/renderer/frame_interfaces', |
| + ], function(tabSocketMojom, frameInterfaces) { |
| + tabSocket_ = new tabSocketMojom.TabSocketPtr( |
| + frameInterfaces.getInterface(tabSocketMojom.TabSocket.name)); |
| + // Send any messages that may have been created before the dependency |
| + // was resolved. |
| + for (var i = 0; i < messages_to_send_.length; i++) { |
| + tabSocket_.sendMessageToEmbedder(messages_to_send_[i]); |
| + } |
| + messages_to_send_ = []; |
| + if (listener_) |
| + getNextEmbedderMessage(); |
| + }); |
| + |
| + })(); )")); |
| + } |
| +} |
| + |
| } // namespace headless |