上一篇說了adbock plus的匹配規則,但是有一些規則需要其他資訊,而不是簡單的url字串就可以處理了。
比如域資訊,像third-party規則等。目前只打算支援script,image,stylesheet,third-party,domain規則其中script,image,stylesheet規則通過url字串的中的副檔名來匹配,third-party,domain則藉助KURL類進行解析,因此設計新的FilterRule介面如下:
#ifndef FILTER_H
#define FILTER_H
#include "PlatformString.h"
#include "FilterManager.h"
#include <wtf/Vector.h>
#include <wtf/HashMap.h>
#include "KURL.h"
namespace WebCore {
class FilterRule {
public:
/*
首先manager已經判斷過是過濾而不是隱藏規則了
以@@開始,則是白名單,manager會優先考慮
以||開始則是不匹配協議名的過濾,並去掉||
以|開始,則去掉|,否則在開始處添加*
含有$類型指定規則,去掉這些字串,並處理類型
以|結尾,去掉|,否則在結尾處添加*
*/
FilterRule( const String & rule);
/*
是否應該過濾,如果是白名單,匹配則應該不過濾,否則過濾
類型用於只過濾adlbock plus規則中指定的類型。
*/
bool shouldFilter(const KURL & mainURL,const
KURL & url, FilterType t);
//是否是白名單
bool isWhiteFilter() { return m_isException;}
//是否是通過類型來進行過濾,比如只過濾指令碼等。這個可能需要很多資訊,暫時不予考慮,比如domain類型過濾,
bool isNeedMimeType() { return m_type!=0;}
const String & getRegularFilter(){ return
m_reFilter;}
const String & getWholeRule() { return
m_rule;}
//inline const StringVector & constantsForFastSearch() {return constants;}
void print();
private:
bool m_isException; // start with @@ //白名單
bool m_isMatchProtocol;
/*
adblock rule describe
in regular expression
*/
String m_reFilter;
//StringVector constants;
String m_rule;
/*
Type options: determine
which types of elements a filter can block (or whitelist in case of an exception
rule). Multiple type options can be specified to indicate that the filter should
be applied to several types of elements. Possible types are:
*/
FilterType m_type;
/*
Restriction to third-party/first-party
requests: If the third-party option is
specified, the filter is only applied
to requests from a different origin
than the currently viewed page.
Similarly, ~third-party restricts the filter
to requests from the same origin
as the currently viewed page.
*/
bool m_filterThirdParty;
bool m_matchFirstParty;
/*
Domain restrictions: The option
domain=example.com means that the filter
should only be applied on pages
from “example.com” domain. Multiple domains
can be specified using “|” as separator:
with the option
domain=example.com|example.net the
filter will only be applied on pages from
“example.com” or “example.net” domains.
If a domain name is preceded with
“~”, the filter should not be applied
on pages from this domain. For example,
domain=~example.com means that the
filter should be applied on pages from any
domain but “example.com” and domain=example.com|~foo.example.com
restricts
the filter to the “example.com”
domain with the exception of
“foo.example.com” subdomain.
*/
Vector<String> m_domains;
Vector<String> m_inverseDomains;
private:
bool isMatchType(const KURL & url,FilterType
t);
bool isMatchThirdParty(const KURL &
host,const KURL & other);
bool isMatchDomains( const KURL & url);
bool processDomains(String & ds);
};
//隱藏規則,含有##的規則
class HideRule {
public:
/*
將##之前的字串解析為一組網域名稱,後面的原封不動,作為css選取器來處理。
*/
HideRule(const String & r);
//隱藏規則適用的domain。如果為空白,則適用於所有,否則只適用於指明的domain
const StringVector & domains();
//example.com,~foo.example.com##*.sponsor
//*.sponsor就是selector
const String & selector();
void print();
private:
String m_sel;
StringVector m_domains;
};
}
#endif // FILTER_H