blob: 13d960b8a5458f47b95171def8b068f24d424ce5 [file] [log] [blame]
[email protected]fb5c4742014-05-01 09:05:271// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]b6e7b542013-03-08 01:57:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fb5c4742014-05-01 09:05:275#include "extensions/renderer/dom_activity_logger.h"
[email protected]b6e7b542013-03-08 01:57:526
dchenge59eca1602015-12-18 17:48:007#include <utility>
8
mek87e0ab52015-02-13 01:18:269#include "content/public/child/v8_value_converter.h"
[email protected]b6e7b542013-03-08 01:57:5210#include "content/public/renderer/render_thread.h"
[email protected]fb5c4742014-05-01 09:05:2711#include "extensions/common/dom_action_types.h"
[email protected]fb820c02014-03-13 15:07:0812#include "extensions/common/extension_messages.h"
[email protected]fb5c4742014-05-01 09:05:2713#include "extensions/renderer/activity_log_converter_strategy.h"
[email protected]e48cf4f2013-05-30 12:40:1214#include "third_party/WebKit/public/platform/WebString.h"
[email protected]9f07b0422013-09-24 21:58:2315#include "third_party/WebKit/public/platform/WebURL.h"
[email protected]b6e7b542013-03-08 01:57:5216
17using content::V8ValueConverter;
[email protected]a1221aea2013-11-07 01:31:3018using blink::WebString;
19using blink::WebURL;
[email protected]b6e7b542013-03-08 01:57:5220
21namespace extensions {
22
[email protected]370c6bed2014-05-01 18:06:0823namespace {
24
25// Converts the given |v8_value| and appends it to the given |list|, if the
26// conversion succeeds.
27void AppendV8Value(const std::string& api_name,
tfarinaf85316f2015-04-29 17:03:4028 const v8::Local<v8::Value>& v8_value,
[email protected]370c6bed2014-05-01 18:06:0829 base::ListValue* list) {
30 DCHECK(list);
mostynbea64f7c2016-04-03 16:03:4431 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]370c6bed2014-05-01 18:06:0832 ActivityLogConverterStrategy strategy;
[email protected]370c6bed2014-05-01 18:06:0833 converter->SetFunctionAllowed(true);
34 converter->SetStrategy(&strategy);
mostynbea64f7c2016-04-03 16:03:4435 std::unique_ptr<base::Value> value(converter->FromV8Value(
[email protected]370c6bed2014-05-01 18:06:0836 v8_value, v8::Isolate::GetCurrent()->GetCurrentContext()));
37
38 if (value.get())
dcheng5d090492016-06-09 17:53:3439 list->Append(std::move(value));
[email protected]370c6bed2014-05-01 18:06:0840}
41
42} // namespace
43
[email protected]9f07b0422013-09-24 21:58:2344DOMActivityLogger::DOMActivityLogger(const std::string& extension_id)
[email protected]fd46e7a2014-04-16 17:59:4745 : extension_id_(extension_id) {
46}
47
48DOMActivityLogger::~DOMActivityLogger() {}
[email protected]b6e7b542013-03-08 01:57:5249
[email protected]b6e7b542013-03-08 01:57:5250void DOMActivityLogger::AttachToWorld(int world_id,
[email protected]9f07b0422013-09-24 21:58:2351 const std::string& extension_id) {
[email protected]b6e7b542013-03-08 01:57:5252 // If there is no logger registered for world_id, construct a new logger
53 // and register it with world_id.
[email protected]e693a082014-06-04 00:37:2554 if (!blink::hasDOMActivityLogger(world_id,
55 WebString::fromUTF8(extension_id))) {
[email protected]9f07b0422013-09-24 21:58:2356 DOMActivityLogger* logger = new DOMActivityLogger(extension_id);
[email protected]e693a082014-06-04 00:37:2557 blink::setDOMActivityLogger(world_id,
58 WebString::fromUTF8(extension_id),
59 logger);
[email protected]b6e7b542013-03-08 01:57:5260 }
[email protected]b6e7b542013-03-08 01:57:5261}
62
[email protected]370c6bed2014-05-01 18:06:0863void DOMActivityLogger::logGetter(const WebString& api_name,
64 const WebURL& url,
65 const WebString& title) {
mostynbea64f7c2016-04-03 16:03:4466 SendDomActionMessage(api_name.utf8(), url, title, DomActionType::GETTER,
67 std::unique_ptr<base::ListValue>(new base::ListValue()));
[email protected]370c6bed2014-05-01 18:06:0868}
69
70void DOMActivityLogger::logSetter(const WebString& api_name,
tfarinaf85316f2015-04-29 17:03:4071 const v8::Local<v8::Value>& new_value,
[email protected]370c6bed2014-05-01 18:06:0872 const WebURL& url,
73 const WebString& title) {
tfarinaf85316f2015-04-29 17:03:4074 logSetter(api_name, new_value, v8::Local<v8::Value>(), url, title);
[email protected]370c6bed2014-05-01 18:06:0875}
76
77void DOMActivityLogger::logSetter(const WebString& api_name,
tfarinaf85316f2015-04-29 17:03:4078 const v8::Local<v8::Value>& new_value,
79 const v8::Local<v8::Value>& old_value,
[email protected]370c6bed2014-05-01 18:06:0880 const WebURL& url,
81 const WebString& title) {
mostynbea64f7c2016-04-03 16:03:4482 std::unique_ptr<base::ListValue> args(new base::ListValue);
[email protected]370c6bed2014-05-01 18:06:0883 std::string api_name_utf8 = api_name.utf8();
84 AppendV8Value(api_name_utf8, new_value, args.get());
85 if (!old_value.IsEmpty())
86 AppendV8Value(api_name_utf8, old_value, args.get());
dchenge59eca1602015-12-18 17:48:0087 SendDomActionMessage(api_name_utf8, url, title, DomActionType::SETTER,
88 std::move(args));
[email protected]370c6bed2014-05-01 18:06:0889}
90
91void DOMActivityLogger::logMethod(const WebString& api_name,
92 int argc,
tfarinaf85316f2015-04-29 17:03:4093 const v8::Local<v8::Value>* argv,
[email protected]370c6bed2014-05-01 18:06:0894 const WebURL& url,
95 const WebString& title) {
mostynbea64f7c2016-04-03 16:03:4496 std::unique_ptr<base::ListValue> args(new base::ListValue);
[email protected]370c6bed2014-05-01 18:06:0897 std::string api_name_utf8 = api_name.utf8();
98 for (int i = 0; i < argc; ++i)
99 AppendV8Value(api_name_utf8, argv[i], args.get());
dchenge59eca1602015-12-18 17:48:00100 SendDomActionMessage(api_name_utf8, url, title, DomActionType::METHOD,
101 std::move(args));
[email protected]370c6bed2014-05-01 18:06:08102}
103
[email protected]4bf26f6b2014-06-23 10:00:50104void DOMActivityLogger::logEvent(const WebString& event_name,
105 int argc,
106 const WebString* argv,
107 const WebURL& url,
108 const WebString& title) {
mostynbea64f7c2016-04-03 16:03:44109 std::unique_ptr<base::ListValue> args(new base::ListValue);
[email protected]4bf26f6b2014-06-23 10:00:50110 std::string event_name_utf8 = event_name.utf8();
111 for (int i = 0; i < argc; ++i)
dcheng04f99a52016-06-03 17:38:20112 args->AppendString(argv[i]);
dchenge59eca1602015-12-18 17:48:00113 SendDomActionMessage(event_name_utf8, url, title, DomActionType::METHOD,
114 std::move(args));
[email protected]4bf26f6b2014-06-23 10:00:50115}
116
mostynbea64f7c2016-04-03 16:03:44117void DOMActivityLogger::SendDomActionMessage(
118 const std::string& api_call,
119 const GURL& url,
120 const base::string16& url_title,
121 DomActionType::Type call_type,
122 std::unique_ptr<base::ListValue> args) {
[email protected]370c6bed2014-05-01 18:06:08123 ExtensionHostMsg_DOMAction_Params params;
124 params.api_call = api_call;
125 params.url = url;
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));
131}
132
[email protected]b6e7b542013-03-08 01:57:52133} // namespace extensions