Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/renderer/safe_browsing/threat_dom_details.cc

Issue 2667343006: Componentize safe_browsing [X+1] : move the renderer part to component.
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/safe_browsing/threat_dom_details.h"
6
7 #include <map>
8
9 #include "base/compiler_specific.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/safe_browsing/common/safebrowsing_messages.h"
12 #include "content/public/renderer/render_frame.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebElement.h"
16 #include "third_party/WebKit/public/web/WebElementCollection.h"
17 #include "third_party/WebKit/public/web/WebFrame.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19
20 namespace safe_browsing {
21
22 // A map for keeping track of the identity of DOM Elements, used to generate
23 // unique IDs for each element and lookup elements IDs by parent Element, to
24 // maintain proper parent/child relationships.
25 // They key is a WebNode from the DOM, which is basically a pointer so can be
26 // copied into the map when inserting new elements.
27 // The values are pointers to IPC messages generated by ThreatDOMDetails. They
28 // are not owned by the map - ownership remains with the vector of resources
29 // collected by this class.
30 typedef std::map<blink::WebNode, SafeBrowsingHostMsg_ThreatDOMDetails_Node*>
31 ElementToNodeMap;
32
33 namespace {
34
35 // Handler for the various HTML elements that we extract URLs from.
36 void HandleElement(
37 const blink::WebElement& element,
38 SafeBrowsingHostMsg_ThreatDOMDetails_Node* parent_node,
39 std::vector<SafeBrowsingHostMsg_ThreatDOMDetails_Node>* resources,
40 safe_browsing::ElementToNodeMap* element_to_node_map) {
41 if (!element.hasAttribute("src"))
42 return;
43
44 // Retrieve the link and resolve the link in case it's relative.
45 blink::WebURL full_url =
46 element.document().completeURL(element.getAttribute("src"));
47
48 const GURL& child_url = GURL(full_url);
49
50 // Add to the parent node.
51 parent_node->children.push_back(child_url);
52
53 // Create the child node.
54 resources->push_back(SafeBrowsingHostMsg_ThreatDOMDetails_Node());
55 SafeBrowsingHostMsg_ThreatDOMDetails_Node* child_node = &resources->back();
56 child_node->url = child_url;
57 child_node->tag_name = element.tagName().utf8();
58 child_node->parent = parent_node->url;
59
60 // Update the ID mapping. First generate the ID for the current node.
61 // Then, if its parent is available, set the current node's parent ID, and
62 // also update the parent's children with the current node's ID.
63 const int child_id = element_to_node_map->size() + 1;
64 child_node->node_id = child_id;
65 if (!element.parentNode().isNull()) {
66 auto parent_node_iter = element_to_node_map->find(element.parentNode());
67 if (parent_node_iter != element_to_node_map->end()) {
68 child_node->parent_node_id = parent_node->node_id;
69 parent_node->child_node_ids.push_back(child_id);
70 }
71 }
72 (*element_to_node_map)[element] = child_node;
73 }
74
75 } // namespace
76
77 // An upper limit on the number of nodes we collect.
78 uint32_t ThreatDOMDetails::kMaxNodes = 500;
79
80 // static
81 ThreatDOMDetails* ThreatDOMDetails::Create(content::RenderFrame* render_frame) {
82 // Private constructor and public static Create() method to facilitate
83 // stubbing out this class for binary-size reduction purposes.
84 return new ThreatDOMDetails(render_frame);
85 }
86
87 ThreatDOMDetails::ThreatDOMDetails(content::RenderFrame* render_frame)
88 : content::RenderFrameObserver(render_frame) {}
89
90 ThreatDOMDetails::~ThreatDOMDetails() {}
91
92 bool ThreatDOMDetails::OnMessageReceived(const IPC::Message& message) {
93 bool handled = true;
94 IPC_BEGIN_MESSAGE_MAP(ThreatDOMDetails, message)
95 IPC_MESSAGE_HANDLER(SafeBrowsingMsg_GetThreatDOMDetails,
96 OnGetThreatDOMDetails)
97 IPC_MESSAGE_UNHANDLED(handled = false)
98 IPC_END_MESSAGE_MAP()
99 return handled;
100 }
101
102 void ThreatDOMDetails::OnGetThreatDOMDetails() {
103 std::vector<SafeBrowsingHostMsg_ThreatDOMDetails_Node> resources;
104 ExtractResources(&resources);
105 // Notify the browser.
106 Send(new SafeBrowsingHostMsg_ThreatDOMDetails(routing_id(), resources));
107 }
108
109 void ThreatDOMDetails::ExtractResources(
110 std::vector<SafeBrowsingHostMsg_ThreatDOMDetails_Node>* resources) {
111 blink::WebFrame* frame = render_frame()->GetWebFrame();
112 if (!frame)
113 return;
114 SafeBrowsingHostMsg_ThreatDOMDetails_Node details_node;
115 blink::WebDocument document = frame->document();
116 details_node.url = GURL(document.url());
117 if (document.isNull()) {
118 // Nothing in this frame. Just report its URL.
119 resources->push_back(details_node);
120 return;
121 }
122
123 ElementToNodeMap element_to_node_map;
124 blink::WebElementCollection elements = document.all();
125 blink::WebElement element = elements.firstItem();
126 for (; !element.isNull(); element = elements.nextItem()) {
127 if (element.hasHTMLTagName("iframe") || element.hasHTMLTagName("frame") ||
128 element.hasHTMLTagName("embed") || element.hasHTMLTagName("script")) {
129 HandleElement(element, &details_node, resources, &element_to_node_map);
130 if (resources->size() >= kMaxNodes) {
131 // We have reached kMaxNodes, exit early.
132 resources->push_back(details_node);
133 return;
134 }
135 }
136 }
137 resources->push_back(details_node);
138 }
139
140 void ThreatDOMDetails::OnDestruct() {
141 delete this;
142 }
143
144 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/renderer/safe_browsing/threat_dom_details.h ('k') | chrome/renderer/safe_browsing/threat_dom_details_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698