summaryrefslogtreecommitdiffstats
path: root/content/child/shared_worker_devtools_agent.cc
blob: 3a9b3509b20f84d2a977140c2655599ac5b50d55 (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
// 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 "content/child/shared_worker_devtools_agent.h"

#include "content/child/child_thread.h"
#include "content/common/devtools_messages.h"
#include "ipc/ipc_channel.h"
#include "third_party/WebKit/public/platform/WebCString.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebSharedWorker.h"

using blink::WebSharedWorker;
using blink::WebString;

namespace content {

static const size_t kMaxMessageChunkSize =
    IPC::Channel::kMaximumMessageSize / 4;

SharedWorkerDevToolsAgent::SharedWorkerDevToolsAgent(
    int route_id,
    WebSharedWorker* webworker)
    : route_id_(route_id),
      webworker_(webworker) {
}

SharedWorkerDevToolsAgent::~SharedWorkerDevToolsAgent() {
}

// Called on the Worker thread.
bool SharedWorkerDevToolsAgent::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(SharedWorkerDevToolsAgent, message)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Attach, OnAttach)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Reattach, OnReattach)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_Detach, OnDetach)
    IPC_MESSAGE_HANDLER(DevToolsAgentMsg_DispatchOnInspectorBackend,
                        OnDispatchOnInspectorBackend)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void SharedWorkerDevToolsAgent::SendDevToolsMessage(
    int call_id,
    const blink::WebString& msg,
    const blink::WebString& state) {
  std::string message = msg.utf8();
  std::string post_state = state.utf8();
  DevToolsMessageChunk chunk;
  chunk.message_size = message.size();
  chunk.is_first = true;

  if (message.length() < kMaxMessageChunkSize) {
    chunk.data.swap(message);
    chunk.call_id = call_id;
    chunk.post_state = post_state;
    chunk.is_last = true;
    Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
        route_id_, chunk));
    return;
  }

  for (size_t pos = 0; pos < message.length(); pos += kMaxMessageChunkSize) {
    chunk.is_last = pos + kMaxMessageChunkSize >= message.length();
    chunk.call_id = chunk.is_last ? call_id : 0;
    chunk.post_state = chunk.is_last ? post_state : std::string();
    chunk.data = message.substr(pos, kMaxMessageChunkSize);
    Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
        route_id_, chunk));
    chunk.is_first = false;
    chunk.message_size = 0;
  }
}

void SharedWorkerDevToolsAgent::OnAttach(const std::string& host_id) {
  webworker_->attachDevTools(WebString::fromUTF8(host_id));
}

void SharedWorkerDevToolsAgent::OnReattach(const std::string& host_id,
                                           const std::string& state) {
  webworker_->reattachDevTools(WebString::fromUTF8(host_id),
                               WebString::fromUTF8(state));
}

void SharedWorkerDevToolsAgent::OnDetach() {
  webworker_->detachDevTools();
}

void SharedWorkerDevToolsAgent::OnDispatchOnInspectorBackend(
    const std::string& message) {
  webworker_->dispatchDevToolsMessage(WebString::fromUTF8(message));
}

bool SharedWorkerDevToolsAgent::Send(IPC::Message* message) {
  return ChildThread::current()->Send(message);
}

}  // namespace content