summaryrefslogtreecommitdiffstats
path: root/webkit/glue/webdevtoolsclient_impl.h
blob: b4ae53e5fee88ba1c2fb3967802653fd47ec61c1 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2009 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 WEBKIT_GLUE_WEBDEVTOOLSCLIENT_IMPL_H_
#define WEBKIT_GLUE_WEBDEVTOOLSCLIENT_IMPL_H_

#include <string>

#include "base/string_util.h"
#include "webkit/glue/cpp_bound_class.h"
#include "webkit/glue/devtools/devtools_rpc.h"
#include "webkit/glue/devtools/dom_agent.h"
#include "webkit/glue/devtools/net_agent.h"
#include "webkit/glue/devtools/tools_agent.h"
#include "webkit/glue/webdevtoolsclient.h"

namespace WebCore {
class String;
}

class DomAgentStub;
class NetAgentStub;
class ToolsAgentStub;
class WebDevToolsClientDelegate;
class WebViewImpl;

class WebDevToolsClientImpl : public WebDevToolsClient,
                              public CppBoundClass,
                              public DevToolsRpc::Delegate,
                              public DomAgentDelegate,
                              public NetAgentDelegate,
                              public ToolsAgentDelegate {
 public:
  WebDevToolsClientImpl(
      WebViewImpl* web_view_impl,
      WebDevToolsClientDelegate* delegate);
  virtual ~WebDevToolsClientImpl();

  // DomAgentDelegate implementation.
  virtual void DocumentElementUpdated(const Value& value);
  virtual void AttributesUpdated(int id, const Value& attributes);
  virtual void ChildNodesUpdated(int id, const Value& value);
  virtual void ChildNodeInserted(
      int parent_id,
      int prev_id,
      const Value& value);
  virtual void ChildNodeRemoved(int parent_id, int id);
  virtual void HasChildrenUpdated(int id, bool new_value);

  // NetAgentDelegate implementation.
  virtual void WillSendRequest(
      int identifier,
      const Value& request);
  virtual void DidReceiveResponse(
      int identifier,
      const Value& response);
  virtual void DidFinishLoading(int identifier, const Value& response);
  virtual void DidFailLoading(int identifier, const Value& response);
  virtual void SetResourceContent(
      int identifier,
      const WebCore::String& content);

  // ToolsAgentDelegate implementation.
  virtual void UpdateFocusedNode(int node_id);

  // DevToolsRpc::Delegate implementation.
  virtual void SendRpcMessage(const std::string& raw_msg);

  // WebDevToolsClient implementation.
  virtual void DispatchMessageFromAgent(const std::string& raw_msg);

 private:
  // MakeJsCall templates.
  void MakeJsCall(const std::string& func) {
    EvaluateJs(StringPrintf("%s()", func.c_str()));
  }
  template<class T1>
  void MakeJsCall(const std::string& func, T1 t1) {
    EvaluateJs(StringPrintf("%s(%s)", func.c_str(),
        ToJSON(t1).c_str()));
  }
  template<class T1, class T2>
  void MakeJsCall(const std::string& func, T1 t1, T2 t2) {
    EvaluateJs(StringPrintf("%s(%s, %s)", func.c_str(),
        ToJSON(t1).c_str(), ToJSON(t2).c_str()));
  }
  template<class T1, class T2, class T3>
  void MakeJsCall(const std::string& func, T1 t1, T2 t2, T3 t3) {
    EvaluateJs(StringPrintf("%s(%s, %s, %s)", func.c_str(),
        ToJSON(t1).c_str(), ToJSON(t2).c_str(), ToJSON(t3).c_str()));
  }
  template<class T1, class T2, class T3, class T4>
  void MakeJsCall(const std::string& func, T1 t1, T2 t2, T3 t3, T4 t4) {
    EvaluateJs(StringPrintf("%s(%s, %s, %s, %s)", func.c_str(),
        ToJSON(t1).c_str(), ToJSON(t2).c_str(), ToJSON(t3).c_str(),
        ToJSON(t4).c_str()));
  }
  template<class T1, class T2, class T3, class T4, class T5>
  void MakeJsCall(const std::string& func, T1 t1, T2 t2, T3 t3, T4 t4,
      T5 t5) {
    EvaluateJs(StringPrintf("%s(%s, %s, %s, %s, %s)",
        func.c_str(), ToJSON(t1).c_str(), ToJSON(t2).c_str(),
        ToJSON(t3).c_str(), ToJSON(t4).c_str(), ToJSON(t5).c_str()));
  }

  void EvaluateJs(const std::string& expr);

  void JsGetResourceSource(const CppArgumentList& args, CppVariant* result);

  void JsGetDocumentElement(const CppArgumentList& args, CppVariant* result);

  void JsGetChildNodes(const CppArgumentList& args, CppVariant* result);

  void JsSetAttribute(const CppArgumentList& args, CppVariant* result);

  void JsRemoveAttribute(const CppArgumentList& args, CppVariant* result);

  void JsSetTextNodeValue(const CppArgumentList& args, CppVariant* result);

  void JsHighlightDOMNode(const CppArgumentList& args, CppVariant* result);

  void JsHideDOMNodeHighlight(const CppArgumentList& args, CppVariant* result);

 private:
  // Serializers
  static std::string ToJSON(const WebCore::String& value);
  static std::string ToJSON(int value);
  static std::string ToJSON(const Value* value);

  WebViewImpl* web_view_impl_;
  WebDevToolsClientDelegate* delegate_;
  scoped_ptr<DomAgentStub> dom_agent_stub_;
  scoped_ptr<NetAgentStub> net_agent_stub_;
  scoped_ptr<ToolsAgentStub> tools_agent_stub_;
  DomAgentDelegateDispatch dom_agent_delegate_dispatch_;
  NetAgentDelegateDispatch net_agent_delegate_dispatch_;
  ToolsAgentDelegateDispatch tools_agent_delegate_dispatch_;
  DISALLOW_COPY_AND_ASSIGN(WebDevToolsClientImpl);
};

#endif  // WEBKIT_GLUE_WEBDEVTOOLSCLIENT_IMPL_H_