summaryrefslogtreecommitdiffstats
path: root/chrome/browser/debugger/debugger_node.h
blob: 351d9c76fcc570ba86d7bd620fded350c22011cc (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Proxy objects for exposing Chrome internals to V8.  Adds some convenience
// methods to simplify properties, indexes and functions, as well as helping
// with object lifetime bi-directionally.

// TODO: this code is temporary and will be converted to use IDL
// Also note that it's missing a lot of functionality and isn't correct.
// For example, objects aren't being cached properly (browser.foo = 1 wouldn't
// be remembered), and setters aren't implemented to begin with.

#ifndef CHROME_BROWSER_DEBUGGER_DEBUGGER_NODE_H__
#define CHROME_BROWSER_DEBUGGER_DEBUGGER_NODE_H__

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "chrome/common/notification_service.h"
#include "v8/public/v8.h"

class Browser;
class TabContents;
class DebuggerShell;
class WebContents;

class DebuggerNode : public NotificationObserver {
 public:
  DebuggerNode();
  virtual ~DebuggerNode();

  // does your object handle array references? (e.g. myobj[0])
  virtual bool IsCollection() = 0;
  // does your object work as a function (e.g. myobj())
  virtual bool IsFunction() = 0;
  // does your object contain other named properties? (e.g. myobj.foo)
  virtual bool IsObject() = 0;

  // Is the underlying C++ object valid or not?  It's possible for the JS object
  // to be alive after the underlying C++ object has gone away.  In that case,
  // the DebuggerNode stays around but is marked as invalid.
  bool IsValid() { return valid_; }
  virtual void Invalidate() { valid_ = false; }

  // Callback for DebuggerNode subclasses which use the NotificationService to
  // track object validity.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);
  void StopObserving();
  virtual void StopObserving(NotificationService *service);

  // Index getter callback from V8 for objects where IsCollection is true
  virtual v8::Handle<v8::Value> IndexGetter(uint32_t index,
                                            const v8::AccessorInfo& info);
  // Index getter callback from V8 for objects where IsObject is true
  virtual v8::Handle<v8::Value> PropGetter(v8::Handle<v8::String> prop,
                                           const v8::AccessorInfo& info);
  // Functor callback from V8 for objects where IsFunction is true
  virtual v8::Handle<v8::Value> Function(const v8::Arguments& args);

  // Create a new instance of this JS object.
  virtual v8::Handle<v8::Value> NewInstance();

  // Generic DebuggerNode named property getter
  static v8::Handle<v8::Value> NodeGetter(v8::Local<v8::String> prop,
                                          const v8::AccessorInfo& info);
  // Generic DebuggerNode index getter
  static v8::Handle<v8::Value> NodeIndex(uint32_t index,
                                         const v8::AccessorInfo& info);
  // Generic DebuggerNode functor
  static v8::Handle<v8::Value> NodeFunc(const v8::Arguments& args);

 protected:
  void *data_;
  bool valid_;
  bool observing_;

 private:
};

// A wrapper around the proxy to handle two issues:
// - call virtual methods to stop observing at destruction time
// - call virtual methods during callbacks from V8 after a static_cast
//   from void*
// The point here is that we'd like to be able to stick DebuggerNode* objects
// into V8.  To do that, we need to cast them to void*, which means we need
// this additional layer of wrapper to protect them from the harmful effects
// of static_cast.  Rather than passing in a DebuggerNode*, we instead pass in
// a DebuggerNodeWrapper*.  Since this is what's being referenced by V8, we
// also handle lifetime issues (RefCounted) in the wrapper.
class DebuggerNodeWrapper : public base::RefCounted<DebuggerNodeWrapper> {
 public:
  DebuggerNodeWrapper(DebuggerNode* node) : node_(node) {}
  virtual ~DebuggerNodeWrapper() {
    node_->StopObserving();
    delete node_;
  }
  DebuggerNode* node() { return node_; }
 private:
  DebuggerNode* node_;
};

// top level chrome object implements:
// * pid() - process id of chrome browser process
// * browser[] - returns collection of browser objects
class ChromeNode : public DebuggerNode {
 public:
  ChromeNode(DebuggerShell* debugger);
  virtual ~ChromeNode();

  bool IsCollection() { return false; }
  bool IsFunction() { return false; }
  bool IsObject() { return true; }

  virtual v8::Handle<v8::Value> PropGetter(v8::Handle<v8::String> prop,
                                           const v8::AccessorInfo& info);

  virtual void StopObserving(NotificationService *service);

 private:

  DebuggerShell* debugger_;
};

// browser collection, simply returns the n'th browser from BrowserList
class BrowserListNode : public DebuggerNode {
 public:
  bool IsCollection() { return true; }
  bool IsFunction() { return false; }
  bool IsObject() { return false; }

  virtual v8::Handle<v8::Value> IndexGetter(uint32_t index,
                                            const v8::AccessorInfo& info);
  virtual void StopObserving(NotificationService *service);

  static BrowserListNode* BrowserList();

 private:
  BrowserListNode();
  virtual ~BrowserListNode();
};

// Wrapper around Browser object.  implements:
// * title - title of the current tab
// * tab[] - collection of tabs
class BrowserNode : public DebuggerNode {
 public:
  bool IsCollection() { return false; }
  bool IsFunction() { return false; }
  bool IsObject() { return true; }

  static BrowserNode* BrowserAtIndex(int index);

  virtual void StopObserving(NotificationService *service);

  virtual v8::Handle<v8::Value> PropGetter(v8::Handle<v8::String> prop,
                                           const v8::AccessorInfo& info);

 private:
  BrowserNode(Browser* b);
  Browser* GetBrowser();
  virtual ~BrowserNode();
};

// tab collection, simply returns the n'th TabContents from Browser
class TabListNode : public DebuggerNode {
 public:
  bool IsCollection() { return true; }
  bool IsFunction() { return false; }
  bool IsObject() { return false; }

  virtual void StopObserving(NotificationService *service);

  virtual v8::Handle<v8::Value> IndexGetter(uint32_t index,
                                            const v8::AccessorInfo& info);
  static TabListNode* TabList(Browser* b);

 private:
  TabListNode(Browser* b);
  virtual ~TabListNode();
  Browser* GetBrowser();
};

// Wrapper around TabContents.  Implements:
// * title - tab title
// * attach - starts debugging in this tab (currently this just means log msgs)
// * detach - stops debugging in this tab
// * eval(xpath, expr), eval(expr) - evaluates JS expr in xpath iframe context
class TabNode : public DebuggerNode {
 public:
  bool IsCollection() { return false; }
  bool IsFunction() { return false; }
  bool IsObject() { return true; }

  TabNode(TabContents* c);
  virtual void StopObserving(NotificationService* service);
  virtual v8::Handle<v8::Value> PropGetter(v8::Handle<v8::String> prop,
                                           const v8::AccessorInfo& info);
 private:

  static v8::Handle<v8::Value> SendToDebugger(const v8::Arguments& args,
                                              WebContents* data);
  static v8::Handle<v8::Value> Attach(const v8::Arguments& args,
                                      WebContents* data);
  static v8::Handle<v8::Value> Detach(const v8::Arguments& args,
                                      WebContents* data);
  static v8::Handle<v8::Value> Break(const v8::Arguments& args,
                                     WebContents* data);

  virtual ~TabNode();
  TabContents* GetTab();
};

template<class T>
class FunctionNode : public DebuggerNode {
 public:
  bool IsCollection() { return false; }
  bool IsFunction() { return true; }
  bool IsObject() { return false; }

  typedef v8::Handle<v8::Value> (*Callback)(const v8::Arguments& args, T* data);

  FunctionNode(Callback f, T* data) :
      function_(f), data_(data) {};

private:
  // Functor callback from V8 for objects where IsFunction is true
  virtual v8::Handle<v8::Value> Function(const v8::Arguments& args);

  Callback function_;
  T* data_;
};


#endif // CHROME_BROWSER_DEBUGGER_DEBUGGER_NODE_H__