Commit 14140a91 authored by battre@chromium.org's avatar battre@chromium.org

Migrate WebRequestRedirectByRegExAction to use RE2


BUG=112155
TBR=ben@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10826120

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149852 0039d316-1c4b-4281-b951-d872f2087c98
parent 8ad40981
......@@ -65,6 +65,7 @@ include_rules = [
"+third_party/libevent", # For the remote V8 debugging server
"+third_party/libjingle",
"+third_party/protobuf/src/google/protobuf",
"+third_party/re2",
"+third_party/sqlite",
"+third_party/undoview",
......
......@@ -10,7 +10,6 @@
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/declarative_webrequest/request_stages.h"
#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h"
......@@ -19,6 +18,7 @@
#include "chrome/browser/extensions/extension_info_map.h"
#include "chrome/common/extensions/extension.h"
#include "net/url_request/url_request.h"
#include "third_party/re2/re2/re2.h"
namespace extensions {
......@@ -108,20 +108,18 @@ scoped_ptr<WebRequestAction> CreateRedirectRequestByRegExAction(
INPUT_FORMAT_VALIDATE(dict->GetString(keys::kFromKey, &from));
INPUT_FORMAT_VALIDATE(dict->GetString(keys::kToKey, &to));
// TODO(battre): Add this line once we migrate from ICU RegEx to RE2 RegEx.s
// to = WebRequestRedirectByRegExAction::PerlToRe2Style(to);
to = WebRequestRedirectByRegExAction::PerlToRe2Style(to);
UParseError parse_error;
UErrorCode status = U_ZERO_ERROR;
scoped_ptr<icu::RegexPattern> pattern(
icu::RegexPattern::compile(icu::UnicodeString(from.data(), from.size()),
0, parse_error, status));
if (U_FAILURE(status) || !pattern.get()) {
RE2::Options options;
options.set_case_sensitive(false);
scoped_ptr<RE2> from_pattern(new RE2(from, options));
if (!from_pattern->ok()) {
*error = "Invalid pattern '" + from + "' -> '" + to + "'";
return scoped_ptr<WebRequestAction>(NULL);
}
return scoped_ptr<WebRequestAction>(
new WebRequestRedirectByRegExAction(pattern.Pass(), to));
new WebRequestRedirectByRegExAction(from_pattern.Pass(), to));
}
scoped_ptr<WebRequestAction> CreateSetRequestHeaderAction(
......@@ -590,7 +588,7 @@ WebRequestRedirectToEmptyDocumentAction::CreateDelta(
//
WebRequestRedirectByRegExAction::WebRequestRedirectByRegExAction(
scoped_ptr<icu::RegexPattern> from_pattern,
scoped_ptr<RE2> from_pattern,
const std::string& to_pattern)
: from_pattern_(from_pattern.Pass()),
to_pattern_(to_pattern.data(), to_pattern.size()) {}
......@@ -667,29 +665,17 @@ LinkedPtrEventResponseDelta WebRequestRedirectByRegExAction::CreateDelta(
CHECK(request_stage & GetStages());
CHECK(from_pattern_.get());
UErrorCode status = U_ZERO_ERROR;
const std::string& old_url = request->url().spec();
icu::UnicodeString old_url_unicode(old_url.data(), old_url.size());
scoped_ptr<icu::RegexMatcher> matcher(
from_pattern_->matcher(old_url_unicode, status));
if (U_FAILURE(status) || !matcher.get())
return LinkedPtrEventResponseDelta(NULL);
icu::UnicodeString new_url = matcher->replaceAll(to_pattern_, status);
if (U_FAILURE(status))
return LinkedPtrEventResponseDelta(NULL);
std::string new_url_utf8;
UTF16ToUTF8(new_url.getBuffer(), new_url.length(), &new_url_utf8);
if (new_url_utf8 == request->url().spec())
std::string new_url = old_url;
if (!RE2::Replace(&new_url, *from_pattern_, to_pattern_) ||
new_url == old_url) {
return LinkedPtrEventResponseDelta(NULL);
}
LinkedPtrEventResponseDelta result(
new extension_web_request_api_helpers::EventResponseDelta(
extension_id, extension_install_time));
result->new_url = GURL(new_url_utf8);
result->new_url = GURL(new_url);
return result;
}
......
......@@ -16,7 +16,6 @@
#include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
#include "chrome/common/extensions/api/events.h"
#include "googleurl/src/gurl.h"
#include "unicode/regex.h"
class WebRequestPermission;
......@@ -38,6 +37,10 @@ namespace net {
class URLRequest;
}
namespace re2 {
class RE2;
}
namespace extensions {
typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta>
......@@ -248,11 +251,10 @@ class WebRequestRedirectToEmptyDocumentAction : public WebRequestAction {
// Action that instructs to redirect a network request.
class WebRequestRedirectByRegExAction : public WebRequestAction {
public:
// The |to_pattern| has to be passed in ICU syntax.
// TODO(battre): Change this to Perl style when migrated to RE2.
explicit WebRequestRedirectByRegExAction(
scoped_ptr<icu::RegexPattern> from_pattern,
const std::string& to_pattern);
// The |to_pattern| has to be passed in RE2 syntax with the exception that
// capture groups are referenced in Perl style ($1, $2, ...).
WebRequestRedirectByRegExAction(scoped_ptr<re2::RE2> from_pattern,
const std::string& to_pattern);
virtual ~WebRequestRedirectByRegExAction();
// Conversion of capture group styles between Perl style ($1, $2, ...) and
......@@ -270,8 +272,8 @@ class WebRequestRedirectByRegExAction : public WebRequestAction {
const base::Time& extension_install_time) const OVERRIDE;
private:
scoped_ptr<icu::RegexPattern> from_pattern_;
icu::UnicodeString to_pattern_;
scoped_ptr<re2::RE2> from_pattern_;
std::string to_pattern_;
DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectByRegExAction);
};
......
......@@ -38,6 +38,7 @@
'../third_party/icu/icu.gyp:icuuc',
'../third_party/leveldatabase/leveldatabase.gyp:leveldatabase',
'../third_party/libusb/libusb.gyp:libusb',
'../third_party/re2/re2.gyp:re2',
'../ui/base/strings/ui_strings.gyp:ui_strings',
'../ui/ui.gyp:ui',
'../ui/ui.gyp:ui_resources',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment