This source file includes following definitions.
- GetSavableResourceLinkForElement
- GetAllSavableResourceLinksForFrame
- GetSubResourceLinkFromElement
- GetAllSavableResourceLinksForCurrentPage
#include "content/renderer/savable_resources.h"
#include <set>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebVector.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/WebInputElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebNodeList.h"
#include "third_party/WebKit/public/web/WebView.h"
using blink::WebDocument;
using blink::WebElement;
using blink::WebElementCollection;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebNode;
using blink::WebNodeList;
using blink::WebString;
using blink::WebVector;
using blink::WebView;
namespace content {
namespace {
struct SavableResourcesUniqueCheck {
std::set<GURL>* resources_set;
std::set<GURL>* frames_set;
std::vector<WebFrame*>* frames;
SavableResourcesUniqueCheck()
: resources_set(NULL),
frames_set(NULL),
frames(NULL) {}
SavableResourcesUniqueCheck(std::set<GURL>* resources_set,
std::set<GURL>* frames_set, std::vector<WebFrame*>* frames)
: resources_set(resources_set),
frames_set(frames_set),
frames(frames) {}
};
void GetSavableResourceLinkForElement(
const WebElement& element,
const WebDocument& current_doc,
SavableResourcesUniqueCheck* unique_check,
SavableResourcesResult* result) {
if (element.hasTagName("iframe") ||
element.hasTagName("frame")) {
WebFrame* sub_frame = WebFrame::fromFrameOwnerElement(element);
if (sub_frame)
unique_check->frames->push_back(sub_frame);
return;
}
WebString value = GetSubResourceLinkFromElement(element);
if (value.isNull())
return;
GURL u = current_doc.completeURL(value);
if (!u.is_valid())
return;
if (!u.SchemeIsHTTPOrHTTPS() && !u.SchemeIs("file"))
return;
if (!unique_check->resources_set->insert(u).second)
return;
result->resources_list->push_back(u);
result->referrer_urls_list->push_back(GURL());
result->referrer_policies_list->push_back(blink::WebReferrerPolicyDefault);
}
void GetAllSavableResourceLinksForFrame(WebFrame* current_frame,
SavableResourcesUniqueCheck* unique_check,
SavableResourcesResult* result,
const char** savable_schemes) {
GURL current_frame_url = current_frame->document().url();
if (!current_frame_url.is_valid())
return;
bool is_valid_protocol = false;
for (int i = 0; savable_schemes[i] != NULL; ++i) {
if (current_frame_url.SchemeIs(savable_schemes[i])) {
is_valid_protocol = true;
break;
}
}
if (!is_valid_protocol)
return;
if (!unique_check->frames_set->insert(current_frame_url).second)
return;
WebDocument current_doc = current_frame->document();
WebElementCollection all = current_doc.all();
for (WebElement element = all.firstItem(); !element.isNull();
element = all.nextItem()) {
GetSavableResourceLinkForElement(element,
current_doc,
unique_check,
result);
}
}
}
WebString GetSubResourceLinkFromElement(const WebElement& element) {
const char* attribute_name = NULL;
if (element.hasHTMLTagName("img") ||
element.hasHTMLTagName("script")) {
attribute_name = "src";
} else if (element.hasHTMLTagName("input")) {
const WebInputElement input = element.toConst<WebInputElement>();
if (input.isImageButton()) {
attribute_name = "src";
}
} else if (element.hasHTMLTagName("body") ||
element.hasHTMLTagName("table") ||
element.hasHTMLTagName("tr") ||
element.hasHTMLTagName("td")) {
attribute_name = "background";
} else if (element.hasHTMLTagName("blockquote") ||
element.hasHTMLTagName("q") ||
element.hasHTMLTagName("del") ||
element.hasHTMLTagName("ins")) {
attribute_name = "cite";
} else if (element.hasHTMLTagName("link")) {
if (LowerCaseEqualsASCII(element.getAttribute("type"), "text/css")) {
attribute_name = "href";
}
}
if (!attribute_name)
return WebString();
WebString value = element.getAttribute(WebString::fromUTF8(attribute_name));
if (!value.isNull() && !value.isEmpty() &&
!StartsWithASCII(value.utf8(), "javascript:", false))
return value;
return WebString();
}
bool GetAllSavableResourceLinksForCurrentPage(WebView* view,
const GURL& page_url, SavableResourcesResult* result,
const char** savable_schemes) {
WebFrame* main_frame = view->mainFrame();
if (!main_frame)
return false;
std::set<GURL> resources_set;
std::set<GURL> frames_set;
std::vector<WebFrame*> frames;
SavableResourcesUniqueCheck unique_check(&resources_set,
&frames_set,
&frames);
GURL main_page_gurl(main_frame->document().url());
if (page_url != main_page_gurl)
return true;
frames.push_back(main_frame);
for (int i = 0; i < static_cast<int>(frames.size()); ++i) {
GetAllSavableResourceLinksForFrame(frames[i], &unique_check, result,
savable_schemes);
}
for (std::set<GURL>::iterator it = frames_set.begin();
it != frames_set.end(); ++it) {
if (resources_set.find(*it) == resources_set.end())
result->frames_list->push_back(*it);
}
return true;
}
}