summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/safe_browsing_database.cc
blob: f978200cdef1e4a0648e73b3f174243c8df4509e (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
// Copyright (c) 2006-2008 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.

#include "chrome/browser/safe_browsing/safe_browsing_database.h"

#include "base/file_util.h"
#include "base/logging.h"
#include "base/sha2.h"
#include "chrome/browser/safe_browsing/safe_browsing_database_impl.h"
#include "googleurl/src/gurl.h"

// Filename suffix for the bloom filter.
static const wchar_t kBloomFilterFile[] = L" Filter";

// Factory method.
SafeBrowsingDatabase* SafeBrowsingDatabase::Create() {
  return new SafeBrowsingDatabaseImpl;
}

bool SafeBrowsingDatabase::NeedToCheckUrl(const GURL& url) {
  if (!bloom_filter_.get())
    return true;

  IncrementBloomFilterReadCount();

  std::vector<std::string> hosts;
  safe_browsing_util::GenerateHostsToCheck(url, &hosts);
  if (hosts.size() == 0)
    return false;  // Could be about:blank.

  SBPrefix host_key;
  if (url.HostIsIPAddress()) {
    base::SHA256HashString(url.host() + "/", &host_key, sizeof(SBPrefix));
    if (bloom_filter_->Exists(host_key))
      return true;
  } else {
    base::SHA256HashString(hosts[0] + "/", &host_key, sizeof(SBPrefix));
    if (bloom_filter_->Exists(host_key))
      return true;

    if (hosts.size() > 1) {
      base::SHA256HashString(hosts[1] + "/", &host_key, sizeof(SBPrefix));
      if (bloom_filter_->Exists(host_key))
        return true;
    }
  }
  return false;
}

std::wstring SafeBrowsingDatabase::BloomFilterFilename(
    const std::wstring& db_filename) {
  return db_filename + kBloomFilterFile;
}

void SafeBrowsingDatabase::LoadBloomFilter() {
  DCHECK(!bloom_filter_filename_.empty());

  int64 size_64;
  if (!file_util::GetFileSize(bloom_filter_filename_, &size_64) ||
      size_64 == 0) {
    BuildBloomFilter();
    return;
  }

  int size = static_cast<int>(size_64);
  char* data = new char[size];
  CHECK(data);

  Time before = Time::Now();
  file_util::ReadFile(bloom_filter_filename_, data, size);
  SB_DLOG(INFO) << "SafeBrowsingDatabase read bloom filter in " <<
        (Time::Now() - before).InMilliseconds() << " ms";

  bloom_filter_.reset(new BloomFilter(data, size));
}

void SafeBrowsingDatabase::DeleteBloomFilter() {
  file_util::Delete(bloom_filter_filename_, false);
}

void SafeBrowsingDatabase::WriteBloomFilter() {
  if (!bloom_filter_.get())
    return;

  Time before = Time::Now();
  file_util::WriteFile(bloom_filter_filename_,
                       bloom_filter_->data(),
                       bloom_filter_->size());
  SB_DLOG(INFO) << "SafeBrowsingDatabase wrote bloom filter in " <<
      (Time::Now() - before).InMilliseconds() << " ms";
}