| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/safe_browsing/malware_dom_details.h" | 5 #include "chrome/renderer/safe_browsing/malware_dom_details.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "chrome/common/chrome_constants.h" | 8 #include "chrome/common/chrome_constants.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/safebrowsing_messages.h" |
| 10 #include "chrome/common/render_messages_params.h" | |
| 11 #include "content/renderer/render_view.h" | 10 #include "content/renderer/render_view.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 18 | 17 |
| 19 namespace safe_browsing { | 18 namespace safe_browsing { |
| 20 | 19 |
| 21 // An upper limit on the number of nodes we collect. | 20 // An upper limit on the number of nodes we collect. |
| 22 uint32 MalwareDOMDetails::kMaxNodes = 500; | 21 uint32 MalwareDOMDetails::kMaxNodes = 500; |
| 23 | 22 |
| 24 MalwareDOMDetails::MalwareDOMDetails(RenderView* render_view) | 23 MalwareDOMDetails::MalwareDOMDetails(RenderView* render_view) |
| 25 : RenderViewObserver(render_view), | 24 : RenderViewObserver(render_view), |
| 26 render_view_(render_view) { | 25 render_view_(render_view) { |
| 27 } | 26 } |
| 28 | 27 |
| 29 MalwareDOMDetails::~MalwareDOMDetails() { | 28 MalwareDOMDetails::~MalwareDOMDetails() { |
| 30 } | 29 } |
| 31 | 30 |
| 32 bool MalwareDOMDetails::OnMessageReceived(const IPC::Message& message) { | 31 bool MalwareDOMDetails::OnMessageReceived(const IPC::Message& message) { |
| 33 bool handled = true; | 32 bool handled = true; |
| 34 IPC_BEGIN_MESSAGE_MAP(MalwareDOMDetails, message) | 33 IPC_BEGIN_MESSAGE_MAP(MalwareDOMDetails, message) |
| 35 IPC_MESSAGE_HANDLER(ViewMsg_GetMalwareDOMDetails, OnGetMalwareDOMDetails) | 34 IPC_MESSAGE_HANDLER(SafeBrowsingMsg_GetMalwareDOMDetails, OnGetMalwareDOMDet
ails) |
| 36 IPC_MESSAGE_UNHANDLED(handled = false) | 35 IPC_MESSAGE_UNHANDLED(handled = false) |
| 37 IPC_END_MESSAGE_MAP() | 36 IPC_END_MESSAGE_MAP() |
| 38 return handled; | 37 return handled; |
| 39 } | 38 } |
| 40 | 39 |
| 41 void MalwareDOMDetails::OnGetMalwareDOMDetails() { | 40 void MalwareDOMDetails::OnGetMalwareDOMDetails() { |
| 42 ViewHostMsg_MalwareDOMDetails_Params resources; | 41 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> resources; |
| 43 ExtractResources(&resources); | 42 ExtractResources(&resources); |
| 44 // Notify the browser. | 43 // Notify the browser. |
| 45 render_view()->Send(new ViewHostMsg_MalwareDOMDetails( | 44 render_view()->Send(new SafeBrowsingHostMsg_MalwareDOMDetails( |
| 46 render_view()->routing_id(), resources)); | 45 render_view()->routing_id(), resources)); |
| 47 } | 46 } |
| 48 | 47 |
| 49 void MalwareDOMDetails::ExtractResources( | 48 void MalwareDOMDetails::ExtractResources( |
| 50 ViewHostMsg_MalwareDOMDetails_Params* resources) { | 49 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>* resources) { |
| 51 WebKit::WebView* web_view = render_view_->webview(); | 50 WebKit::WebView* web_view = render_view_->webview(); |
| 52 if (!web_view) { | 51 if (!web_view) { |
| 53 NOTREACHED(); | 52 NOTREACHED(); |
| 54 return; | 53 return; |
| 55 } | 54 } |
| 56 WebKit::WebFrame* cur_frame = web_view->mainFrame(); | 55 WebKit::WebFrame* cur_frame = web_view->mainFrame(); |
| 57 for (; cur_frame; | 56 for (; cur_frame; |
| 58 cur_frame = cur_frame->traverseNext(false /* don't wrap around */)) { | 57 cur_frame = cur_frame->traverseNext(false /* don't wrap around */)) { |
| 59 DCHECK(cur_frame); | 58 DCHECK(cur_frame); |
| 60 ViewHostMsg_MalwareDOMDetails_Node frame_node; | 59 SafeBrowsingHostMsg_MalwareDOMDetails_Node frame_node; |
| 61 frame_node.url = GURL(cur_frame->url()); | 60 frame_node.url = GURL(cur_frame->url()); |
| 62 WebKit::WebDocument doc = cur_frame->document(); | 61 WebKit::WebDocument doc = cur_frame->document(); |
| 63 if (doc.isNull()) { | 62 if (doc.isNull()) { |
| 64 // Nothing in this frame, move on to the next one. | 63 // Nothing in this frame, move on to the next one. |
| 65 resources->nodes.push_back(frame_node); | 64 resources->push_back(frame_node); |
| 66 continue; | 65 continue; |
| 67 } | 66 } |
| 68 | 67 |
| 69 WebKit::WebNodeCollection elements = doc.all(); | 68 WebKit::WebNodeCollection elements = doc.all(); |
| 70 WebKit::WebNode cur_node = elements.firstItem(); | 69 WebKit::WebNode cur_node = elements.firstItem(); |
| 71 for (; !cur_node.isNull(); cur_node = elements.nextItem()) { | 70 for (; !cur_node.isNull(); cur_node = elements.nextItem()) { |
| 72 if (!cur_node.isElementNode()) { | 71 if (!cur_node.isElementNode()) { |
| 73 continue; | 72 continue; |
| 74 } | 73 } |
| 75 WebKit::WebElement element = cur_node.to<WebKit::WebElement>(); | 74 WebKit::WebElement element = cur_node.to<WebKit::WebElement>(); |
| 76 if (element.hasTagName("iframe") || element.hasTagName("frame") || | 75 if (element.hasTagName("iframe") || element.hasTagName("frame") || |
| 77 element.hasTagName("embed") || element.hasTagName("script")) { | 76 element.hasTagName("embed") || element.hasTagName("script")) { |
| 78 HandleElement(element, &frame_node, resources); | 77 HandleElement(element, &frame_node, resources); |
| 79 if (resources->nodes.size() >= kMaxNodes) { | 78 if (resources->size() >= kMaxNodes) { |
| 80 // We have reached kMaxNodes, exit early. | 79 // We have reached kMaxNodes, exit early. |
| 81 resources->nodes.push_back(frame_node); | 80 resources->push_back(frame_node); |
| 82 return; | 81 return; |
| 83 } | 82 } |
| 84 } | 83 } |
| 85 } | 84 } |
| 86 | 85 |
| 87 resources->nodes.push_back(frame_node); | 86 resources->push_back(frame_node); |
| 88 } | 87 } |
| 89 } | 88 } |
| 90 | 89 |
| 91 void MalwareDOMDetails::HandleElement( | 90 void MalwareDOMDetails::HandleElement( |
| 92 const WebKit::WebElement& element, | 91 const WebKit::WebElement& element, |
| 93 ViewHostMsg_MalwareDOMDetails_Node* parent_node, | 92 SafeBrowsingHostMsg_MalwareDOMDetails_Node* parent_node, |
| 94 ViewHostMsg_MalwareDOMDetails_Params* resources) { | 93 std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>* resources) { |
| 95 if (!element.hasAttribute("src")) { | 94 if (!element.hasAttribute("src")) { |
| 96 return; | 95 return; |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Retrieve the link and resolve the link in case it's relative. | 98 // Retrieve the link and resolve the link in case it's relative. |
| 100 WebKit::WebURL full_url = element.document().completeURL( | 99 WebKit::WebURL full_url = element.document().completeURL( |
| 101 element.getAttribute("src")); | 100 element.getAttribute("src")); |
| 102 | 101 |
| 103 const GURL& child_url = GURL(full_url); | 102 const GURL& child_url = GURL(full_url); |
| 104 | 103 |
| 105 // Add to the parent node. | 104 // Add to the parent node. |
| 106 parent_node->children.push_back(child_url); | 105 parent_node->children.push_back(child_url); |
| 107 | 106 |
| 108 // Create the child node. | 107 // Create the child node. |
| 109 ViewHostMsg_MalwareDOMDetails_Node child_node; | 108 SafeBrowsingHostMsg_MalwareDOMDetails_Node child_node; |
| 110 child_node.url = child_url; | 109 child_node.url = child_url; |
| 111 child_node.tag_name = element.tagName().utf8(); | 110 child_node.tag_name = element.tagName().utf8(); |
| 112 child_node.parent = parent_node->url; | 111 child_node.parent = parent_node->url; |
| 113 resources->nodes.push_back(child_node); | 112 resources->push_back(child_node); |
| 114 } | 113 } |
| 115 | 114 |
| 116 } // namespace safe_browsing | 115 } // namespace safe_browsing |
| OLD | NEW |