OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/blacklist_state_fetcher.h" | 5 #include "chrome/browser/extensions/blacklist_state_fetcher.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" | 10 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" |
11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
12 #include "chrome/common/safe_browsing/crx_info.pb.h" | 12 #include "chrome/common/safe_browsing/crx_info.pb.h" |
13 #include "google_apis/google_api_keys.h" | 13 #include "google_apis/google_api_keys.h" |
14 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
15 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
16 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 using content::BrowserThread; | 20 using content::BrowserThread; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class BlacklistRequestContextGetter : public net::URLRequestContextGetter { | 24 class BlacklistRequestContextGetter : public net::URLRequestContextGetter { |
25 public: | 25 public: |
26 explicit BlacklistRequestContextGetter( | 26 explicit BlacklistRequestContextGetter( |
27 net::URLRequestContextGetter* parent_context_getter) : | 27 net::URLRequestContextGetter* parent_context_getter) : |
28 network_task_runner_( | 28 network_task_runner_( |
29 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { | 29 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { |
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
31 url_request_context_.reset(new net::URLRequestContext()); | 31 url_request_context_.reset(new net::URLRequestContext()); |
32 url_request_context_->CopyFrom( | 32 url_request_context_->CopyFrom( |
33 parent_context_getter->GetURLRequestContext()); | 33 parent_context_getter->GetURLRequestContext()); |
34 } | 34 } |
35 | 35 |
36 static void Create( | 36 static void Create( |
37 scoped_refptr<net::URLRequestContextGetter> parent_context_getter, | 37 scoped_refptr<net::URLRequestContextGetter> parent_context_getter, |
38 base::Callback<void(scoped_refptr<net::URLRequestContextGetter>)> | 38 base::Callback<void(scoped_refptr<net::URLRequestContextGetter>)> |
39 callback) { | 39 callback) { |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
41 | 41 |
42 scoped_refptr<net::URLRequestContextGetter> context_getter = | 42 scoped_refptr<net::URLRequestContextGetter> context_getter = |
43 new BlacklistRequestContextGetter(parent_context_getter); | 43 new BlacklistRequestContextGetter(parent_context_getter); |
44 BrowserThread::PostTask(BrowserThread::UI, | 44 BrowserThread::PostTask(BrowserThread::UI, |
45 FROM_HERE, | 45 FROM_HERE, |
46 base::Bind(callback, context_getter)); | 46 base::Bind(callback, context_getter)); |
47 } | 47 } |
48 | 48 |
49 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { | 49 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
51 return url_request_context_.get(); | 51 return url_request_context_.get(); |
52 } | 52 } |
53 | 53 |
54 virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | 54 virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
55 const OVERRIDE { | 55 const OVERRIDE { |
56 return network_task_runner_; | 56 return network_task_runner_; |
57 } | 57 } |
58 | 58 |
59 protected: | 59 protected: |
60 virtual ~BlacklistRequestContextGetter() { | 60 virtual ~BlacklistRequestContextGetter() { |
61 url_request_context_->AssertNoURLRequests(); | 61 url_request_context_->AssertNoURLRequests(); |
62 } | 62 } |
63 | 63 |
64 private: | 64 private: |
65 scoped_ptr<net::URLRequestContext> url_request_context_; | 65 scoped_ptr<net::URLRequestContext> url_request_context_; |
66 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 66 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
67 }; | 67 }; |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 namespace extensions { | 71 namespace extensions { |
72 | 72 |
73 BlacklistStateFetcher::BlacklistStateFetcher() | 73 BlacklistStateFetcher::BlacklistStateFetcher() |
74 : url_fetcher_id_(0), | 74 : url_fetcher_id_(0), |
75 weak_ptr_factory_(this) {} | 75 weak_ptr_factory_(this) {} |
76 | 76 |
77 BlacklistStateFetcher::~BlacklistStateFetcher() { | 77 BlacklistStateFetcher::~BlacklistStateFetcher() { |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
79 STLDeleteContainerPairFirstPointers(requests_.begin(), requests_.end()); | 79 STLDeleteContainerPairFirstPointers(requests_.begin(), requests_.end()); |
80 requests_.clear(); | 80 requests_.clear(); |
81 } | 81 } |
82 | 82 |
83 void BlacklistStateFetcher::Request(const std::string& id, | 83 void BlacklistStateFetcher::Request(const std::string& id, |
84 const RequestCallback& callback) { | 84 const RequestCallback& callback) { |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
86 if (!safe_browsing_config_) { | 86 if (!safe_browsing_config_) { |
87 if (g_browser_process && g_browser_process->safe_browsing_service()) { | 87 if (g_browser_process && g_browser_process->safe_browsing_service()) { |
88 SetSafeBrowsingConfig( | 88 SetSafeBrowsingConfig( |
89 g_browser_process->safe_browsing_service()->GetProtocolConfig()); | 89 g_browser_process->safe_browsing_service()->GetProtocolConfig()); |
90 } else { | 90 } else { |
91 base::MessageLoopProxy::current()->PostTask( | 91 base::MessageLoopProxy::current()->PostTask( |
92 FROM_HERE, base::Bind(callback, BLACKLISTED_UNKNOWN)); | 92 FROM_HERE, base::Bind(callback, BLACKLISTED_UNKNOWN)); |
93 return; | 93 return; |
94 } | 94 } |
95 } | 95 } |
(...skipping 21 matching lines...) Expand all Loading... |
117 parent_request_context, | 117 parent_request_context, |
118 base::Bind(&BlacklistStateFetcher::SaveRequestContext, | 118 base::Bind(&BlacklistStateFetcher::SaveRequestContext, |
119 weak_ptr_factory_.GetWeakPtr(), | 119 weak_ptr_factory_.GetWeakPtr(), |
120 id))); | 120 id))); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 void BlacklistStateFetcher::SaveRequestContext( | 124 void BlacklistStateFetcher::SaveRequestContext( |
125 const std::string& id, | 125 const std::string& id, |
126 scoped_refptr<net::URLRequestContextGetter> request_context_getter) { | 126 scoped_refptr<net::URLRequestContextGetter> request_context_getter) { |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
128 if (!url_request_context_getter_) | 128 if (!url_request_context_getter_) |
129 url_request_context_getter_ = request_context_getter; | 129 url_request_context_getter_ = request_context_getter; |
130 SendRequest(id); | 130 SendRequest(id); |
131 } | 131 } |
132 | 132 |
133 void BlacklistStateFetcher::SendRequest(const std::string& id) { | 133 void BlacklistStateFetcher::SendRequest(const std::string& id) { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
135 | 135 |
136 ClientCRXListInfoRequest request; | 136 ClientCRXListInfoRequest request; |
137 request.set_id(id); | 137 request.set_id(id); |
138 std::string request_str; | 138 std::string request_str; |
139 request.SerializeToString(&request_str); | 139 request.SerializeToString(&request_str); |
140 | 140 |
141 GURL request_url = RequestUrl(); | 141 GURL request_url = RequestUrl(); |
142 net::URLFetcher* fetcher = net::URLFetcher::Create(url_fetcher_id_++, | 142 net::URLFetcher* fetcher = net::URLFetcher::Create(url_fetcher_id_++, |
143 request_url, | 143 request_url, |
144 net::URLFetcher::POST, | 144 net::URLFetcher::POST, |
(...skipping 24 matching lines...) Expand all Loading... |
169 safe_browsing_config_->version.c_str()); | 169 safe_browsing_config_->version.c_str()); |
170 std::string api_key = google_apis::GetAPIKey(); | 170 std::string api_key = google_apis::GetAPIKey(); |
171 if (!api_key.empty()) { | 171 if (!api_key.empty()) { |
172 base::StringAppendF(&url, "&key=%s", | 172 base::StringAppendF(&url, "&key=%s", |
173 net::EscapeQueryParamValue(api_key, true).c_str()); | 173 net::EscapeQueryParamValue(api_key, true).c_str()); |
174 } | 174 } |
175 return GURL(url); | 175 return GURL(url); |
176 } | 176 } |
177 | 177 |
178 void BlacklistStateFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 178 void BlacklistStateFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
180 | 180 |
181 std::map<const net::URLFetcher*, std::string>::iterator it = | 181 std::map<const net::URLFetcher*, std::string>::iterator it = |
182 requests_.find(source); | 182 requests_.find(source); |
183 if (it == requests_.end()) { | 183 if (it == requests_.end()) { |
184 NOTREACHED(); | 184 NOTREACHED(); |
185 return; | 185 return; |
186 } | 186 } |
187 | 187 |
188 scoped_ptr<const net::URLFetcher> fetcher; | 188 scoped_ptr<const net::URLFetcher> fetcher; |
189 | 189 |
(...skipping 30 matching lines...) Expand all Loading... |
220 callback_it != range.second; | 220 callback_it != range.second; |
221 ++callback_it) { | 221 ++callback_it) { |
222 callback_it->second.Run(state); | 222 callback_it->second.Run(state); |
223 } | 223 } |
224 | 224 |
225 callbacks_.erase(range.first, range.second); | 225 callbacks_.erase(range.first, range.second); |
226 } | 226 } |
227 | 227 |
228 } // namespace extensions | 228 } // namespace extensions |
229 | 229 |
OLD | NEW |