summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/dom_activity_logger.cc
blob: 5cebd389801d2a2bfab5fe563f520c23ede29ce0 (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
// 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 "extensions/renderer/dom_activity_logger.h"

#include "content/public/child/v8_value_converter.h"
#include "content/public/renderer/render_thread.h"
#include "extensions/common/dom_action_types.h"
#include "extensions/common/extension_messages.h"
#include "extensions/renderer/activity_log_converter_strategy.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"

using content::V8ValueConverter;
using blink::WebString;
using blink::WebURL;

namespace extensions {

namespace {

// Converts the given |v8_value| and appends it to the given |list|, if the
// conversion succeeds.
void AppendV8Value(const std::string& api_name,
                   const v8::Handle<v8::Value>& v8_value,
                   base::ListValue* list) {
  DCHECK(list);
  scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
  ActivityLogConverterStrategy strategy;
  converter->SetFunctionAllowed(true);
  converter->SetStrategy(&strategy);
  scoped_ptr<base::Value> value(converter->FromV8Value(
      v8_value, v8::Isolate::GetCurrent()->GetCurrentContext()));

  if (value.get())
    list->Append(value.release());
}

}  // namespace

DOMActivityLogger::DOMActivityLogger(const std::string& extension_id)
    : extension_id_(extension_id) {
}

DOMActivityLogger::~DOMActivityLogger() {}

void DOMActivityLogger::AttachToWorld(int world_id,
                                      const std::string& extension_id) {
  // If there is no logger registered for world_id, construct a new logger
  // and register it with world_id.
  if (!blink::hasDOMActivityLogger(world_id,
                                   WebString::fromUTF8(extension_id))) {
    DOMActivityLogger* logger = new DOMActivityLogger(extension_id);
    blink::setDOMActivityLogger(world_id,
                                WebString::fromUTF8(extension_id),
                                logger);
  }
}

void DOMActivityLogger::logGetter(const WebString& api_name,
                                  const WebURL& url,
                                  const WebString& title) {
  SendDomActionMessage(api_name.utf8(),
                       url,
                       title,
                       DomActionType::GETTER,
                       scoped_ptr<base::ListValue>(new base::ListValue()));
}

void DOMActivityLogger::logSetter(const WebString& api_name,
                                  const v8::Handle<v8::Value>& new_value,
                                  const WebURL& url,
                                  const WebString& title) {
  logSetter(api_name, new_value, v8::Handle<v8::Value>(), url, title);
}

void DOMActivityLogger::logSetter(const WebString& api_name,
                                  const v8::Handle<v8::Value>& new_value,
                                  const v8::Handle<v8::Value>& old_value,
                                  const WebURL& url,
                                  const WebString& title) {
  scoped_ptr<base::ListValue> args(new base::ListValue);
  std::string api_name_utf8 = api_name.utf8();
  AppendV8Value(api_name_utf8, new_value, args.get());
  if (!old_value.IsEmpty())
    AppendV8Value(api_name_utf8, old_value, args.get());
  SendDomActionMessage(
      api_name_utf8, url, title, DomActionType::SETTER, args.Pass());
}

void DOMActivityLogger::logMethod(const WebString& api_name,
                                  int argc,
                                  const v8::Handle<v8::Value>* argv,
                                  const WebURL& url,
                                  const WebString& title) {
  scoped_ptr<base::ListValue> args(new base::ListValue);
  std::string api_name_utf8 = api_name.utf8();
  for (int i = 0; i < argc; ++i)
    AppendV8Value(api_name_utf8, argv[i], args.get());
  SendDomActionMessage(
      api_name_utf8, url, title, DomActionType::METHOD, args.Pass());
}

void DOMActivityLogger::logEvent(const WebString& event_name,
                                 int argc,
                                 const WebString* argv,
                                 const WebURL& url,
                                 const WebString& title) {
  scoped_ptr<base::ListValue> args(new base::ListValue);
  std::string event_name_utf8 = event_name.utf8();
  for (int i = 0; i < argc; ++i)
    args->Append(new base::StringValue(argv[i]));
  SendDomActionMessage(
      event_name_utf8, url, title, DomActionType::METHOD, args.Pass());
}

void DOMActivityLogger::SendDomActionMessage(const std::string& api_call,
                                             const GURL& url,
                                             const base::string16& url_title,
                                             DomActionType::Type call_type,
                                             scoped_ptr<base::ListValue> args) {
  ExtensionHostMsg_DOMAction_Params params;
  params.api_call = api_call;
  params.url = url;
  params.url_title = url_title;
  params.call_type = call_type;
  params.arguments.Swap(args.get());
  content::RenderThread::Get()->Send(
      new ExtensionHostMsg_AddDOMActionToActivityLog(extension_id_, params));
}

}  // namespace extensions