This source file includes following definitions.
- Create
- OnMessageReceived
- OnGetMalwareDOMDetails
- ExtractResources
- HandleElement
#include "chrome/renderer/safe_browsing/malware_dom_details.h"
#include "base/compiler_specific.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/safe_browsing/safebrowsing_messages.h"
#include "content/public/renderer/render_view.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebElementCollection.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
namespace safe_browsing {
uint32 MalwareDOMDetails::kMaxNodes = 500;
MalwareDOMDetails* MalwareDOMDetails::Create(content::RenderView* render_view) {
return new MalwareDOMDetails(render_view);
}
MalwareDOMDetails::MalwareDOMDetails(content::RenderView* render_view)
: content::RenderViewObserver(render_view) {
}
MalwareDOMDetails::~MalwareDOMDetails() {
}
bool MalwareDOMDetails::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MalwareDOMDetails, message)
IPC_MESSAGE_HANDLER(SafeBrowsingMsg_GetMalwareDOMDetails,
OnGetMalwareDOMDetails)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void MalwareDOMDetails::OnGetMalwareDOMDetails() {
std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node> resources;
ExtractResources(&resources);
render_view()->Send(new SafeBrowsingHostMsg_MalwareDOMDetails(
render_view()->GetRoutingID(), resources));
}
void MalwareDOMDetails::ExtractResources(
std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>* resources) {
blink::WebView* web_view = render_view()->GetWebView();
if (!web_view) {
NOTREACHED();
return;
}
blink::WebFrame* frame = web_view->mainFrame();
for (; frame; frame = frame->traverseNext(false )) {
DCHECK(frame);
SafeBrowsingHostMsg_MalwareDOMDetails_Node details_node;
blink::WebDocument document = frame->document();
details_node.url = GURL(document.url());
if (document.isNull()) {
resources->push_back(details_node);
continue;
}
blink::WebElementCollection elements = document.all();
blink::WebElement element = elements.firstItem();
for (; !element.isNull(); element = elements.nextItem()) {
if (element.hasTagName("iframe") || element.hasTagName("frame") ||
element.hasTagName("embed") || element.hasTagName("script")) {
HandleElement(element, &details_node, resources);
if (resources->size() >= kMaxNodes) {
resources->push_back(details_node);
return;
}
}
}
resources->push_back(details_node);
}
}
void MalwareDOMDetails::HandleElement(
const blink::WebElement& element,
SafeBrowsingHostMsg_MalwareDOMDetails_Node* parent_node,
std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>* resources) {
if (!element.hasAttribute("src")) {
return;
}
blink::WebURL full_url = element.document().completeURL(
element.getAttribute("src"));
const GURL& child_url = GURL(full_url);
parent_node->children.push_back(child_url);
SafeBrowsingHostMsg_MalwareDOMDetails_Node child_node;
child_node.url = child_url;
child_node.tag_name = element.tagName().utf8();
child_node.parent = parent_node->url;
resources->push_back(child_node);
}
}