Commit c4efa79a authored by Bruno Kim Medeiros Cesar's avatar Bruno Kim Medeiros Cesar Committed by Commit Bot

Provide user's region code for Safe Search API.

Bug: 857533
Change-Id: I1aa1d88ba50f179fa73b1d563f7e3bc45d1176e4
Reviewed-on: https://chromium-review.googlesource.com/1122637
Commit-Queue: Bruno Kim Medeiros Cesar <brunokim@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575298}
parent a28f6b88
...@@ -22,11 +22,13 @@ ...@@ -22,11 +22,13 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/task_runner_util.h" #include "base/task_runner_util.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h" #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
#include "components/google/core/browser/google_util.h" #include "components/google/core/browser/google_util.h"
#include "components/policy/core/browser/url_blacklist_manager.h" #include "components/policy/core/browser/url_blacklist_manager.h"
#include "components/url_formatter/url_fixer.h" #include "components/url_formatter/url_fixer.h"
#include "components/url_matcher/url_matcher.h" #include "components/url_matcher/url_matcher.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "net/base/escape.h" #include "net/base/escape.h"
...@@ -553,8 +555,14 @@ void SupervisedUserURLFilter::InitAsyncURLChecker( ...@@ -553,8 +555,14 @@ void SupervisedUserURLFilter::InitAsyncURLChecker(
"family dashboard." "family dashboard."
policy_exception_justification: "Not implemented." policy_exception_justification: "Not implemented."
})"); })");
std::string country;
variations::VariationsService* variations_service =
g_browser_process->variations_service();
if (variations_service)
country = variations_service->GetLatestCountry();
async_url_checker_ = std::make_unique<safe_search_api::URLChecker>( async_url_checker_ = std::make_unique<safe_search_api::URLChecker>(
std::move(url_loader_factory), traffic_annotation); std::move(url_loader_factory), traffic_annotation, country);
} }
void SupervisedUserURLFilter::ClearAsyncURLChecker() { void SupervisedUserURLFilter::ClearAsyncURLChecker() {
......
...@@ -34,15 +34,18 @@ namespace { ...@@ -34,15 +34,18 @@ namespace {
const char kSafeSearchApiUrl[] = const char kSafeSearchApiUrl[] =
"https://safesearch.googleapis.com/v1:classify"; "https://safesearch.googleapis.com/v1:classify";
const char kDataContentType[] = "application/x-www-form-urlencoded"; const char kDataContentType[] = "application/x-www-form-urlencoded";
const char kDataFormat[] = "key=%s&urls=%s"; const char kDataFormat[] = "key=%s&urls=%s&region_code=%s";
const size_t kDefaultCacheSize = 1000; const size_t kDefaultCacheSize = 1000;
const size_t kDefaultCacheTimeoutSeconds = 3600; const size_t kDefaultCacheTimeoutSeconds = 3600;
// Builds the POST data for SafeSearch API requests. // Builds the POST data for SafeSearch API requests.
std::string BuildRequestData(const std::string& api_key, const GURL& url) { std::string BuildRequestData(const std::string& api_key,
const GURL& url,
const std::string& region_code) {
std::string query = net::EscapeQueryParamValue(url.spec(), true); std::string query = net::EscapeQueryParamValue(url.spec(), true);
return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str()); return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str(),
region_code.c_str());
} }
// Parses a SafeSearch API |response| and stores the result in |is_porn|. // Parses a SafeSearch API |response| and stores the result in |is_porn|.
...@@ -114,17 +117,21 @@ URLChecker::CheckResult::CheckResult(Classification classification, ...@@ -114,17 +117,21 @@ URLChecker::CheckResult::CheckResult(Classification classification,
URLChecker::URLChecker( URLChecker::URLChecker(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const net::NetworkTrafficAnnotationTag& traffic_annotation) const net::NetworkTrafficAnnotationTag& traffic_annotation,
const std::string& country)
: URLChecker(std::move(url_loader_factory), : URLChecker(std::move(url_loader_factory),
traffic_annotation, traffic_annotation,
country,
kDefaultCacheSize) {} kDefaultCacheSize) {}
URLChecker::URLChecker( URLChecker::URLChecker(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const net::NetworkTrafficAnnotationTag& traffic_annotation, const net::NetworkTrafficAnnotationTag& traffic_annotation,
const std::string& country,
size_t cache_size) size_t cache_size)
: url_loader_factory_(std::move(url_loader_factory)), : url_loader_factory_(std::move(url_loader_factory)),
traffic_annotation_(traffic_annotation), traffic_annotation_(traffic_annotation),
country_(country),
cache_(cache_size), cache_(cache_size),
cache_timeout_( cache_timeout_(
base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {} base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {}
...@@ -183,8 +190,8 @@ bool URLChecker::CheckURL(const GURL& url, CheckCallback callback) { ...@@ -183,8 +190,8 @@ bool URLChecker::CheckURL(const GURL& url, CheckCallback callback) {
std::unique_ptr<network::SimpleURLLoader> simple_url_loader = std::unique_ptr<network::SimpleURLLoader> simple_url_loader =
network::SimpleURLLoader::Create(std::move(resource_request), network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation_); traffic_annotation_);
simple_url_loader->AttachStringForUpload(BuildRequestData(api_key, url), simple_url_loader->AttachStringForUpload(
kDataContentType); BuildRequestData(api_key, url, country_), kDataContentType);
auto it = checks_in_progress_.insert( auto it = checks_in_progress_.insert(
checks_in_progress_.begin(), checks_in_progress_.begin(),
std::make_unique<Check>(url, std::move(simple_url_loader), std::make_unique<Check>(url, std::move(simple_url_loader),
......
...@@ -43,10 +43,14 @@ class URLChecker { ...@@ -43,10 +43,14 @@ class URLChecker {
using CheckCallback = base::OnceCallback< using CheckCallback = base::OnceCallback<
void(const GURL&, Classification classification, bool /* uncertain */)>; void(const GURL&, Classification classification, bool /* uncertain */)>;
// |country| should be a two-letter country code (ISO 3166-1 alpha-2), e.g.,
// "us".
URLChecker(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, URLChecker(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const net::NetworkTrafficAnnotationTag& traffic_annotation); const net::NetworkTrafficAnnotationTag& traffic_annotation,
const std::string& country);
URLChecker(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, URLChecker(scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const net::NetworkTrafficAnnotationTag& traffic_annotation, const net::NetworkTrafficAnnotationTag& traffic_annotation,
const std::string& country,
size_t cache_size); size_t cache_size);
~URLChecker(); ~URLChecker();
...@@ -72,6 +76,7 @@ class URLChecker { ...@@ -72,6 +76,7 @@ class URLChecker {
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
const net::NetworkTrafficAnnotationTag traffic_annotation_; const net::NetworkTrafficAnnotationTag traffic_annotation_;
const std::string country_;
CheckList checks_in_progress_; CheckList checks_in_progress_;
......
...@@ -75,6 +75,7 @@ class SafeSearchURLCheckerTest : public testing::Test { ...@@ -75,6 +75,7 @@ class SafeSearchURLCheckerTest : public testing::Test {
&test_url_loader_factory_)), &test_url_loader_factory_)),
checker_(test_shared_loader_factory_, checker_(test_shared_loader_factory_,
TRAFFIC_ANNOTATION_FOR_TESTS, TRAFFIC_ANNOTATION_FOR_TESTS,
"us",
kCacheSize) {} kCacheSize) {}
MOCK_METHOD3(OnCheckDone, MOCK_METHOD3(OnCheckDone,
......
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