OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/extensions/blacklist_fetcher.h" | |
6 | |
7 #include "base/strings/stringprintf.h" | |
8 #include "chrome/browser/browser_process.h" | |
9 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" | |
10 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
11 #include "chrome/common/safe_browsing/crx_info.pb.h" | |
12 #include "google_apis/google_api_keys.h" | |
13 #include "net/base/escape.h" | |
14 #include "net/url_request/url_request_status.h" | |
15 | |
16 using content::BrowserThread; | |
17 | |
18 namespace extensions { | |
19 | |
20 BlacklistFetcher::BlacklistFetcher() | |
21 : safe_browsing_config_initialized_(false), | |
22 url_fetcher_id_(0) { | |
23 } | |
24 | |
25 void BlacklistFetcher::Request(const std::string& id, | |
26 const RequestCallback& callback) { | |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
28 if (!safe_browsing_config_initialized_) { | |
29 if (g_browser_process && g_browser_process->safe_browsing_service()) { | |
30 SetSafeBrowsingConfig( | |
31 g_browser_process->safe_browsing_service()->GetProtocolConfig()); | |
32 } else { | |
33 base::MessageLoopProxy::current()->PostTask( | |
34 FROM_HERE, base::Bind(callback, Blacklist::NOT_BLACKLISTED)); | |
not at google - send to devlin
2013/11/18 17:03:49
are you sure you shouldn't wait until its initiali
Oleg Eterevsky
2013/11/20 12:58:45
I was under impression that if safe_browsing_servi
not at google - send to devlin
2013/11/20 23:44:48
Yes you're right, I misread this a little bit, sor
| |
35 return; | |
36 } | |
37 } | |
38 | |
39 bool request_already_sent = callbacks_.find(id) != callbacks_.end(); | |
not at google - send to devlin
2013/11/18 17:03:49
likewise since you're not using the iterator resul
Oleg Eterevsky
2013/11/20 12:58:45
Ditto.
| |
40 callbacks_.insert(std::make_pair(id, callback)); | |
41 if (request_already_sent) | |
42 return; | |
43 | |
44 ClientCRXListInfoRequest request; | |
45 | |
46 request.set_id(id); | |
47 std::string request_str; | |
48 request.SerializeToString(&request_str); | |
49 | |
50 GURL request_url = RequestUrl(); | |
not at google - send to devlin
2013/11/18 17:03:49
inline this?
Oleg Eterevsky
2013/11/20 12:58:45
It looks more readable like this, and the compiler
not at google - send to devlin
2013/11/20 23:44:48
It's not about inlining it's about readability, so
| |
51 net::URLFetcher* fetcher = net::URLFetcher::Create(url_fetcher_id_++, | |
52 request_url, | |
53 net::URLFetcher::POST, | |
54 this); | |
55 requests_[fetcher] = id; | |
56 fetcher->SetAutomaticallyRetryOn5xx(false); // Don't retry on error. | |
57 fetcher->SetUploadData("application/octet-stream", request_str); | |
58 fetcher->Start(); | |
59 } | |
60 | |
61 void BlacklistFetcher::SetSafeBrowsingConfig( | |
62 const SafeBrowsingProtocolConfig& config) { | |
63 safe_browsing_config_ = config; | |
64 safe_browsing_config_initialized_ = true; | |
65 } | |
66 | |
67 GURL BlacklistFetcher::RequestUrl() const { | |
68 std::string url = base::StringPrintf( | |
69 "%s/%s?client=%s&appver=%s&pver=2.2", | |
70 safe_browsing_config_.url_prefix.c_str(), | |
71 "clientreport/crx-list-info", | |
72 safe_browsing_config_.client_name.c_str(), | |
73 safe_browsing_config_.version.c_str()); | |
74 std::string api_key = google_apis::GetAPIKey(); | |
75 if (!api_key.empty()) { | |
76 base::StringAppendF(&url, "&key=%s", | |
77 net::EscapeQueryParamValue(api_key, true).c_str()); | |
78 } | |
79 return GURL(url); | |
80 } | |
81 | |
82 void BlacklistFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
84 | |
85 std::map<const net::URLFetcher*, std::string>::iterator it = | |
86 requests_.find(source); | |
87 if (it == requests_.end()) | |
88 return; | |
89 | |
90 scoped_ptr<const net::URLFetcher> fetcher; | |
91 | |
92 fetcher.reset(it->first); | |
93 std::string id = it->second; | |
94 requests_.erase(it); | |
95 | |
96 Blacklist::BlacklistState state; | |
97 | |
98 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { | |
99 std::string data; | |
100 source->GetResponseAsString(&data); | |
101 ClientCRXListInfoResponse response; | |
102 if (response.ParseFromString(data)) { | |
103 state = static_cast<Blacklist::BlacklistState>(response.verdict()); | |
104 } else { | |
105 state = Blacklist::NOT_BLACKLISTED; | |
106 } | |
107 } else { | |
108 if (source->GetStatus().status() == net::URLRequestStatus::FAILED) { | |
109 VLOG(1) << "Blacklist request for: " << id | |
110 << " failed with error: " << source->GetStatus().error(); | |
111 } else { | |
112 VLOG(1) << "Blacklist request for: " << id | |
113 << " failed with error: " << source->GetResponseCode(); | |
114 } | |
115 | |
116 state = Blacklist::BLACKLISTED_UNKNOWN; | |
117 } | |
118 | |
119 std::pair<CallbackMultiMap::iterator, CallbackMultiMap::iterator> range = | |
120 callbacks_.equal_range(id); | |
121 for (CallbackMultiMap::const_iterator callback_it = range.first; | |
122 callback_it != range.second; | |
123 ++callback_it) { | |
124 callback_it->second.Run(state); | |
125 } | |
126 | |
127 callbacks_.erase(range.first, range.second); | |
128 } | |
129 | |
130 } // namespace extensions | |
131 | |
OLD | NEW |