forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_log_converter_strategy.cc
189 lines (161 loc) · 6.95 KB
/
activity_log_converter_strategy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/activity_log_converter_strategy.h"
#include "base/logging.h"
#include "base/values.h"
#include "extensions/common/ad_injection_constants.h"
#include "v8/include/v8.h"
namespace extensions {
namespace {
typedef ActivityLogConverterStrategy::FromV8ValueCallback FromV8ValueCallback;
namespace constants = ad_injection_constants;
namespace keys = constants::keys;
const char kFirstChildProperty[] = "firstElementChild";
const char kNextElementSiblingProperty[] = "nextElementSibling";
scoped_ptr<base::DictionaryValue> ParseV8Object(
v8::Isolate* isolate,
v8::Object* object,
const FromV8ValueCallback& callback);
// Get a property from a V8 object without entering javascript. We use this
// in order to examine the objects, while ensuring that we don't cause any
// change in the running program.
v8::Local<v8::Value> SafeGetProperty(v8::Isolate* isolate,
v8::Object* object,
const char* key) {
v8::TryCatch try_catch;
v8::Isolate::DisallowJavascriptExecutionScope scope(
isolate, v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
v8::Local<v8::String> key_string = v8::String::NewFromUtf8(isolate, key);
v8::Local<v8::Value> value = object->Get(key_string);
if (try_catch.HasCaught() || value.IsEmpty() || value->IsUndefined() ||
value->IsNull()) {
return v8::Local<v8::Value>();
}
return value;
}
// Append a property to the given |dict| from the given |object| if the
// property exists on |object| and can be accessed safely (i.e., without
// triggering any javascript execution).
void MaybeAppendV8Property(v8::Isolate* isolate,
v8::Object* object,
const char* property_name,
base::DictionaryValue* dict,
const FromV8ValueCallback& callback) {
v8::Handle<v8::Value> value = SafeGetProperty(isolate, object, property_name);
if (!value.IsEmpty()) {
scoped_ptr<base::Value> parsed_value(callback.Run(value, isolate));
if (parsed_value.get())
dict->Set(property_name, parsed_value.release());
}
}
// Parse the children of a V8 |object| and return them as a list. This will
// return an empty scoped_ptr if no children are present, or if the children
// cannot be read safely (without triggering javascript).
scoped_ptr<base::ListValue> MaybeParseV8Children(
v8::Isolate* isolate,
v8::Object* object,
const FromV8ValueCallback& callback) {
scoped_ptr<base::ListValue> parsed_children(new base::ListValue());
v8::Local<v8::Value> child_value =
SafeGetProperty(isolate, object, kFirstChildProperty);
size_t checked_children = 0u;
while (!child_value.IsEmpty() &&
child_value->IsObject() &&
checked_children < constants::kMaximumChildrenToCheck) {
++checked_children;
v8::Handle<v8::Object> child_object = child_value->ToObject();
scoped_ptr<base::Value> parsed_child(
callback.Run(child_object, isolate));
if (parsed_child.get())
parsed_children->Append(parsed_child.release());
child_value =
SafeGetProperty(isolate, *child_object, kNextElementSiblingProperty);
}
return parsed_children->GetSize() > 0 ? parsed_children.Pass()
: scoped_ptr<base::ListValue>();
}
// Parse a V8 |object| into a DictionaryValue. This will examine the object
// for a few important properties, including:
// - href
// - src
// - children
// These properties are necessary to analyze whether or not the object contains
// ads, which may have been injected.
scoped_ptr<base::DictionaryValue> ParseV8Object(
v8::Isolate* isolate,
v8::Object* object,
const FromV8ValueCallback& callback) {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
dict->SetString(keys::kType,
*v8::String::Utf8Value(object->GetConstructorName()));
MaybeAppendV8Property(isolate, object, keys::kHref, dict.get(), callback);
MaybeAppendV8Property(isolate, object, keys::kSrc, dict.get(), callback);
scoped_ptr<base::ListValue> maybe_children =
MaybeParseV8Children(isolate, object, callback);
if (maybe_children.get())
dict->Set(keys::kChildren, maybe_children.release());
return dict.Pass();
}
// Summarize a V8 value. This performs a shallow conversion in all cases, and
// returns only a string with a description of the value (e.g.,
// "[HTMLElement]").
scoped_ptr<base::Value> SummarizeV8Value(v8::Isolate* isolate,
v8::Handle<v8::Object> object) {
v8::TryCatch try_catch;
v8::Isolate::DisallowJavascriptExecutionScope scope(
isolate, v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, "[");
if (object->IsFunction()) {
name =
v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "Function"));
v8::Local<v8::Value> fname =
v8::Handle<v8::Function>::Cast(object)->GetName();
if (fname->IsString() && v8::Handle<v8::String>::Cast(fname)->Length()) {
name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, " "));
name = v8::String::Concat(name, v8::Handle<v8::String>::Cast(fname));
name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "()"));
}
} else {
name = v8::String::Concat(name, object->GetConstructorName());
}
name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "]"));
if (try_catch.HasCaught()) {
return scoped_ptr<base::Value>(
new base::StringValue("[JS Execution Exception]"));
}
return scoped_ptr<base::Value>(
new base::StringValue(std::string(*v8::String::Utf8Value(name))));
}
} // namespace
ActivityLogConverterStrategy::ActivityLogConverterStrategy()
: enable_detailed_parsing_(false) {}
ActivityLogConverterStrategy::~ActivityLogConverterStrategy() {}
bool ActivityLogConverterStrategy::FromV8Object(
v8::Handle<v8::Object> value,
base::Value** out,
v8::Isolate* isolate,
const FromV8ValueCallback& callback) const {
return FromV8Internal(value, out, isolate, callback);
}
bool ActivityLogConverterStrategy::FromV8Array(
v8::Handle<v8::Array> value,
base::Value** out,
v8::Isolate* isolate,
const FromV8ValueCallback& callback) const {
return FromV8Internal(value, out, isolate, callback);
}
bool ActivityLogConverterStrategy::FromV8Internal(
v8::Handle<v8::Object> value,
base::Value** out,
v8::Isolate* isolate,
const FromV8ValueCallback& callback) const {
scoped_ptr<base::Value> parsed_value;
if (enable_detailed_parsing_)
parsed_value = ParseV8Object(isolate, *value, callback);
if (!parsed_value.get())
parsed_value = SummarizeV8Value(isolate, value);
*out = parsed_value.release();
return true;
}
} // namespace extensions