Resubmit: Extend DOMActivityLogger interface for upcoming change

Resubmit of https://codereview.chromium.org/247953008/

---------------

In order to reduce pain (hopefully to zero) for sheriffs and fellow developers
who prefer their trees green rather than red, let's make landing the change
at https://codereview.chromium.org/213783002/ as safe as possible.

Step 1: This CL. Extend DOMActivityLogger with new methods for logGetter,
logSetter, and logMethod, which have the same functionality as the current
log() method.
Step 2: https://codereview.chromium.org/213783002/ - The implementation change
in blink to switch from using log() to using logX.
Step 3: Cleanup -- remove old log method from chrome.

BUG=356890

Review URL: https://codereview.chromium.org/265633002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267582 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/extensions/renderer/dom_activity_logger.cc b/extensions/renderer/dom_activity_logger.cc
index 6e74e9e..87b4074 100644
--- a/extensions/renderer/dom_activity_logger.cc
+++ b/extensions/renderer/dom_activity_logger.cc
@@ -12,8 +12,6 @@
 #include "extensions/renderer/activity_log_converter_strategy.h"
 #include "third_party/WebKit/public/platform/WebString.h"
 #include "third_party/WebKit/public/platform/WebURL.h"
-#include "third_party/WebKit/public/web/WebDOMActivityLogger.h"
-#include "v8/include/v8.h"
 
 using content::V8ValueConverter;
 using blink::WebString;
@@ -21,6 +19,29 @@
 
 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;
+  strategy.set_enable_detailed_parsing(
+      ad_injection_constants::ApiCanInjectAds(api_name));
+  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) {
 }
@@ -34,35 +55,18 @@
     const WebString& call_type,
     const WebURL& url,
     const WebString& title) {
-  scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
-  ActivityLogConverterStrategy strategy;
-  strategy.set_enable_detailed_parsing(
-      ad_injection_constants::ApiCanInjectAds(api_name.utf8().c_str()));
-  converter->SetFunctionAllowed(true);
-  converter->SetStrategy(&strategy);
-  scoped_ptr<base::ListValue> argv_list_value(new base::ListValue());
-  for (int i = 0; i < argc; i++) {
-    argv_list_value->Set(
-        i,
-        converter->FromV8Value(argv[i],
-                               v8::Isolate::GetCurrent()->GetCurrentContext()));
-  }
+  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());
 
-  ExtensionHostMsg_DOMAction_Params params;
-  params.url = url;
-  params.url_title = title;
-  params.api_call = api_name.utf8();
-  params.arguments.Swap(argv_list_value.get());
-  const std::string type = call_type.utf8();
-  if (type == "Getter")
-    params.call_type = DomActionType::GETTER;
-  else if (type == "Setter")
-    params.call_type = DomActionType::SETTER;
-  else
-    params.call_type = DomActionType::METHOD;
-
-  content::RenderThread::Get()->Send(
-      new ExtensionHostMsg_AddDOMActionToActivityLog(extension_id_, params));
+  DomActionType::Type type = DomActionType::METHOD;
+  if (call_type == "Getter")
+    type = DomActionType::GETTER;
+  else if (call_type == "Setter")
+    type = DomActionType::SETTER;
+  // else DomActionType::METHOD is correct.
+  SendDomActionMessage(api_name_utf8, url, title, type, args.Pass());
 }
 
 void DOMActivityLogger::AttachToWorld(int world_id,
@@ -77,4 +81,63 @@
 #endif
 }
 
+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::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