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.
5 #include "chrome/renderer/extensions/dom_activity_logger.h"
7 #include "base/logging.h"
8 #include "chrome/common/extensions/dom_action_types.h"
9 #include "chrome/renderer/chrome_render_process_observer.h"
10 #include "chrome/renderer/extensions/activity_log_converter_strategy.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "content/public/renderer/v8_value_converter.h"
13 #include "extensions/common/extension_messages.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"
19 using content::V8ValueConverter
;
20 using blink::WebString
;
23 namespace extensions
{
25 DOMActivityLogger::DOMActivityLogger(const std::string
& extension_id
)
26 : extension_id_(extension_id
) {}
28 void DOMActivityLogger::log(
29 const WebString
& api_name
,
31 const v8::Handle
<v8::Value
> argv
[],
32 const WebString
& call_type
,
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
<base::ListValue
> argv_list_value(new base::ListValue());
40 for (int i
=0; i
< argc
; i
++) {
43 converter
->FromV8Value(argv
[i
],
44 v8::Isolate::GetCurrent()->GetCurrentContext()));
46 ExtensionHostMsg_DOMAction_Params params
;
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();
53 params
.call_type
= DomActionType::GETTER
;
54 else if (type
== "Setter")
55 params
.call_type
= DomActionType::SETTER
;
57 params
.call_type
= DomActionType::METHOD
;
59 content::RenderThread::Get()->Send(
60 new ExtensionHostMsg_AddDOMActionToActivityLog(extension_id_
, params
));
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
);
75 } // namespace extensions