summaryrefslogtreecommitdiff
path: root/chromium/components/subresource_filter/content/browser/subresource_filter_safe_browsing_client_request.h
blob: 2560050d9438eb99dfca04729979d928679ac7b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BROWSING_CLIENT_REQUEST_H_
#define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BROWSING_CLIENT_REQUEST_H_

#include <stddef.h>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/safe_browsing_db/database_manager.h"
#include "components/safe_browsing_db/util.h"
#include "url/gurl.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace safe_browsing {
struct ThreatMetadata;
}  // namespace safe_browsing

namespace subresource_filter {

class SubresourceFilterSafeBrowsingClient;

// This class is scoped to a single database check, and it lives on the IO
// thread exclusively.
class SubresourceFilterSafeBrowsingClientRequest
    : public safe_browsing::SafeBrowsingDatabaseManager::Client {
 public:
  SubresourceFilterSafeBrowsingClientRequest(
      const GURL& url,
      size_t request_id,
      scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
          database_manager,
      scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
      SubresourceFilterSafeBrowsingClient* client);
  ~SubresourceFilterSafeBrowsingClientRequest() override;

  void Start();

  // safe_browsing::SafeBrowsingDatabaseManager::Client:
  void OnCheckBrowseUrlResult(
      const GURL& url,
      safe_browsing::SBThreatType threat_type,
      const safe_browsing::ThreatMetadata& metadata) override;

  const GURL& url() const { return url_; }
  size_t request_id() const { return request_id_; }

  // Maximum time in milliseconds to wait for the Safe Browsing service to
  // verify a URL. After this amount of time the outstanding check will be
  // aborted, and the URL will be treated as if it didn't belong to the
  // Subresource Filter only list.
  static constexpr base::TimeDelta kCheckURLTimeout =
      base::TimeDelta::FromSeconds(5);

 private:
  // Callback for when the safe browsing check has taken longer than
  // kCheckURLTimeout.
  void OnCheckUrlTimeout();

  void SendCheckResultToClient(bool served_from_network,
                               safe_browsing::SBThreatType threat_type,
                               const safe_browsing::ThreatMetadata& metadata);

  const GURL url_;

  // The |request_id_| identifies a particular request, as issued from the
  // SubresourceFilterSafeBrowsingClient. It will be unique in the scope of a
  // single navigation (i.e. the scope of the
  // SubresourceFilterSafeBrowsingClient).
  const size_t request_id_;

  scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_;
  SubresourceFilterSafeBrowsingClient* client_ = nullptr;

  // Timer to abort the safe browsing check if it takes too long.
  base::OneShotTimer timer_;

  base::TimeTicks start_time_;

  bool request_completed_ = false;

  DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClientRequest);
};

}  // namespace subresource_filter

#endif  // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BROWSING_CLIENT_REQUEST_H_