[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 5 | #include "extensions/renderer/dom_activity_logger.h" |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 6 | |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 9 | #include "content/public/child/v8_value_converter.h" |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 10 | #include "content/public/renderer/render_thread.h" |
[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 11 | #include "extensions/common/dom_action_types.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 12 | #include "extensions/common/extension_messages.h" |
[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 13 | #include "extensions/renderer/activity_log_converter_strategy.h" |
[email protected] | e48cf4f | 2013-05-30 12:40:12 | [diff] [blame] | 14 | #include "third_party/WebKit/public/platform/WebString.h" |
[email protected] | 9f07b042 | 2013-09-24 21:58:23 | [diff] [blame] | 15 | #include "third_party/WebKit/public/platform/WebURL.h" |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 16 | |
| 17 | using content::V8ValueConverter; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 18 | using blink::WebString; |
| 19 | using blink::WebURL; |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 20 | |
| 21 | namespace extensions { |
| 22 | |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Converts the given |v8_value| and appends it to the given |list|, if the |
| 26 | // conversion succeeds. |
| 27 | void AppendV8Value(const std::string& api_name, |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 28 | const v8::Local<v8::Value>& v8_value, |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 29 | base::ListValue* list) { |
| 30 | DCHECK(list); |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 31 | std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 32 | ActivityLogConverterStrategy strategy; |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 33 | converter->SetFunctionAllowed(true); |
| 34 | converter->SetStrategy(&strategy); |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 35 | std::unique_ptr<base::Value> value(converter->FromV8Value( |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 36 | v8_value, v8::Isolate::GetCurrent()->GetCurrentContext())); |
| 37 | |
| 38 | if (value.get()) |
dcheng | 5d09049 | 2016-06-09 17:53:34 | [diff] [blame] | 39 | list->Append(std::move(value)); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
[email protected] | 9f07b042 | 2013-09-24 21:58:23 | [diff] [blame] | 44 | DOMActivityLogger::DOMActivityLogger(const std::string& extension_id) |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 45 | : extension_id_(extension_id) { |
| 46 | } |
| 47 | |
| 48 | DOMActivityLogger::~DOMActivityLogger() {} |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 49 | |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 50 | void DOMActivityLogger::AttachToWorld(int world_id, |
[email protected] | 9f07b042 | 2013-09-24 21:58:23 | [diff] [blame] | 51 | const std::string& extension_id) { |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 52 | // If there is no logger registered for world_id, construct a new logger |
| 53 | // and register it with world_id. |
[email protected] | e693a08 | 2014-06-04 00:37:25 | [diff] [blame] | 54 | if (!blink::hasDOMActivityLogger(world_id, |
| 55 | WebString::fromUTF8(extension_id))) { |
[email protected] | 9f07b042 | 2013-09-24 21:58:23 | [diff] [blame] | 56 | DOMActivityLogger* logger = new DOMActivityLogger(extension_id); |
[email protected] | e693a08 | 2014-06-04 00:37:25 | [diff] [blame] | 57 | blink::setDOMActivityLogger(world_id, |
| 58 | WebString::fromUTF8(extension_id), |
| 59 | logger); |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 60 | } |
[email protected] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 63 | void DOMActivityLogger::logGetter(const WebString& api_name, |
| 64 | const WebURL& url, |
| 65 | const WebString& title) { |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 66 | SendDomActionMessage(api_name.utf8(), url, title, DomActionType::GETTER, |
| 67 | std::unique_ptr<base::ListValue>(new base::ListValue())); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void DOMActivityLogger::logSetter(const WebString& api_name, |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 71 | const v8::Local<v8::Value>& new_value, |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 72 | const WebURL& url, |
| 73 | const WebString& title) { |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 74 | logSetter(api_name, new_value, v8::Local<v8::Value>(), url, title); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void DOMActivityLogger::logSetter(const WebString& api_name, |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 78 | const v8::Local<v8::Value>& new_value, |
| 79 | const v8::Local<v8::Value>& old_value, |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 80 | const WebURL& url, |
| 81 | const WebString& title) { |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 82 | std::unique_ptr<base::ListValue> args(new base::ListValue); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 83 | 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()); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 87 | SendDomActionMessage(api_name_utf8, url, title, DomActionType::SETTER, |
| 88 | std::move(args)); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void DOMActivityLogger::logMethod(const WebString& api_name, |
| 92 | int argc, |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 93 | const v8::Local<v8::Value>* argv, |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 94 | const WebURL& url, |
| 95 | const WebString& title) { |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 96 | std::unique_ptr<base::ListValue> args(new base::ListValue); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 97 | 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()); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 100 | SendDomActionMessage(api_name_utf8, url, title, DomActionType::METHOD, |
| 101 | std::move(args)); |
[email protected] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 4bf26f6b | 2014-06-23 10:00:50 | [diff] [blame] | 104 | void DOMActivityLogger::logEvent(const WebString& event_name, |
| 105 | int argc, |
| 106 | const WebString* argv, |
| 107 | const WebURL& url, |
| 108 | const WebString& title) { |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 109 | std::unique_ptr<base::ListValue> args(new base::ListValue); |
[email protected] | 4bf26f6b | 2014-06-23 10:00:50 | [diff] [blame] | 110 | std::string event_name_utf8 = event_name.utf8(); |
| 111 | for (int i = 0; i < argc; ++i) |
dcheng | 04f99a5 | 2016-06-03 17:38:20 | [diff] [blame] | 112 | args->AppendString(argv[i]); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 113 | SendDomActionMessage(event_name_utf8, url, title, DomActionType::METHOD, |
| 114 | std::move(args)); |
[email protected] | 4bf26f6b | 2014-06-23 10:00:50 | [diff] [blame] | 115 | } |
| 116 | |
mostynb | ea64f7c | 2016-04-03 16:03:44 | [diff] [blame] | 117 | void 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] | 370c6bed | 2014-05-01 18:06:08 | [diff] [blame] | 123 | 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] | b6e7b54 | 2013-03-08 01:57:52 | [diff] [blame] | 133 | } // namespace extensions |