blob: 79624ddf256ca37b5cf7f3ee6b86b5ef4fddefc3 (
plain)
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
|
// Copyright (c) 2011 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 "chrome/renderer/safe_browsing/malware_dom_details.h"
#include "base/compiler_specific.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/render_messages_params.h"
#include "content/renderer/render_view.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
namespace safe_browsing {
// An upper limit on the number of nodes we collect.
uint32 MalwareDOMDetails::kMaxNodes = 500;
MalwareDOMDetails::MalwareDOMDetails(RenderView* render_view)
: RenderViewObserver(render_view),
render_view_(render_view) {
}
MalwareDOMDetails::~MalwareDOMDetails() {
}
bool MalwareDOMDetails::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MalwareDOMDetails, message)
IPC_MESSAGE_HANDLER(ViewMsg_GetMalwareDOMDetails, OnGetMalwareDOMDetails)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void MalwareDOMDetails::OnGetMalwareDOMDetails() {
ViewHostMsg_MalwareDOMDetails_Params resources;
ExtractResources(&resources);
// Notify the browser.
render_view()->Send(new ViewHostMsg_MalwareDOMDetails(
render_view()->routing_id(), resources));
}
void MalwareDOMDetails::ExtractResources(
ViewHostMsg_MalwareDOMDetails_Params* resources) {
WebKit::WebView* web_view = render_view_->webview();
if (!web_view) {
NOTREACHED();
return;
}
WebKit::WebFrame* cur_frame = web_view->mainFrame();
for (; cur_frame;
cur_frame = cur_frame->traverseNext(false /* don't wrap around */)) {
DCHECK(cur_frame);
ViewHostMsg_MalwareDOMDetails_Node frame_node;
frame_node.url = GURL(cur_frame->url());
WebKit::WebDocument doc = cur_frame->document();
if (doc.isNull()) {
// Nothing in this frame, move on to the next one.
resources->nodes.push_back(frame_node);
continue;
}
WebKit::WebNodeCollection elements = doc.all();
WebKit::WebNode cur_node = elements.firstItem();
for (; !cur_node.isNull(); cur_node = elements.nextItem()) {
if (!cur_node.isElementNode()) {
continue;
}
WebKit::WebElement element = cur_node.to<WebKit::WebElement>();
if (element.hasTagName("iframe") || element.hasTagName("frame") ||
element.hasTagName("embed") || element.hasTagName("script")) {
HandleElement(element, &frame_node, resources);
if (resources->nodes.size() >= kMaxNodes) {
// We have reached kMaxNodes, exit early.
resources->nodes.push_back(frame_node);
return;
}
}
}
resources->nodes.push_back(frame_node);
}
}
void MalwareDOMDetails::HandleElement(
const WebKit::WebElement& element,
ViewHostMsg_MalwareDOMDetails_Node* parent_node,
ViewHostMsg_MalwareDOMDetails_Params* resources) {
if (!element.hasAttribute("src")) {
return;
}
// Retrieve the link and resolve the link in case it's relative.
WebKit::WebURL full_url = element.document().completeURL(
element.getAttribute("src"));
const GURL& child_url = GURL(full_url);
// Add to the parent node.
parent_node->children.push_back(child_url);
// Create the child node.
ViewHostMsg_MalwareDOMDetails_Node child_node;
child_node.url = child_url;
child_node.tag_name = element.tagName().utf8();
child_node.parent = parent_node->url;
resources->nodes.push_back(child_node);
}
} // namespace safe_browsing
|