1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/supervised_user/kids_management_url_checker_client.h"
6 
7 #include <utility>
8 
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/supervised_user/kids_chrome_management/kids_chrome_management_client_factory.h"
14 #include "url/gurl.h"
15 
16 namespace {
17 
18 using kids_chrome_management::ClassifyUrlResponse;
19 
ToSafeSearchClientClassification(ClassifyUrlResponse * classify_url_response)20 safe_search_api::ClientClassification ToSafeSearchClientClassification(
21     ClassifyUrlResponse* classify_url_response) {
22   switch (classify_url_response->display_classification()) {
23     case ClassifyUrlResponse::UNKNOWN_DISPLAY_CLASSIFICATION:
24       return safe_search_api::ClientClassification::kUnknown;
25     case ClassifyUrlResponse::RESTRICTED:
26       return safe_search_api::ClientClassification::kRestricted;
27     case ClassifyUrlResponse::ALLOWED:
28       return safe_search_api::ClientClassification::kAllowed;
29   }
30 }
31 
32 }  // namespace
33 
KidsManagementURLCheckerClient(const std::string & country)34 KidsManagementURLCheckerClient::KidsManagementURLCheckerClient(
35     const std::string& country)
36     : country_(country) {}
37 
38 KidsManagementURLCheckerClient::~KidsManagementURLCheckerClient() = default;
39 
CheckURL(const GURL & url,ClientCheckCallback callback)40 void KidsManagementURLCheckerClient::CheckURL(const GURL& url,
41                                               ClientCheckCallback callback) {
42   auto classify_url_request =
43       std::make_unique<kids_chrome_management::ClassifyUrlRequest>();
44   classify_url_request->set_url(url.spec());
45   classify_url_request->set_region_code(country_);
46 
47   KidsChromeManagementClient* kids_chrome_management_client =
48       KidsChromeManagementClientFactory::GetInstance()->GetForBrowserContext(
49           ProfileManager::GetActiveUserProfile());
50 
51   kids_chrome_management_client->ClassifyURL(
52       std::move(classify_url_request),
53       base::BindOnce(&KidsManagementURLCheckerClient::ConvertResponseCallback,
54                      base::Unretained(this), url, std::move(callback)));
55 }
56 
ConvertResponseCallback(const GURL & url,ClientCheckCallback client_callback,std::unique_ptr<google::protobuf::MessageLite> response_proto,KidsChromeManagementClient::ErrorCode error_code)57 void KidsManagementURLCheckerClient::ConvertResponseCallback(
58     const GURL& url,
59     ClientCheckCallback client_callback,
60     std::unique_ptr<google::protobuf::MessageLite> response_proto,
61     KidsChromeManagementClient::ErrorCode error_code) {
62   ClassifyUrlResponse* classify_url_response =
63       static_cast<ClassifyUrlResponse*>(response_proto.get());
64 
65   DVLOG(1) << "URL classification = "
66            << classify_url_response->display_classification();
67 
68   if (error_code != KidsChromeManagementClient::ErrorCode::kSuccess) {
69     DVLOG(1) << "ClassifyUrl request failed.";
70     std::move(client_callback)
71         .Run(url, safe_search_api::ClientClassification::kUnknown);
72     return;
73   }
74 
75   std::move(client_callback)
76       .Run(url, ToSafeSearchClientClassification(classify_url_response));
77 }
78