| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "components/sessions/core/base_session_service.h" | 5 #include "components/sessions/core/base_session_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/sessions/core/base_session_service_delegate.h" | 14 #include "components/sessions/core/base_session_service_delegate.h" |
| 15 #include "components/sessions/core/session_backend.h" | 15 #include "components/sessions/core/session_backend.h" |
| 16 | 16 |
| 17 // BaseSessionService --------------------------------------------------------- | 17 // BaseSessionService --------------------------------------------------------- |
| 18 | 18 |
| 19 namespace sessions { | 19 namespace sessions { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner | 22 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner |
| 23 // thread if it's not canceled. | 23 // thread if it's not canceled. |
| 24 void RunIfNotCanceled( | 24 void RunIfNotCanceled( |
| 25 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, | 25 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 26 const BaseSessionService::GetCommandsCallback& callback, | 26 const BaseSessionService::GetCommandsCallback& callback, |
| 27 ScopedVector<SessionCommand> commands) { | 27 std::vector<std::unique_ptr<SessionCommand>> commands) { |
| 28 if (is_canceled.Run()) | 28 if (is_canceled.Run()) |
| 29 return; | 29 return; |
| 30 callback.Run(std::move(commands)); | 30 callback.Run(std::move(commands)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void PostOrRunInternalGetCommandsCallback( | 33 void PostOrRunInternalGetCommandsCallback( |
| 34 base::TaskRunner* task_runner, | 34 base::TaskRunner* task_runner, |
| 35 const BaseSessionService::GetCommandsCallback& callback, | 35 const BaseSessionService::GetCommandsCallback& callback, |
| 36 ScopedVector<SessionCommand> commands) { | 36 std::vector<std::unique_ptr<SessionCommand>> commands) { |
| 37 if (task_runner->RunsTasksOnCurrentThread()) { | 37 if (task_runner->RunsTasksOnCurrentThread()) { |
| 38 callback.Run(std::move(commands)); | 38 callback.Run(std::move(commands)); |
| 39 } else { | 39 } else { |
| 40 task_runner->PostTask(FROM_HERE, | 40 task_runner->PostTask(FROM_HERE, |
| 41 base::Bind(callback, base::Passed(&commands))); | 41 base::Bind(callback, base::Passed(&commands))); |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 } // namespace | 45 } // namespace |
| 46 | 46 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 73 void BaseSessionService::DeleteLastSession() { | 73 void BaseSessionService::DeleteLastSession() { |
| 74 RunTaskOnBackendThread( | 74 RunTaskOnBackendThread( |
| 75 FROM_HERE, | 75 FROM_HERE, |
| 76 base::Bind(&SessionBackend::DeleteLastSession, backend_)); | 76 base::Bind(&SessionBackend::DeleteLastSession, backend_)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void BaseSessionService::ScheduleCommand( | 79 void BaseSessionService::ScheduleCommand( |
| 80 std::unique_ptr<SessionCommand> command) { | 80 std::unique_ptr<SessionCommand> command) { |
| 81 DCHECK(command); | 81 DCHECK(command); |
| 82 commands_since_reset_++; | 82 commands_since_reset_++; |
| 83 pending_commands_.push_back(command.release()); | 83 pending_commands_.push_back(std::move(command)); |
| 84 StartSaveTimer(); | 84 StartSaveTimer(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void BaseSessionService::AppendRebuildCommand( | 87 void BaseSessionService::AppendRebuildCommand( |
| 88 std::unique_ptr<SessionCommand> command) { | 88 std::unique_ptr<SessionCommand> command) { |
| 89 DCHECK(command); | 89 DCHECK(command); |
| 90 pending_commands_.push_back(command.release()); | 90 pending_commands_.push_back(std::move(command)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void BaseSessionService::EraseCommand(SessionCommand* old_command) { | 93 void BaseSessionService::EraseCommand(SessionCommand* old_command) { |
| 94 ScopedVector<SessionCommand>::iterator it = | 94 auto it = std::find_if( |
| 95 std::find(pending_commands_.begin(), | 95 pending_commands_.begin(), pending_commands_.end(), |
| 96 pending_commands_.end(), | 96 [old_command](const std::unique_ptr<SessionCommand>& command_ptr) { |
| 97 old_command); | 97 return command_ptr.get() == old_command; |
| 98 }); |
| 98 CHECK(it != pending_commands_.end()); | 99 CHECK(it != pending_commands_.end()); |
| 99 pending_commands_.erase(it); | 100 pending_commands_.erase(it); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void BaseSessionService::SwapCommand( | 103 void BaseSessionService::SwapCommand( |
| 103 SessionCommand* old_command, | 104 SessionCommand* old_command, |
| 104 std::unique_ptr<SessionCommand> new_command) { | 105 std::unique_ptr<SessionCommand> new_command) { |
| 105 ScopedVector<SessionCommand>::iterator it = | 106 auto it = std::find_if( |
| 106 std::find(pending_commands_.begin(), | 107 pending_commands_.begin(), pending_commands_.end(), |
| 107 pending_commands_.end(), | 108 [old_command](const std::unique_ptr<SessionCommand>& command_ptr) { |
| 108 old_command); | 109 return command_ptr.get() == old_command; |
| 110 }); |
| 109 CHECK(it != pending_commands_.end()); | 111 CHECK(it != pending_commands_.end()); |
| 110 *it = new_command.release(); | 112 *it = std::move(new_command); |
| 111 delete old_command; | |
| 112 } | 113 } |
| 113 | 114 |
| 114 void BaseSessionService::ClearPendingCommands() { | 115 void BaseSessionService::ClearPendingCommands() { |
| 115 pending_commands_.clear(); | 116 pending_commands_.clear(); |
| 116 } | 117 } |
| 117 | 118 |
| 118 void BaseSessionService::StartSaveTimer() { | 119 void BaseSessionService::StartSaveTimer() { |
| 119 // Don't start a timer when testing. | 120 // Don't start a timer when testing. |
| 120 if (delegate_->ShouldUseDelayedSave() && | 121 if (delegate_->ShouldUseDelayedSave() && |
| 121 base::ThreadTaskRunnerHandle::IsSet() && !weak_factory_.HasWeakPtrs()) { | 122 base::ThreadTaskRunnerHandle::IsSet() && !weak_factory_.HasWeakPtrs()) { |
| 122 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 123 FROM_HERE, | 124 FROM_HERE, |
| 124 base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()), | 125 base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()), |
| 125 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 126 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 void BaseSessionService::Save() { | 130 void BaseSessionService::Save() { |
| 130 // Inform the delegate that we will save the commands now, giving it the | 131 // Inform the delegate that we will save the commands now, giving it the |
| 131 // opportunity to append more commands. | 132 // opportunity to append more commands. |
| 132 delegate_->OnWillSaveCommands(); | 133 delegate_->OnWillSaveCommands(); |
| 133 | 134 |
| 134 if (pending_commands_.empty()) | 135 if (pending_commands_.empty()) |
| 135 return; | 136 return; |
| 136 | 137 |
| 137 // We create a new ScopedVector which will receive all elements from the | 138 // We create a new vector which will receive all elements from the |
| 138 // current commands. This will also clear the current list. | 139 // current commands. This will also clear the current list. |
| 139 RunTaskOnBackendThread( | 140 RunTaskOnBackendThread( |
| 140 FROM_HERE, | 141 FROM_HERE, |
| 141 base::Bind(&SessionBackend::AppendCommands, backend_, | 142 base::Bind(&SessionBackend::AppendCommands, backend_, |
| 142 base::Passed(&pending_commands_), | 143 base::Passed(&pending_commands_), |
| 143 pending_reset_)); | 144 pending_reset_)); |
| 144 | 145 |
| 145 if (pending_reset_) { | 146 if (pending_reset_) { |
| 146 commands_since_reset_ = 0; | 147 commands_since_reset_ = 0; |
| 147 pending_reset_ = false; | 148 pending_reset_ = false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); | 182 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); |
| 182 } else { | 183 } else { |
| 183 // Fall back to executing on the main thread if the sequence | 184 // Fall back to executing on the main thread if the sequence |
| 184 // worker pool has been requested to shutdown (around shutdown | 185 // worker pool has been requested to shutdown (around shutdown |
| 185 // time). | 186 // time). |
| 186 task.Run(); | 187 task.Run(); |
| 187 } | 188 } |
| 188 } | 189 } |
| 189 | 190 |
| 190 } // namespace sessions | 191 } // namespace sessions |
| OLD | NEW |