summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/devtools_agent_impl.h
blob: f062b31de5e5ea2f08dfd065b1a3b29e1b42939f (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
// 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.

#ifndef COMPONENTS_HTML_VIEWER_DEVTOOLS_AGENT_IMPL_H_
#define COMPONENTS_HTML_VIEWER_DEVTOOLS_AGENT_IMPL_H_

#include <string>
#include <vector>

#include "base/macros.h"
#include "components/devtools_service/public/interfaces/devtools_service.mojom.h"
#include "third_party/WebKit/public/web/WebDevToolsAgentClient.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"

namespace blink {
class WebLocalFrame;
}

namespace html_viewer {

class DevToolsAgentImpl : public devtools_service::DevToolsAgent,
                          public blink::WebDevToolsAgentClient {
 public:
  // |frame| must outlive this object.
  // This agent should restore its internal state using |state| if it is not
  // null.
  DevToolsAgentImpl(blink::WebLocalFrame* frame,
                    const std::string& id,
                    const std::string* state);
  ~DevToolsAgentImpl() override;

  void BindToRequest(mojo::InterfaceRequest<DevToolsAgent> request);

 private:
  // devtools_service::DevToolsAgent implementation.
  void SetClient(devtools_service::DevToolsAgentClientPtr client) override;
  void DispatchProtocolMessage(const mojo::String& message) override;

  // blink::WebDevToolsAgentClient implementation.
  void sendProtocolMessage(int call_id,
                           const blink::WebString& response,
                           const blink::WebString& state);

  void OnConnectionError();

  blink::WebLocalFrame* const frame_;
  const std::string id_;

  mojo::Binding<DevToolsAgent> binding_;
  devtools_service::DevToolsAgentClientPtr client_;

  // If we restore the agent's internal state using serialized state data from a
  // previous agent, the agent may generate messages before |client_| is set.
  // In that case, we need to cache messages for the client.
  bool cache_until_client_ready_;

  struct CachedClientMessage {
    int call_id;
    mojo::String response;
    mojo::String state;
  };
  std::vector<CachedClientMessage> cached_client_messages_;

  DISALLOW_COPY_AND_ASSIGN(DevToolsAgentImpl);
};

}  // namespace html_viewer

#endif  // COMPONENTS_HTML_VIEWER_DEVTOOLS_AGENT_IMPL_H_