1 // Copyright 2014 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.
5 #include "content/child/shared_worker_devtools_agent.h"
7 #include "content/child/child_thread_impl.h"
8 #include "content/common/devtools_messages.h"
9 #include "ipc/ipc_channel.h"
10 #include "third_party/WebKit/public/platform/WebCString.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebSharedWorker.h"
14 using blink::WebSharedWorker
;
15 using blink::WebString
;
19 static const size_t kMaxMessageChunkSize
=
20 IPC::Channel::kMaximumMessageSize
/ 4;
22 SharedWorkerDevToolsAgent::SharedWorkerDevToolsAgent(
24 WebSharedWorker
* webworker
)
25 : route_id_(route_id
),
26 webworker_(webworker
) {
29 SharedWorkerDevToolsAgent::~SharedWorkerDevToolsAgent() {
32 // Called on the Worker thread.
33 bool SharedWorkerDevToolsAgent::OnMessageReceived(const IPC::Message
& message
) {
35 IPC_BEGIN_MESSAGE_MAP(SharedWorkerDevToolsAgent
, message
)
36 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Attach
, OnAttach
)
37 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Reattach
, OnReattach
)
38 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach
, OnDetach
)
39 IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend
,
40 OnDispatchOnInspectorBackend
)
41 IPC_MESSAGE_UNHANDLED(handled
= false)
46 void SharedWorkerDevToolsAgent::SendDevToolsMessage(
48 const blink::WebString
& msg
,
49 const blink::WebString
& state
) {
50 std::string message
= msg
.utf8();
51 std::string post_state
= state
.utf8();
52 DevToolsMessageChunk chunk
;
53 chunk
.message_size
= message
.size();
54 chunk
.is_first
= true;
56 if (message
.length() < kMaxMessageChunkSize
) {
57 chunk
.data
.swap(message
);
58 chunk
.call_id
= call_id
;
59 chunk
.post_state
= post_state
;
61 Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
66 for (size_t pos
= 0; pos
< message
.length(); pos
+= kMaxMessageChunkSize
) {
67 chunk
.is_last
= pos
+ kMaxMessageChunkSize
>= message
.length();
68 chunk
.call_id
= chunk
.is_last
? call_id
: 0;
69 chunk
.post_state
= chunk
.is_last
? post_state
: std::string();
70 chunk
.data
= message
.substr(pos
, kMaxMessageChunkSize
);
71 Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
73 chunk
.is_first
= false;
74 chunk
.message_size
= 0;
78 void SharedWorkerDevToolsAgent::OnAttach(const std::string
& host_id
) {
79 webworker_
->attachDevTools(WebString::fromUTF8(host_id
));
82 void SharedWorkerDevToolsAgent::OnReattach(const std::string
& host_id
,
83 const std::string
& state
) {
84 webworker_
->reattachDevTools(WebString::fromUTF8(host_id
),
85 WebString::fromUTF8(state
));
88 void SharedWorkerDevToolsAgent::OnDetach() {
89 webworker_
->detachDevTools();
92 void SharedWorkerDevToolsAgent::OnDispatchOnInspectorBackend(
93 const std::string
& message
) {
94 webworker_
->dispatchDevToolsMessage(WebString::fromUTF8(message
));
97 bool SharedWorkerDevToolsAgent::Send(IPC::Message
* message
) {
98 return ChildThreadImpl::current()->Send(message
);
101 } // namespace content