Commit 65feca2a authored by Gang Wu's avatar Gang Wu Committed by Commit Bot

[Sync] change BridgeTask from Callback to OnceCallback.

change ModelTypeController::BridgeTask from Callback to OnceCallback.

Bug: 786478
Change-Id: I5d559aafefa3917b5cde5e06f398078989b3bc10
Reviewed-on: https://chromium-review.googlesource.com/777386
Commit-Queue: Gang Wu <gangwu@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517633}
parent f810ef4a
...@@ -25,8 +25,8 @@ namespace { ...@@ -25,8 +25,8 @@ namespace {
// the tasks we want to run. // the tasks we want to run.
class RunTaskOnHistoryThread : public HistoryDBTask { class RunTaskOnHistoryThread : public HistoryDBTask {
public: public:
explicit RunTaskOnHistoryThread(const ModelTypeController::BridgeTask& task) explicit RunTaskOnHistoryThread(ModelTypeController::BridgeTask task)
: task_(task) {} : task_(std::move(task)) {}
bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override { bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override {
// Invoke the task, then free it immediately so we don't keep a reference // Invoke the task, then free it immediately so we don't keep a reference
...@@ -34,13 +34,9 @@ class RunTaskOnHistoryThread : public HistoryDBTask { ...@@ -34,13 +34,9 @@ class RunTaskOnHistoryThread : public HistoryDBTask {
// main thread - we want to release references as soon as possible to avoid // main thread - we want to release references as soon as possible to avoid
// keeping them around too long during shutdown. // keeping them around too long during shutdown.
TypedURLSyncBridge* bridge = backend->GetTypedURLSyncBridge(); TypedURLSyncBridge* bridge = backend->GetTypedURLSyncBridge();
if (!bridge) { DCHECK(bridge);
NOTREACHED();
return true;
}
task_.Run(bridge); std::move(task_).Run(bridge);
task_.Reset();
return true; return true;
} }
...@@ -73,7 +69,7 @@ bool TypedURLModelTypeController::ReadyForStart() const { ...@@ -73,7 +69,7 @@ bool TypedURLModelTypeController::ReadyForStart() const {
} }
void TypedURLModelTypeController::PostBridgeTask(const base::Location& location, void TypedURLModelTypeController::PostBridgeTask(const base::Location& location,
const BridgeTask& task) { BridgeTask task) {
history::HistoryService* history = sync_client()->GetHistoryService(); history::HistoryService* history = sync_client()->GetHistoryService();
if (!history) { if (!history) {
// History must be disabled - don't start. // History must be disabled - don't start.
...@@ -81,8 +77,9 @@ void TypedURLModelTypeController::PostBridgeTask(const base::Location& location, ...@@ -81,8 +77,9 @@ void TypedURLModelTypeController::PostBridgeTask(const base::Location& location,
return; return;
} }
history->ScheduleDBTask(std::make_unique<RunTaskOnHistoryThread>(task), history->ScheduleDBTask(
&task_tracker_); std::make_unique<RunTaskOnHistoryThread>(std::move(task)),
&task_tracker_);
} }
void TypedURLModelTypeController::OnSavingBrowserHistoryDisabledChanged() { void TypedURLModelTypeController::OnSavingBrowserHistoryDisabledChanged() {
......
...@@ -23,8 +23,7 @@ class TypedURLModelTypeController : public syncer::ModelTypeController { ...@@ -23,8 +23,7 @@ class TypedURLModelTypeController : public syncer::ModelTypeController {
private: private:
// syncer::ModelTypeController implementation. // syncer::ModelTypeController implementation.
void PostBridgeTask(const base::Location& location, void PostBridgeTask(const base::Location& location, BridgeTask task) override;
const BridgeTask& task) override;
void OnSavingBrowserHistoryDisabledChanged(); void OnSavingBrowserHistoryDisabledChanged();
......
...@@ -52,11 +52,10 @@ base::WeakPtr<ModelTypeSyncBridge> ReturnCapturedBridge( ...@@ -52,11 +52,10 @@ base::WeakPtr<ModelTypeSyncBridge> ReturnCapturedBridge(
return arg; return arg;
} }
void RunBridgeTask(const BridgeProvider& bridge_provider, void RunBridgeTask(BridgeProvider bridge_provider, BridgeTask task) {
const BridgeTask& task) { base::WeakPtr<ModelTypeSyncBridge> bridge = std::move(bridge_provider).Run();
if (base::WeakPtr<ModelTypeSyncBridge> bridge = bridge_provider.Run()) { if (bridge.get())
task.Run(bridge.get()); std::move(task).Run(bridge.get());
}
} }
} // namespace } // namespace
...@@ -263,10 +262,11 @@ BridgeProvider ModelTypeController::GetBridgeProvider() { ...@@ -263,10 +262,11 @@ BridgeProvider ModelTypeController::GetBridgeProvider() {
} }
void ModelTypeController::PostBridgeTask(const base::Location& location, void ModelTypeController::PostBridgeTask(const base::Location& location,
const BridgeTask& task) { BridgeTask task) {
DCHECK(model_thread_); DCHECK(model_thread_);
model_thread_->PostTask( model_thread_->PostTask(
location, base::Bind(&RunBridgeTask, GetBridgeProvider(), task)); location, base::Bind(&RunBridgeTask, base::Passed(GetBridgeProvider()),
base::Passed(std::move(task))));
} }
} // namespace syncer } // namespace syncer
...@@ -27,8 +27,9 @@ struct ActivationContext; ...@@ -27,8 +27,9 @@ struct ActivationContext;
// DataTypeController implementation for Unified Sync and Storage model types. // DataTypeController implementation for Unified Sync and Storage model types.
class ModelTypeController : public DataTypeController { class ModelTypeController : public DataTypeController {
public: public:
using BridgeProvider = base::Callback<base::WeakPtr<ModelTypeSyncBridge>()>; using BridgeProvider =
using BridgeTask = base::Callback<void(ModelTypeSyncBridge*)>; base::OnceCallback<base::WeakPtr<ModelTypeSyncBridge>()>;
using BridgeTask = base::OnceCallback<void(ModelTypeSyncBridge*)>;
ModelTypeController( ModelTypeController(
ModelType type, ModelType type,
...@@ -76,8 +77,7 @@ class ModelTypeController : public DataTypeController { ...@@ -76,8 +77,7 @@ class ModelTypeController : public DataTypeController {
// Post the given task that requires the bridge object to run to the model // Post the given task that requires the bridge object to run to the model
// thread, where the bridge lives. // thread, where the bridge lives.
virtual void PostBridgeTask(const base::Location& location, virtual void PostBridgeTask(const base::Location& location, BridgeTask task);
const BridgeTask& task);
// The sync client, which provides access to this type's ModelTypeSyncBridge. // The sync client, which provides access to this type's ModelTypeSyncBridge.
SyncClient* const sync_client_; SyncClient* const sync_client_;
......
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