Commit 9e2ebb26 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Remove CallbackCounter class

There were at least three different CallbackCounter classes
in the code base. Most of them counting the number of time
a callback has been invoked (and used only for tests) and
one invoking a OnceClosure after calls to DecrementCounter
matched earlier calls to IncrementCounter.

Since the class in base/ios/callback_counter.h had only a
single use, inline the code into the callsite and remove
the class that is confusing (the name does not really
correspond to the behaviour of the class and neither to
the other classes with the same name).

Other implementations are in:
- components/sync/test/callback_counter.h
- net/cookies/cookie_store_test_helpers.h

Bug: none
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I822243085afde29e640c72fd3e9790240a965987
Reviewed-on: https://chromium-review.googlesource.com/1020235
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarMohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#552257}
parent a3d3f13c
......@@ -388,8 +388,6 @@ jumbo_component("base") {
"hash.cc",
"hash.h",
"ios/block_types.h",
"ios/callback_counter.h",
"ios/callback_counter.mm",
"ios/crb_protocol_observers.h",
"ios/crb_protocol_observers.mm",
"ios/device_util.h",
......@@ -2143,7 +2141,6 @@ test("base_unittests") {
"i18n/string_search_unittest.cc",
"i18n/time_formatting_unittest.cc",
"i18n/timezone_unittest.cc",
"ios/callback_counter_unittest.mm",
"ios/crb_protocol_observers_unittest.mm",
"ios/device_util_unittest.mm",
"ios/weak_nsobject_unittest.mm",
......
// 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_
// 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();
}
}
// 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;
});
}
......@@ -16,7 +16,6 @@
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/ios/block_types.h"
#include "base/ios/callback_counter.h"
#import "base/mac/bind_objc_block.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
......
......@@ -5,7 +5,7 @@
#import "ios/web/net/cookies/wk_http_system_cookie_store.h"
#include "base/bind.h"
#include "base/ios/callback_counter.h"
#import "base/ios/block_types.h"
#import "base/mac/bind_objc_block.h"
#import "ios/net/cookies/cookie_creation_time_manager.h"
#include "ios/net/cookies/system_cookie_util.h"
......@@ -21,18 +21,16 @@
#endif
namespace web {
namespace {
// private
// Posts |callback| to run on IO Thread, this is needed because
// WKHTTPCookieStore executes callbacks on the main thread, while
// SystemCookieStore should operate on IO thread.
void RunSystemCookieCallbackOnIOThread(base::OnceClosure callback) {
if (callback.is_null())
return;
DCHECK(!callback.is_null());
web::WebThread::PostTask(web::WebThread::IO, FROM_HERE, std::move(callback));
}
// private
// Returns wether |cookie| should be included for queries about |url|.
// To include |cookie| for |url|, all these conditions need to be met:
// 1- If the cookie is secure the URL needs to be secure.
......@@ -50,6 +48,8 @@ bool ShouldIncludeForRequestUrl(NSHTTPCookie* cookie, const GURL& url) {
return canonical_cookie.IncludeForRequestURL(url, options);
}
} // namespace
WKHTTPSystemCookieStore::WKHTTPSystemCookieStore(
WKHTTPCookieStore* cookie_store)
: cookie_store_(cookie_store) {}
......@@ -159,19 +159,31 @@ void WKHTTPSystemCookieStore::ClearStoreAsync(SystemCookieCallback callback) {
web::WebThread::PostTask(
web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{
[cookie_store_ getAllCookies:^(NSArray<NSHTTPCookie*>* cookies) {
scoped_refptr<CallbackCounter> callback_counter =
new CallbackCounter(base::BindRepeating(
&RunSystemCookieCallbackOnIOThread, base::BindBlockArc(^{
if (weak_time_manager)
weak_time_manager->Clear();
std::move(shared_callback).Run();
})));
// Add callback for each cookie
callback_counter->IncrementCount(cookies.count);
ProceduralBlock completionHandler = ^{
RunSystemCookieCallbackOnIOThread(base::BindBlockArc(^{
if (weak_time_manager)
weak_time_manager->Clear();
std::move(shared_callback).Run();
}));
};
// If there are no cookies to clear, immediately invoke the
// completion handler on IO thread, otherwise count the number
// of cookies that still need to be cleared and invoke it when
// all of them have been cleared.
__block NSUInteger remainingCookiesToClearCount = cookies.count;
if (remainingCookiesToClearCount == 0) {
completionHandler();
return;
}
for (NSHTTPCookie* cookie in cookies) {
[cookie_store_ deleteCookie:cookie
completionHandler:^{
callback_counter->DecrementCount();
DCHECK(remainingCookiesToClearCount);
if (--remainingCookiesToClearCount == 0) {
completionHandler();
}
}];
}
}];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment