Chromium Code Reviews| Index: rlz/lib/financial_ping.cc |
| diff --git a/rlz/lib/financial_ping.cc b/rlz/lib/financial_ping.cc |
| index f09355fe094a4e0ea10465fb08a9ccd946ab6f80..67c3bfc4ec63dacbbff6ed6318ab113870714bd3 100644 |
| --- a/rlz/lib/financial_ping.cc |
| +++ b/rlz/lib/financial_ping.cc |
| @@ -13,11 +13,12 @@ |
| #include "base/atomicops.h" |
| #include "base/location.h" |
| #include "base/macros.h" |
| -#include "base/memory/weak_ptr.h" |
| -#include "base/single_thread_task_runner.h" |
| +#include "base/memory/ref_counted.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/task_scheduler/post_task.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "build/build_config.h" |
| #include "rlz/lib/assert.h" |
| @@ -203,39 +204,54 @@ bool FinancialPing::SetURLRequestContext( |
| namespace { |
| -class FinancialPingUrlFetcherDelegate : public net::URLFetcherDelegate { |
| +class RefCountedWaitableEvent |
| + : public base::RefCountedThreadSafe<RefCountedWaitableEvent> { |
| public: |
| - FinancialPingUrlFetcherDelegate(const base::Closure& callback) |
| - : callback_(callback) { |
| + RefCountedWaitableEvent() |
| + : event_(base::WaitableEvent::ResetPolicy::MANUAL, |
| + base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| + |
| + void Signal() { event_.Signal(); } |
| + |
| + bool TimedWait(const base::TimeDelta& timeout) { |
| + return event_.TimedWait(timeout); |
| } |
| - void OnURLFetchComplete(const net::URLFetcher* source) override; |
| private: |
| - base::Closure callback_; |
| + ~RefCountedWaitableEvent() {} |
| + friend class base::RefCountedThreadSafe<RefCountedWaitableEvent>; |
| + |
| + base::WaitableEvent event_; |
| }; |
| -void FinancialPingUrlFetcherDelegate::OnURLFetchComplete( |
| - const net::URLFetcher* source) { |
| - callback_.Run(); |
| -} |
| +class FinancialPingUrlFetcherDelegate : public net::URLFetcherDelegate { |
| + public: |
| + FinancialPingUrlFetcherDelegate(scoped_refptr<RefCountedWaitableEvent> event) |
| + : event_(event) {} |
|
gab
2017/07/17 16:44:20
std::move(event)
Roger Tawa OOO till Jul 10th
2017/07/17 19:19:37
This happens exactly once per run of chrome.exe in
gab
2017/07/17 21:05:20
It's just a good habit to have when taking scoped_
|
| + |
| + void OnURLFetchComplete(const net::URLFetcher* source) override { |
|
gab
2017/07/17 16:44:20
Does this work? url_fetch.h says:
// You may crea
Roger Tawa OOO till Jul 10th
2017/07/17 19:19:36
I think that comment in URLFetcher may no longer b
gab
2017/07/17 21:05:20
Hmmmm, well it's definitely also not running on th
Roger Tawa OOO till Jul 10th
2017/07/21 13:53:42
Got it. The main point is to not block this seque
gab
2017/07/21 22:39:17
True, so long as that doesn't become SequencedTask
|
| + event_->Signal(); |
| + } |
| + |
| + private: |
| + scoped_refptr<RefCountedWaitableEvent> event_; |
| +}; |
| bool send_financial_ping_interrupted_for_test = false; |
| } // namespace |
| -void ShutdownCheck(base::WeakPtr<base::RunLoop> weak) { |
| - if (!weak.get()) |
| - return; |
| +void ShutdownCheck(scoped_refptr<RefCountedWaitableEvent> event) { |
| if (!base::subtle::Acquire_Load(&g_context)) { |
| send_financial_ping_interrupted_for_test = true; |
| - weak->QuitClosure().Run(); |
| + event->Signal(); |
| return; |
| } |
| // How frequently the financial ping thread should check |
| // the shutdown condition? |
| const base::TimeDelta kInterval = base::TimeDelta::FromMilliseconds(500); |
| - base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| - FROM_HERE, base::Bind(&ShutdownCheck, weak), kInterval); |
| + base::PostDelayedTask(FROM_HERE, base::Bind(&ShutdownCheck, event), |
| + kInterval); |
| } |
| #endif |
| @@ -307,13 +323,8 @@ bool FinancialPing::PingServer(const char* request, std::string* response) { |
| if (!context) |
| return false; |
| - // Run a blocking event loop to match the win inet implementation. |
|
gab
2017/07/17 16:44:20
Should we keep the comment to say that this is "bl
Roger Tawa OOO till Jul 10th
2017/07/17 19:19:36
Done.
|
| - std::unique_ptr<base::MessageLoop> message_loop; |
| - // Ensure that we have a MessageLoop. |
| - if (!base::MessageLoop::current()) |
| - message_loop.reset(new base::MessageLoop); |
| - base::RunLoop loop; |
| - FinancialPingUrlFetcherDelegate delegate(loop.QuitClosure()); |
| + auto event = base::MakeRefCounted<RefCountedWaitableEvent>(); |
| + FinancialPingUrlFetcherDelegate delegate(event); |
| std::string url = base::StringPrintf("http://%s:%d%s", |
| kFinancialServer, kFinancialPort, |
| @@ -354,20 +365,11 @@ bool FinancialPing::PingServer(const char* request, std::string* response) { |
| // pings. |
| fetcher->SetRequestContext(context); |
| - base::WeakPtrFactory<base::RunLoop> weak(&loop); |
| - |
| - const base::TimeDelta kTimeout = base::TimeDelta::FromMinutes(5); |
| - base::MessageLoop::ScopedNestableTaskAllower allow_nested( |
| - base::MessageLoop::current()); |
| - base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, base::Bind(&ShutdownCheck, weak.GetWeakPtr())); |
| - base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, |
| - base::Bind(&net::URLFetcher::Start, base::Unretained(fetcher.get()))); |
| - base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| - FROM_HERE, loop.QuitClosure(), kTimeout); |
| + base::PostTask(FROM_HERE, base::Bind(&ShutdownCheck, event)); |
| + base::PostTask(FROM_HERE, base::Bind(&net::URLFetcher::Start, |
|
gab
2017/07/17 16:44:20
net::URLFetcher::Start() seems to just PostTask it
Roger Tawa OOO till Jul 10th
2017/07/17 19:19:36
No, see comment above.
gab
2017/07/17 21:05:20
If the fetcher replies on another sequence, isn't
Roger Tawa OOO till Jul 10th
2017/07/21 13:53:42
Correct, this was racy and still is. That's OK th
gab
2017/07/21 22:39:17
It's okay that this may fail and will be retried b
|
| + base::Unretained(fetcher.get()))); |
| - loop.Run(); |
| + event->TimedWait(base::TimeDelta::FromMinutes(5)); |
| if (fetcher->GetResponseCode() != 200) |
| return false; |