1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/renderer/dom_activity_logger.h"
7 #include "content/public/child/v8_value_converter.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "extensions/common/dom_action_types.h"
10 #include "extensions/common/extension_messages.h"
11 #include "extensions/renderer/activity_log_converter_strategy.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
15 using content::V8ValueConverter
;
16 using blink::WebString
;
19 namespace extensions
{
23 // Converts the given |v8_value| and appends it to the given |list|, if the
24 // conversion succeeds.
25 void AppendV8Value(const std::string
& api_name
,
26 const v8::Handle
<v8::Value
>& v8_value
,
27 base::ListValue
* list
) {
29 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
30 ActivityLogConverterStrategy strategy
;
31 converter
->SetFunctionAllowed(true);
32 converter
->SetStrategy(&strategy
);
33 scoped_ptr
<base::Value
> value(converter
->FromV8Value(
34 v8_value
, v8::Isolate::GetCurrent()->GetCurrentContext()));
37 list
->Append(value
.release());
42 DOMActivityLogger::DOMActivityLogger(const std::string
& extension_id
)
43 : extension_id_(extension_id
) {
46 DOMActivityLogger::~DOMActivityLogger() {}
48 void DOMActivityLogger::AttachToWorld(int world_id
,
49 const std::string
& extension_id
) {
50 // If there is no logger registered for world_id, construct a new logger
51 // and register it with world_id.
52 if (!blink::hasDOMActivityLogger(world_id
,
53 WebString::fromUTF8(extension_id
))) {
54 DOMActivityLogger
* logger
= new DOMActivityLogger(extension_id
);
55 blink::setDOMActivityLogger(world_id
,
56 WebString::fromUTF8(extension_id
),
61 void DOMActivityLogger::logGetter(const WebString
& api_name
,
63 const WebString
& title
) {
64 SendDomActionMessage(api_name
.utf8(),
67 DomActionType::GETTER
,
68 scoped_ptr
<base::ListValue
>(new base::ListValue()));
71 void DOMActivityLogger::logSetter(const WebString
& api_name
,
72 const v8::Handle
<v8::Value
>& new_value
,
74 const WebString
& title
) {
75 logSetter(api_name
, new_value
, v8::Handle
<v8::Value
>(), url
, title
);
78 void DOMActivityLogger::logSetter(const WebString
& api_name
,
79 const v8::Handle
<v8::Value
>& new_value
,
80 const v8::Handle
<v8::Value
>& old_value
,
82 const WebString
& title
) {
83 scoped_ptr
<base::ListValue
> args(new base::ListValue
);
84 std::string api_name_utf8
= api_name
.utf8();
85 AppendV8Value(api_name_utf8
, new_value
, args
.get());
86 if (!old_value
.IsEmpty())
87 AppendV8Value(api_name_utf8
, old_value
, args
.get());
89 api_name_utf8
, url
, title
, DomActionType::SETTER
, args
.Pass());
92 void DOMActivityLogger::logMethod(const WebString
& api_name
,
94 const v8::Handle
<v8::Value
>* argv
,
96 const WebString
& title
) {
97 scoped_ptr
<base::ListValue
> args(new base::ListValue
);
98 std::string api_name_utf8
= api_name
.utf8();
99 for (int i
= 0; i
< argc
; ++i
)
100 AppendV8Value(api_name_utf8
, argv
[i
], args
.get());
101 SendDomActionMessage(
102 api_name_utf8
, url
, title
, DomActionType::METHOD
, args
.Pass());
105 void DOMActivityLogger::logEvent(const WebString
& event_name
,
107 const WebString
* argv
,
109 const WebString
& title
) {
110 scoped_ptr
<base::ListValue
> args(new base::ListValue
);
111 std::string event_name_utf8
= event_name
.utf8();
112 for (int i
= 0; i
< argc
; ++i
)
113 args
->Append(new base::StringValue(argv
[i
]));
114 SendDomActionMessage(
115 event_name_utf8
, url
, title
, DomActionType::METHOD
, args
.Pass());
118 void DOMActivityLogger::SendDomActionMessage(const std::string
& api_call
,
120 const base::string16
& url_title
,
121 DomActionType::Type call_type
,
122 scoped_ptr
<base::ListValue
> args
) {
123 ExtensionHostMsg_DOMAction_Params params
;
124 params
.api_call
= api_call
;
126 params
.url_title
= url_title
;
127 params
.call_type
= call_type
;
128 params
.arguments
.Swap(args
.get());
129 content::RenderThread::Get()->Send(
130 new ExtensionHostMsg_AddDOMActionToActivityLog(extension_id_
, params
));
133 } // namespace extensions