1 // Copyright (c) 2013 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.
4
5 #include "chrome/renderer/extensions/dom_activity_logger.h"
6
7 #include "base/logging.h"
8 #include "chrome/common/extensions/dom_action_types.h"
9 #include "chrome/common/extensions/extension_messages.h"
10 #include "chrome/renderer/chrome_render_process_observer.h"
11 #include "chrome/renderer/extensions/activity_log_converter_strategy.h"
12 #include "content/public/renderer/render_thread.h"
13 #include "content/public/renderer/v8_value_converter.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/web/WebDOMActivityLogger.h"
17 #include "v8/include/v8.h"
18
19 using content::V8ValueConverter;
20 using blink::WebString;
21 using blink::WebURL;
22
23 namespace extensions {
24
DOMActivityLogger(const std::string & extension_id)25 DOMActivityLogger::DOMActivityLogger(const std::string& extension_id)
26 : extension_id_(extension_id) {}
27
log(const WebString & api_name,int argc,const v8::Handle<v8::Value> argv[],const WebString & call_type,const WebURL & url,const WebString & title)28 void DOMActivityLogger::log(
29 const WebString& api_name,
30 int argc,
31 const v8::Handle<v8::Value> argv[],
32 const WebString& call_type,
33 const WebURL& url,
34 const WebString& title) {
35 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
36 ActivityLogConverterStrategy strategy;
37 converter->SetFunctionAllowed(true);
38 converter->SetStrategy(&strategy);
39 scoped_ptr<ListValue> argv_list_value(new ListValue());
40 for (int i =0; i < argc; i++) {
41 argv_list_value->Set(
42 i,
43 converter->FromV8Value(argv[i],
44 v8::Isolate::GetCurrent()->GetCurrentContext()));
45 }
46 ExtensionHostMsg_DOMAction_Params params;
47 params.url = url;
48 params.url_title = title;
49 params.api_call = api_name.utf8();
50 params.arguments.Swap(argv_list_value.get());
51 const std::string type = call_type.utf8();
52 if (type == "Getter")
53 params.call_type = DomActionType::GETTER;
54 else if (type == "Setter")
55 params.call_type = DomActionType::SETTER;
56 else
57 params.call_type = DomActionType::METHOD;
58
59 content::RenderThread::Get()->Send(
60 new ExtensionHostMsg_AddDOMActionToActivityLog(extension_id_, params));
61 }
62
AttachToWorld(int world_id,const std::string & extension_id)63 void DOMActivityLogger::AttachToWorld(int world_id,
64 const std::string& extension_id) {
65 #if defined(ENABLE_EXTENSIONS)
66 // If there is no logger registered for world_id, construct a new logger
67 // and register it with world_id.
68 if (!blink::hasDOMActivityLogger(world_id)) {
69 DOMActivityLogger* logger = new DOMActivityLogger(extension_id);
70 blink::setDOMActivityLogger(world_id, logger);
71 }
72 #endif
73 }
74
75 } // namespace extensions
76
77