diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-01-29 16:35:13 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-02-01 15:33:35 +0000 |
commit | c8c2d1901aec01e934adf561a9fdf0cc776cdef8 (patch) | |
tree | 9157c3d9815e5870799e070b113813bec53e0535 /chromium/base/ios | |
parent | abefd5095b41dac94ca451d784ab6e27372e981a (diff) | |
download | qtwebengine-chromium-c8c2d1901aec01e934adf561a9fdf0cc776cdef8.tar.gz |
BASELINE: Update Chromium to 64.0.3282.139
Change-Id: I1cae68fe9c94ff7608b26b8382fc19862cdb293a
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/base/ios')
-rw-r--r-- | chromium/base/ios/callback_counter.h | 46 | ||||
-rw-r--r-- | chromium/base/ios/callback_counter.mm | 35 | ||||
-rw-r--r-- | chromium/base/ios/callback_counter_unittest.mm | 58 |
3 files changed, 139 insertions, 0 deletions
diff --git a/chromium/base/ios/callback_counter.h b/chromium/base/ios/callback_counter.h new file mode 100644 index 00000000000..eef45547355 --- /dev/null +++ b/chromium/base/ios/callback_counter.h @@ -0,0 +1,46 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_CHROME_BROWSER_CALLBACK_COUNTER_H_ +#define IOS_CHROME_BROWSER_CALLBACK_COUNTER_H_ + +#include "base/callback.h" +#include "base/logging.h" +#include "base/memory/ref_counted.h" +#include "base/threading/thread.h" + +// A helper class that keeps count of the number of pending callbacks that need +// to be received. Calls |final_callback| when all callbacks have been received. +// All methods (except the destructor) must be called on the same thread. +class CallbackCounter : public base::RefCounted<CallbackCounter> { + public: + typedef base::Callback<void()> FinalCallback; + + explicit CallbackCounter(const FinalCallback& final_callback); + + // Increments the count of pending callbacks by |count| . + void IncrementCount(int count); + + // Increments the count of pending callbacks by 1. + void IncrementCount(); + + // Decrements the count of pending callbacks. + void DecrementCount(); + + private: + friend class base::RefCounted<CallbackCounter>; + + ~CallbackCounter(); + + // The number of callbacks that still need to be received. + unsigned callback_count_; + // The callback that is finally called when all callbacks have been received + // (when the |callback_count_| goes down to 0). + FinalCallback final_callback_; + base::ThreadChecker thread_checker_; + + DISALLOW_COPY_AND_ASSIGN(CallbackCounter); +}; + +#endif // IOS_CHROME_BROWSER_CALLBACK_COUNTER_H_ diff --git a/chromium/base/ios/callback_counter.mm b/chromium/base/ios/callback_counter.mm new file mode 100644 index 00000000000..575500925ce --- /dev/null +++ b/chromium/base/ios/callback_counter.mm @@ -0,0 +1,35 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/ios/callback_counter.h" + +CallbackCounter::CallbackCounter(const FinalCallback& final_callback) + : callback_count_(0U), final_callback_(final_callback) { + DCHECK(!final_callback.is_null()); +} + +CallbackCounter::~CallbackCounter() { + DCHECK_EQ(0U, callback_count_); +} + +void CallbackCounter::IncrementCount(int count) { + DCHECK(thread_checker_.CalledOnValidThread()); + DCHECK(!final_callback_.is_null()); + callback_count_ += count; +} + +void CallbackCounter::IncrementCount() { + IncrementCount(1); +} + +void CallbackCounter::DecrementCount() { + DCHECK(thread_checker_.CalledOnValidThread()); + DCHECK(callback_count_); + + --callback_count_; + if (callback_count_ == 0) { + final_callback_.Run(); + final_callback_.Reset(); + } +} diff --git a/chromium/base/ios/callback_counter_unittest.mm b/chromium/base/ios/callback_counter_unittest.mm new file mode 100644 index 00000000000..6d194aaf648 --- /dev/null +++ b/chromium/base/ios/callback_counter_unittest.mm @@ -0,0 +1,58 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/ios/callback_counter.h" + +#include "base/bind.h" +#include "base/mac/bind_objc_block.h" +#include "base/memory/ref_counted.h" +#import "base/test/ios/wait_util.h" +#include "testing/platform_test.h" + +using CallbackCounterTest = PlatformTest; + +// Tests that CallbackCounter works with adding callbacks one by one. +TEST_F(CallbackCounterTest, BasicIncrementByOne) { + __block BOOL block_was_called = NO; + scoped_refptr<CallbackCounter> callback_counter = + new CallbackCounter(base::BindBlock(^{ + block_was_called = YES; + })); + + // Enqueue the first callback. + callback_counter->IncrementCount(); + dispatch_async(dispatch_get_main_queue(), ^{ + callback_counter->DecrementCount(); + }); + + // Enqueue the second callback. + callback_counter->IncrementCount(); + dispatch_async(dispatch_get_main_queue(), ^{ + callback_counter->DecrementCount(); + }); + + base::test::ios::WaitUntilCondition(^bool() { + return block_was_called; + }); +} + +// Tests that CallbackCounter works with adding all callbacks at once. +TEST_F(CallbackCounterTest, BasicIncrementByMoreThanOne) { + __block BOOL block_was_called = NO; + scoped_refptr<CallbackCounter> callback_counter = + new CallbackCounter(base::BindBlock(^{ + block_was_called = YES; + })); + + // Enqueue the 5 callbacks. + callback_counter->IncrementCount(5); + for (int i = 0; i < 5; i++) { + dispatch_async(dispatch_get_main_queue(), ^{ + callback_counter->DecrementCount(); + }); + } + base::test::ios::WaitUntilCondition(^bool() { + return block_was_called; + }); +} |