Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: components/safe_browsing_db/database_manager.cc

Issue 1890753002: SafeBrowsing: Track and cancel API checks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-impl-2
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/safe_browsing_db/database_manager.h" 5 #include "components/safe_browsing_db/database_manager.h"
6 6
7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
8 #include "content/public/browser/browser_thread.h" 8 #include "content/public/browser/browser_thread.h"
9 #include "net/url_request/url_request_context_getter.h" 9 #include "net/url_request/url_request_context_getter.h"
10 #include "url/gurl.h" 10 #include "url/gurl.h"
(...skipping 22 matching lines...) Expand all
33 // |shutdown| not used. Destroys the v4 protocol managers. This may be called 33 // |shutdown| not used. Destroys the v4 protocol managers. This may be called
34 // multiple times during the life of the DatabaseManager. 34 // multiple times during the life of the DatabaseManager.
35 // Must be called on IO thread. 35 // Must be called on IO thread.
36 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { 36 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) {
37 DCHECK_CURRENTLY_ON(BrowserThread::IO); 37 DCHECK_CURRENTLY_ON(BrowserThread::IO);
38 // This cancels all in-flight GetHash requests. 38 // This cancels all in-flight GetHash requests.
39 if (v4_get_hash_protocol_manager_) { 39 if (v4_get_hash_protocol_manager_) {
40 delete v4_get_hash_protocol_manager_; 40 delete v4_get_hash_protocol_manager_;
41 v4_get_hash_protocol_manager_ = NULL; 41 v4_get_hash_protocol_manager_ = NULL;
42 } 42 }
43 // TODO(kcarattini): Call back clients with pending requests. 43
44 // Delete pending checks, calling back any clients with empty metadata.
45 for (CurrentApiChecks::iterator it = api_checks_.begin();
Nathan Parker 2016/04/15 23:27:35 for (auto itr : api_checks_) ?
kcarattini 2016/04/18 03:00:09 Done.
46 it != api_checks_.end(); ++it) {
47 std::shared_ptr<SafeBrowsingApiCheck> check = *it;
48 if (check->client()) {
49 check->client()->
50 OnCheckApiBlacklistUrlResult(check->url(), ThreatMetadata());
51 }
52 }
53 api_checks_.clear();
54 }
55
56 bool SafeBrowsingDatabaseManager::CancelApiCheck(Client* client) {
57 DCHECK_CURRENTLY_ON(BrowserThread::IO);
58 for (CurrentApiChecks::iterator it = api_checks_.begin();
59 it != api_checks_.end(); ++it) {
60 if ((*it)->client() == client) {
61 api_checks_.erase(it);
62 return true;
63 }
64 }
65 return false;
Nathan Parker 2016/04/15 23:27:35 Add NOTREACHED()
kcarattini 2016/04/18 03:00:09 Done.
44 } 66 }
45 67
46 bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, 68 bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url,
47 Client* client) { 69 Client* client) {
48 DCHECK_CURRENTLY_ON(BrowserThread::IO); 70 DCHECK_CURRENTLY_ON(BrowserThread::IO);
49 DCHECK(v4_get_hash_protocol_manager_); 71 DCHECK(v4_get_hash_protocol_manager_);
50 72
51 // Make sure we can check this url. 73 // Make sure we can check this url.
52 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) { 74 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) {
53 return true; 75 return true;
54 } 76 }
55 77
56 // Compute a list of hashes for this url. 78 // Compute a list of hashes for this url.
57 std::vector<SBFullHash> full_hashes; 79 std::vector<SBFullHash> full_hashes;
58 UrlToFullHashes(url, false, &full_hashes); 80 UrlToFullHashes(url, false, &full_hashes);
59 if (full_hashes.empty()) 81 if (full_hashes.empty())
60 return true; 82 return true;
61 83
62 // Copy to prefixes. 84 // Copy to prefixes.
63 std::vector<SBPrefix> prefixes; 85 std::vector<SBPrefix> prefixes;
64 for (const SBFullHash& full_hash : full_hashes) { 86 for (const SBFullHash& full_hash : full_hashes) {
65 prefixes.push_back(full_hash.prefix); 87 prefixes.push_back(full_hash.prefix);
66 } 88 }
67 // Multiple full hashes could share a prefix, remove duplicates. 89 // Multiple full hashes could share a prefix, remove duplicates.
68 std::sort(prefixes.begin(), prefixes.end()); 90 std::sort(prefixes.begin(), prefixes.end());
69 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); 91 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end());
70 DCHECK(!prefixes.empty()); 92 DCHECK(!prefixes.empty());
71 93
72 // TODO(kcarattini): Track checks in a map. 94 // TODO(kcarattini): Merge multiple checks for the same full_hashes? What
95 // about pages that request two permissions?
73 std::shared_ptr<SafeBrowsingApiCheck> check( 96 std::shared_ptr<SafeBrowsingApiCheck> check(
74 new SafeBrowsingApiCheck(url, full_hashes, client)); 97 new SafeBrowsingApiCheck(url, full_hashes, client));
98 api_checks_.insert(check);
Nathan Parker 2016/04/15 23:27:35 I _think_ this could be a scoped_ptr if you just a
kcarattini 2016/04/18 03:00:09 The local and remote managers both store the raw p
Nathan Parker 2016/04/18 17:29:58 The raw ptr is probably fine since that continues
75 99
76 // TODO(kcarattini): Implement cache compliance. 100 // TODO(kcarattini): Implement cache compliance.
77 v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes, 101 v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes,
78 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults, 102 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults,
79 base::Unretained(this), check)); 103 base::Unretained(this), check));
80 104
81 return false; 105 return false;
82 } 106 }
83 107
84 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( 108 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults(
85 std::shared_ptr<SafeBrowsingApiCheck> check, 109 std::shared_ptr<SafeBrowsingApiCheck> check,
86 const std::vector<SBFullHashResult>& full_hash_results, 110 const std::vector<SBFullHashResult>& full_hash_results,
87 const base::TimeDelta& negative_cache_duration) { 111 const base::TimeDelta& negative_cache_duration) {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO); 112 DCHECK_CURRENTLY_ON(BrowserThread::IO);
89 DCHECK(check); 113 DCHECK(check);
90 114
115 // If the check is not in |api_checks_| then the request was cancelled by the
116 // client.
117 CurrentApiChecks::iterator it = api_checks_.find(check);
118 if (it == api_checks_.end())
119 return;
120
91 ThreatMetadata md; 121 ThreatMetadata md;
92 // Merge the metadata from all matching results. 122 // Merge the metadata from all matching results.
93 for (const SBFullHashResult& result : full_hash_results) { 123 for (const SBFullHashResult& result : full_hash_results) {
94 for (const SBFullHash& full_hash : check->full_hashes()) { 124 for (const SBFullHash& full_hash : check->full_hashes()) {
95 if (SBFullHashEqual(full_hash, result.hash)) { 125 if (SBFullHashEqual(full_hash, result.hash)) {
96 md.api_permissions.insert(md.api_permissions.end(), 126 md.api_permissions.insert(md.api_permissions.end(),
97 result.metadata.api_permissions.begin(), 127 result.metadata.api_permissions.begin(),
98 result.metadata.api_permissions.end()); 128 result.metadata.api_permissions.end());
99 break; 129 break;
100 } 130 }
101 } 131 }
102 } 132 }
103 133
104 check->client()->OnCheckApiBlacklistUrlResult(check->url(), md); 134 check->client()->OnCheckApiBlacklistUrlResult(check->url(), md);
135 api_checks_.erase(it);
105 } 136 }
106 137
107 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck( 138 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck(
108 const GURL& url, const std::vector<SBFullHash>& full_hashes, Client* client) 139 const GURL& url, const std::vector<SBFullHash>& full_hashes, Client* client)
109 : url_(url), full_hashes_(full_hashes), client_(client) { 140 : url_(url), full_hashes_(full_hashes), client_(client) {
110 } 141 }
111 142
112 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() { 143 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() {
113 } 144 }
114 145
115 } // namespace safe_browsing 146 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698