summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authornoelutz@google.com <noelutz@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-11 21:53:16 +0000
committernoelutz@google.com <noelutz@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-11 21:53:16 +0000
commitbd1b1d69cb9ac5f7cb03aef7746808a0f0d480c9 (patch)
treebeabf815d613827f78014ab479258ca61fdbd73a /chrome/common
parente221eeb710a2b463402d4550688bb9e0d9cdd496 (diff)
downloadchromium_src-bd1b1d69cb9ac5f7cb03aef7746808a0f0d480c9.zip
chromium_src-bd1b1d69cb9ac5f7cb03aef7746808a0f0d480c9.tar.gz
chromium_src-bd1b1d69cb9ac5f7cb03aef7746808a0f0d480c9.tar.bz2
Add a client-side phishing detection service class.
This class is responsible for talking to the client-side detection servers and to fetch the machine learning model. BUG=none TEST=ClientSideDetectionServiceTest Review URL: http://codereview.chromium.org/3815014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65860 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/net/test_url_fetcher_factory.cc77
-rw-r--r--chrome/common/net/test_url_fetcher_factory.h68
2 files changed, 144 insertions, 1 deletions
diff --git a/chrome/common/net/test_url_fetcher_factory.cc b/chrome/common/net/test_url_fetcher_factory.cc
index 635319f3..48fb314 100644
--- a/chrome/common/net/test_url_fetcher_factory.cc
+++ b/chrome/common/net/test_url_fetcher_factory.cc
@@ -3,6 +3,10 @@
// found in the LICENSE file.
#include "chrome/common/net/test_url_fetcher_factory.h"
+#include <string>
+
+#include "base/message_loop.h"
+#include "net/url_request/url_request_status.h"
TestURLFetcher::TestURLFetcher(int id,
const GURL& url,
@@ -33,3 +37,76 @@ void TestURLFetcherFactory::RemoveFetcherFromMap(int id) {
DCHECK(i != fetchers_.end());
fetchers_.erase(i);
}
+
+// This class is used by the FakeURLFetcherFactory below.
+class FakeURLFetcher : public URLFetcher {
+ public:
+ // Normal URL fetcher constructor but also takes in a pre-baked response.
+ FakeURLFetcher(const GURL& url, RequestType request_type, Delegate* d,
+ const std::string& response_data, bool success)
+ : URLFetcher(url, request_type, d),
+ url_(url),
+ response_data_(response_data),
+ success_(success),
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
+ }
+
+ // Start the request. This will call the given delegate asynchronously
+ // with the pre-baked response as parameter.
+ virtual void Start() {
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate));
+ }
+
+ private:
+ virtual ~FakeURLFetcher() {
+ }
+
+ // This is the method which actually calls the delegate that is passed in the
+ // constructor.
+ void RunDelegate() {
+ URLRequestStatus status;
+ status.set_status(success_ ? URLRequestStatus::SUCCESS :
+ URLRequestStatus::FAILED);
+ delegate()->OnURLFetchComplete(this, url_, status, success_ ? 200 : 500,
+ ResponseCookies(), response_data_);
+ }
+
+ // Pre-baked response data and flag which indicates whether the request should
+ // be successful or not.
+ GURL url_;
+ std::string response_data_;
+ bool success_;
+
+ // Method factory used to run the delegate.
+ ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher);
+};
+
+URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
+ int id,
+ const GURL& url,
+ URLFetcher::RequestType request_type,
+ URLFetcher::Delegate* d) {
+ FakeResponseMap::const_iterator it = fake_responses_.find(url);
+ if (it == fake_responses_.end()) {
+ // If we don't have a baked response for that URL we return NULL.
+ DLOG(ERROR) << "No baked response for URL: " << url.spec();
+ return NULL;
+ }
+ return new FakeURLFetcher(url, request_type, d,
+ it->second.first, it->second.second);
+}
+
+void FakeURLFetcherFactory::SetFakeResponse(const std::string& url,
+ const std::string& response_data,
+ bool success) {
+ // Overwrite existing URL if it already exists.
+ fake_responses_[GURL(url)] = std::make_pair(response_data, success);
+}
+
+void FakeURLFetcherFactory::ClearFakeReponses() {
+ fake_responses_.clear();
+}
diff --git a/chrome/common/net/test_url_fetcher_factory.h b/chrome/common/net/test_url_fetcher_factory.h
index 3afa19e..73f4b46 100644
--- a/chrome/common/net/test_url_fetcher_factory.h
+++ b/chrome/common/net/test_url_fetcher_factory.h
@@ -8,6 +8,7 @@
#include <map>
#include <string>
+#include <utility>
#include "chrome/common/net/url_fetcher.h"
#include "googleurl/src/gurl.h"
@@ -35,7 +36,10 @@
// ...
// // Reset factory.
// URLFetcher::set_factory(NULL);
-
+//
+// Note: if you don't know when your request objects will be created you
+// might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes
+// below.
class TestURLFetcher : public URLFetcher {
public:
@@ -89,4 +93,66 @@ class TestURLFetcherFactory : public URLFetcher::Factory {
DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory);
};
+// The FakeUrlFetcher and FakeUrlFetcherFactory classes are similar to the
+// ones above but don't require you to know when exactly the URLFetcher objects
+// will be created.
+//
+// These classes let you set pre-baked HTTP responses for particular URLs.
+// E.g., if the user requests http://a.com/ then respond with an HTTP/500.
+//
+// We assume that the thread that is calling Start() on the URLFetcher object
+// has a message loop running.
+//
+// This class is not thread-safe. You should not call SetFakeResponse or
+// ClearFakeResponse at the same time you call CreateURLFetcher. However, it is
+// OK to start URLFetcher objects while setting or clearning fake responses
+// since already created URLFetcher objects will not be affected by any changes
+// made to the fake responses (once a URLFetcher object is created you cannot
+// change its fake response).
+//
+// Example usage:
+// FakeURLFetcherFactory factory;
+// URLFetcher::set_factory(&factory);
+//
+// // You know that class SomeService will request url http://a.com/ and you
+// // want to test the service class by returning an error.
+// factory.SetFakeResponse("http://a.com/", "", false);
+// // But if the service requests http://b.com/asdf you want to respond with
+// // a simple html page and an HTTP/200 code.
+// factory.SetFakeResponse("http://b.com/asdf",
+// "<html><body>hello world</body></html>",
+// true);
+//
+// SomeService service;
+// service.Run(); // Will eventually request these two URLs.
+
+class FakeURLFetcherFactory : public URLFetcher::Factory {
+ public:
+ FakeURLFetcherFactory() {}
+
+ // If no fake response is set for the given URL this method will return NULL.
+ // Otherwise, it will return a URLFetcher object which will respond with the
+ // pre-baked response that the client has set by calling SetFakeResponse().
+ virtual URLFetcher* CreateURLFetcher(int id,
+ const GURL& url,
+ URLFetcher::RequestType request_type,
+ URLFetcher::Delegate* d);
+
+ // Sets the fake response for a given URL. If success is true we will serve
+ // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty.
+ void SetFakeResponse(const std::string& url,
+ const std::string& response_data,
+ bool success);
+
+ // Clear all the fake responses that were previously set via
+ // SetFakeResponse().
+ void ClearFakeReponses();
+
+ private:
+ typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap;
+ FakeResponseMap fake_responses_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory);
+};
+
#endif // CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_