summaryrefslogtreecommitdiffstats
path: root/components/devtools_service/devtools_agent_host.cc
blob: d344a5991d99e068c3723e9819f166da18146569 (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
// Copyright 2015 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 "components/devtools_service/devtools_agent_host.h"

#include "base/guid.h"
#include "base/logging.h"

namespace devtools_service {

DevToolsAgentHost::DevToolsAgentHost(DevToolsAgentPtr agent)
    : id_(base::GenerateGUID()),
      agent_(agent.Pass()),
      binding_(this),
      delegate_(nullptr) {
}

DevToolsAgentHost::~DevToolsAgentHost() {
  if (delegate_)
    delegate_->OnAgentHostClosed(this);
}

void DevToolsAgentHost::SetDelegate(Delegate* delegate) {
  delegate_ = delegate;
  if (delegate_) {
    if (binding_.is_bound())
      return;

    DevToolsAgentClientPtr client;
    binding_.Bind(&client);
    agent_->SetClient(client.Pass(), id_);
  } else {
    if (!binding_.is_bound())
      return;

    binding_.Close();
  }
}

void DevToolsAgentHost::SendProtocolMessageToAgent(const std::string& message) {
  agent_->DispatchProtocolMessage(message);
}

void DevToolsAgentHost::DispatchProtocolMessage(const mojo::String& message) {
  delegate_->DispatchProtocolMessage(this, message);
}

}  // namespace devtools_service