| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Library functions related to the Financial Server ping. | 5 // Library functions related to the Financial Server ping. |
| 6 | 6 |
| 7 #include "rlz/lib/financial_ping.h" | 7 #include "rlz/lib/financial_ping.h" |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 private: | 39 private: |
| 40 HINTERNET handle_; | 40 HINTERNET handle_; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 #else | 45 #else |
| 46 | 46 |
| 47 #include "base/bind.h" | 47 #include "base/bind.h" |
| 48 #include "base/message_loop.h" | 48 #include "base/message_loop.h" |
| 49 #include "base/run_loop.h" |
| 49 #include "base/time.h" | 50 #include "base/time.h" |
| 50 #include "googleurl/src/gurl.h" | 51 #include "googleurl/src/gurl.h" |
| 51 #include "net/base/load_flags.h" | 52 #include "net/base/load_flags.h" |
| 52 #include "net/url_request/url_fetcher.h" | 53 #include "net/url_request/url_fetcher.h" |
| 53 #include "net/url_request/url_fetcher_delegate.h" | 54 #include "net/url_request/url_fetcher_delegate.h" |
| 54 #include "net/url_request/url_request_context.h" | 55 #include "net/url_request/url_request_context.h" |
| 55 #include "net/url_request/url_request_context_getter.h" | 56 #include "net/url_request/url_request_context_getter.h" |
| 56 | 57 |
| 57 #endif | 58 #endif |
| 58 | 59 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 return false; | 188 return false; |
| 188 | 189 |
| 189 g_context = context; | 190 g_context = context; |
| 190 return true; | 191 return true; |
| 191 } | 192 } |
| 192 | 193 |
| 193 namespace { | 194 namespace { |
| 194 | 195 |
| 195 class FinancialPingUrlFetcherDelegate : public net::URLFetcherDelegate { | 196 class FinancialPingUrlFetcherDelegate : public net::URLFetcherDelegate { |
| 196 public: | 197 public: |
| 197 FinancialPingUrlFetcherDelegate(MessageLoop* loop) : loop_(loop) { } | 198 FinancialPingUrlFetcherDelegate(const base::Closure& callback) |
| 199 : callback_(callback) { |
| 200 } |
| 198 virtual void OnURLFetchComplete(const net::URLFetcher* source); | 201 virtual void OnURLFetchComplete(const net::URLFetcher* source); |
| 202 |
| 199 private: | 203 private: |
| 200 MessageLoop* loop_; | 204 base::Closure callback_; |
| 201 }; | 205 }; |
| 202 | 206 |
| 203 void FinancialPingUrlFetcherDelegate::OnURLFetchComplete( | 207 void FinancialPingUrlFetcherDelegate::OnURLFetchComplete( |
| 204 const net::URLFetcher* source) { | 208 const net::URLFetcher* source) { |
| 205 loop_->Quit(); | 209 callback_.Run(); |
| 206 } | 210 } |
| 207 | 211 |
| 208 } // namespace | 212 } // namespace |
| 209 | 213 |
| 210 #endif | 214 #endif |
| 211 | 215 |
| 212 bool FinancialPing::PingServer(const char* request, std::string* response) { | 216 bool FinancialPing::PingServer(const char* request, std::string* response) { |
| 213 if (!response) | 217 if (!response) |
| 214 return false; | 218 return false; |
| 215 | 219 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 DWORD bytes_read = 0; | 264 DWORD bytes_read = 0; |
| 261 while (InternetReadFile(http_handle, buffer.get(), kMaxPingResponseLength, | 265 while (InternetReadFile(http_handle, buffer.get(), kMaxPingResponseLength, |
| 262 &bytes_read) && bytes_read > 0) { | 266 &bytes_read) && bytes_read > 0) { |
| 263 response->append(buffer.get(), bytes_read); | 267 response->append(buffer.get(), bytes_read); |
| 264 bytes_read = 0; | 268 bytes_read = 0; |
| 265 }; | 269 }; |
| 266 | 270 |
| 267 return true; | 271 return true; |
| 268 #else | 272 #else |
| 269 // Run a blocking event loop to match the win inet implementation. | 273 // Run a blocking event loop to match the win inet implementation. |
| 270 MessageLoop loop; | 274 scoped_ptr<MessageLoop> message_loop; |
| 271 FinancialPingUrlFetcherDelegate delegate(&loop); | 275 // Ensure that we have a MessageLoop. |
| 276 if (!MessageLoop::current()) |
| 277 message_loop.reset(new MessageLoop); |
| 278 base::RunLoop loop; |
| 279 FinancialPingUrlFetcherDelegate delegate(loop.QuitClosure()); |
| 272 | 280 |
| 273 std::string url = base::StringPrintf("http://%s:%d%s", | 281 std::string url = base::StringPrintf("http://%s:%d%s", |
| 274 kFinancialServer, kFinancialPort, | 282 kFinancialServer, kFinancialPort, |
| 275 request); | 283 request); |
| 276 | 284 |
| 277 scoped_ptr<net::URLFetcher> fetcher(net::URLFetcher::Create( | 285 scoped_ptr<net::URLFetcher> fetcher(net::URLFetcher::Create( |
| 278 GURL(url), net::URLFetcher::GET, &delegate)); | 286 GURL(url), net::URLFetcher::GET, &delegate)); |
| 279 | 287 |
| 280 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 288 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 281 net::LOAD_DO_NOT_SEND_AUTH_DATA | | 289 net::LOAD_DO_NOT_SEND_AUTH_DATA | |
| 282 net::LOAD_DO_NOT_PROMPT_FOR_LOGIN | | 290 net::LOAD_DO_NOT_PROMPT_FOR_LOGIN | |
| 283 net::LOAD_DO_NOT_SEND_COOKIES | | 291 net::LOAD_DO_NOT_SEND_COOKIES | |
| 284 net::LOAD_DO_NOT_SAVE_COOKIES); | 292 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 285 | 293 |
| 286 // Ensure rlz_lib::SetURLRequestContext() has been called before sending | 294 // Ensure rlz_lib::SetURLRequestContext() has been called before sending |
| 287 // pings. | 295 // pings. |
| 288 CHECK(g_context); | 296 CHECK(g_context); |
| 289 fetcher->SetRequestContext(g_context); | 297 fetcher->SetRequestContext(g_context); |
| 290 | 298 |
| 291 const base::TimeDelta kTimeout = base::TimeDelta::FromMinutes(5); | 299 const base::TimeDelta kTimeout = base::TimeDelta::FromMinutes(5); |
| 292 loop.PostTask( | 300 MessageLoop::current()->PostTask( |
| 293 FROM_HERE, | 301 FROM_HERE, |
| 294 base::Bind(&net::URLFetcher::Start, base::Unretained(fetcher.get()))); | 302 base::Bind(&net::URLFetcher::Start, base::Unretained(fetcher.get()))); |
| 295 loop.PostNonNestableDelayedTask( | 303 MessageLoop::current()->PostNonNestableDelayedTask( |
| 296 FROM_HERE, MessageLoop::QuitClosure(), kTimeout); | 304 FROM_HERE, loop.QuitClosure(), kTimeout); |
| 297 | 305 |
| 298 loop.Run(); | 306 loop.Run(); |
| 299 | 307 |
| 300 if (fetcher->GetResponseCode() != 200) | 308 if (fetcher->GetResponseCode() != 200) |
| 301 return false; | 309 return false; |
| 302 | 310 |
| 303 return fetcher->GetResponseAsString(response); | 311 return fetcher->GetResponseAsString(response); |
| 304 #endif | 312 #endif |
| 305 } | 313 } |
| 306 | 314 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 353 |
| 346 bool FinancialPing::ClearLastPingTime(Product product) { | 354 bool FinancialPing::ClearLastPingTime(Product product) { |
| 347 ScopedRlzValueStoreLock lock; | 355 ScopedRlzValueStoreLock lock; |
| 348 RlzValueStore* store = lock.GetStore(); | 356 RlzValueStore* store = lock.GetStore(); |
| 349 if (!store || !store->HasAccess(RlzValueStore::kWriteAccess)) | 357 if (!store || !store->HasAccess(RlzValueStore::kWriteAccess)) |
| 350 return false; | 358 return false; |
| 351 return store->ClearPingTime(product); | 359 return store->ClearPingTime(product); |
| 352 } | 360 } |
| 353 | 361 |
| 354 } // namespace | 362 } // namespace |
| OLD | NEW |