OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/safe_browsing/remote_database_manager.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "chrome/browser/safe_browsing/android_safe_browsing_api_handler.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 |
| 12 using content::BrowserThread; |
| 13 |
| 14 // |
| 15 // RemoteSafeBrowsingDatabaseManager::ClientRequest methods |
| 16 // |
| 17 class RemoteSafeBrowsingDatabaseManager::ClientRequest { |
| 18 public: |
| 19 ClientRequest(Client* client, |
| 20 RemoteSafeBrowsingDatabaseManager* db_manager, |
| 21 const GURL& url); |
| 22 |
| 23 static void OnRequestDoneWeak(const base::WeakPtr<ClientRequest>& req, |
| 24 SBThreatType matched_threat_type, |
| 25 const std::string& metadata); |
| 26 void OnRequestDone(SBThreatType matched_threat_type, |
| 27 const std::string& metadata); |
| 28 |
| 29 // Accessors |
| 30 Client* client() const { return client_; } |
| 31 const GURL& url() const { return url_; } |
| 32 base::WeakPtr<ClientRequest> GetWeakPtr() { |
| 33 return weak_factory_.GetWeakPtr(); |
| 34 } |
| 35 |
| 36 private: |
| 37 Client* client_; |
| 38 RemoteSafeBrowsingDatabaseManager* db_manager_; |
| 39 GURL url_; |
| 40 base::WeakPtrFactory<ClientRequest> weak_factory_; |
| 41 }; |
| 42 |
| 43 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest( |
| 44 Client* client, |
| 45 RemoteSafeBrowsingDatabaseManager* db_manager, |
| 46 const GURL& url) |
| 47 : client_(client), db_manager_(db_manager), url_(url), weak_factory_(this) { |
| 48 } |
| 49 |
| 50 // Static |
| 51 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDoneWeak( |
| 52 const base::WeakPtr<ClientRequest>& req, |
| 53 SBThreatType matched_threat_type, |
| 54 const std::string& metadata) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 56 if (!req) |
| 57 return; // Previously canceled |
| 58 req->OnRequestDone(matched_threat_type, metadata); |
| 59 } |
| 60 |
| 61 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone( |
| 62 SBThreatType matched_threat_type, |
| 63 const std::string& metadata) { |
| 64 VLOG(1) << "OnRequestDone for client " << client_ << " and URL " << url_; |
| 65 client_->OnCheckBrowseUrlResult(url_, matched_threat_type, metadata); |
| 66 db_manager_->CancelCheck(client_); |
| 67 } |
| 68 |
| 69 // |
| 70 // RemoteSafeBrowsingDatabaseManager methods |
| 71 // |
| 72 |
| 73 // TODO(nparker): Add tests for this class once implemented. |
| 74 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager() |
| 75 : enabled_(false) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 77 // TODO(nparker): Implement the initialization glue. |
| 78 // Check flag to see if this is enabled. |
| 79 // Ask AndroidSafeBrowsingAPIHandler if Java API is correct version. |
| 80 } |
| 81 |
| 82 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() { |
| 83 DCHECK(!enabled_); |
| 84 } |
| 85 |
| 86 bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { |
| 87 return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) || |
| 88 url.SchemeIs(url::kFtpScheme); |
| 89 } |
| 90 |
| 91 bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled() |
| 92 const { |
| 93 return false; |
| 94 } |
| 95 |
| 96 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl( |
| 97 const std::vector<GURL>& url_chain, |
| 98 Client* client) { |
| 99 NOTREACHED(); |
| 100 return true; |
| 101 } |
| 102 |
| 103 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs( |
| 104 const std::set<std::string>& extension_ids, |
| 105 Client* client) { |
| 106 NOTREACHED(); |
| 107 return true; |
| 108 } |
| 109 |
| 110 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP( |
| 111 const std::string& ip_address) { |
| 112 NOTREACHED(); |
| 113 return false; |
| 114 } |
| 115 |
| 116 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { |
| 117 NOTREACHED(); |
| 118 return true; |
| 119 } |
| 120 |
| 121 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl( |
| 122 const GURL& url) { |
| 123 NOTREACHED(); |
| 124 return true; |
| 125 } |
| 126 |
| 127 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString( |
| 128 const std::string& str) { |
| 129 NOTREACHED(); |
| 130 return true; |
| 131 } |
| 132 |
| 133 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl( |
| 134 const GURL& url) { |
| 135 NOTREACHED(); |
| 136 return true; |
| 137 } |
| 138 |
| 139 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() { |
| 140 NOTREACHED(); |
| 141 return true; |
| 142 } |
| 143 |
| 144 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { |
| 145 NOTREACHED(); |
| 146 return true; |
| 147 } |
| 148 |
| 149 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, |
| 150 Client* client) { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 152 if (!enabled_) |
| 153 return true; |
| 154 |
| 155 if (!CanCheckUrl(url)) |
| 156 return true; // Safe, continue right away. |
| 157 |
| 158 scoped_ptr<ClientRequest> req(new ClientRequest(client, this, url)); |
| 159 std::vector<SBThreatType> threat_types; |
| 160 threat_types.push_back(SB_THREAT_TYPE_URL_MALWARE); |
| 161 |
| 162 VLOG(1) << "Checking for client " << client << " and URL " << url; |
| 163 bool started = api_handler_.StartURLCheck( |
| 164 base::Bind(&ClientRequest::OnRequestDoneWeak, req->GetWeakPtr()), url, |
| 165 threat_types); |
| 166 if (!started) { |
| 167 LOG(DFATAL) << "Failed to start Safe Browsing request"; |
| 168 return true; |
| 169 } |
| 170 |
| 171 current_requests_.push_back(req.release()); |
| 172 |
| 173 // Defer the resource load. |
| 174 return false; |
| 175 } |
| 176 |
| 177 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client* client) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 DCHECK(enabled_); |
| 180 for (auto itr = current_requests_.begin(); itr != current_requests_.end(); |
| 181 ++itr) { |
| 182 if ((*itr)->client() == client) { |
| 183 VLOG(1) << "Canceling check for URL " << (*itr)->url(); |
| 184 delete *itr; |
| 185 current_requests_.erase(itr); |
| 186 return; |
| 187 } |
| 188 } |
| 189 NOTREACHED(); |
| 190 } |
| 191 |
| 192 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() { |
| 193 VLOG(1) << "RemoteSafeBrowsing starting"; |
| 194 enabled_ = true; |
| 195 } |
| 196 |
| 197 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { |
| 198 // |shutdown| is not used. |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 200 VLOG(1) << "RemoteSafeBrowsing stopping"; |
| 201 |
| 202 // Call back and delete any remaining clients. OnRequestDone() modifies |
| 203 // |current_requests_|, so we make a copy first. |
| 204 std::vector<ClientRequest*> to_callback(current_requests_); |
| 205 for (auto req : to_callback) { |
| 206 VLOG(1) << "Stopping: Invoking unfinished req for URL " << req->url(); |
| 207 req->OnRequestDone(SB_THREAT_TYPE_SAFE, std::string()); |
| 208 } |
| 209 enabled_ = false; |
| 210 } |
| 211 |
OLD | NEW |