| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/sessions/base_session_service.h" | 5 #include "chrome/browser/sessions/base_session_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
| 9 #include "chrome/browser/sessions/base_session_service_delegate.h" | 9 #include "chrome/browser/sessions/base_session_service_delegate.h" |
| 10 #include "chrome/browser/sessions/session_backend.h" | 10 #include "chrome/browser/sessions/session_backend.h" |
| 11 | 11 |
| 12 // BaseSessionService --------------------------------------------------------- | 12 // BaseSessionService --------------------------------------------------------- |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner | 16 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner |
| 17 // thread if it's not canceled. | 17 // thread if it's not canceled. |
| 18 void RunIfNotCanceled( | 18 void RunIfNotCanceled( |
| 19 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, | 19 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 20 const BaseSessionService::InternalGetCommandsCallback& callback, | 20 const BaseSessionService::GetCommandsCallback& callback, |
| 21 ScopedVector<SessionCommand> commands) { | 21 ScopedVector<SessionCommand> commands) { |
| 22 if (is_canceled.Run()) | 22 if (is_canceled.Run()) |
| 23 return; | 23 return; |
| 24 callback.Run(commands.Pass()); | 24 callback.Run(commands.Pass()); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void PostOrRunInternalGetCommandsCallback( | 27 void PostOrRunInternalGetCommandsCallback( |
| 28 base::TaskRunner* task_runner, | 28 base::TaskRunner* task_runner, |
| 29 const BaseSessionService::InternalGetCommandsCallback& callback, | 29 const BaseSessionService::GetCommandsCallback& callback, |
| 30 ScopedVector<SessionCommand> commands) { | 30 ScopedVector<SessionCommand> commands) { |
| 31 if (task_runner->RunsTasksOnCurrentThread()) { | 31 if (task_runner->RunsTasksOnCurrentThread()) { |
| 32 callback.Run(commands.Pass()); | 32 callback.Run(commands.Pass()); |
| 33 } else { | 33 } else { |
| 34 task_runner->PostTask(FROM_HERE, | 34 task_runner->PostTask(FROM_HERE, |
| 35 base::Bind(callback, base::Passed(&commands))); | 35 base::Bind(callback, base::Passed(&commands))); |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 // Delay between when a command is received, and when we save it to the | 41 // Delay between when a command is received, and when we save it to the |
| 42 // backend. | 42 // backend. |
| 43 static const int kSaveDelayMS = 2500; | 43 static const int kSaveDelayMS = 2500; |
| 44 | 44 |
| 45 // static | |
| 46 const int BaseSessionService::max_persist_navigation_count = 6; | |
| 47 | |
| 48 BaseSessionService::BaseSessionService( | 45 BaseSessionService::BaseSessionService( |
| 49 SessionType type, | 46 SessionType type, |
| 50 const base::FilePath& path, | 47 const base::FilePath& path, |
| 51 scoped_ptr<BaseSessionServiceDelegate> delegate) | 48 BaseSessionServiceDelegate* delegate) |
| 52 : pending_reset_(false), | 49 : pending_reset_(false), |
| 53 commands_since_reset_(0), | 50 commands_since_reset_(0), |
| 54 delegate_(delegate.Pass()), | 51 delegate_(delegate), |
| 55 sequence_token_(delegate_->GetBlockingPool()->GetSequenceToken()), | 52 sequence_token_(delegate_->GetBlockingPool()->GetSequenceToken()), |
| 56 weak_factory_(this) { | 53 weak_factory_(this) { |
| 57 backend_ = new SessionBackend(type, path); | 54 backend_ = new SessionBackend(type, path); |
| 58 DCHECK(backend_.get()); | 55 DCHECK(backend_.get()); |
| 59 } | 56 } |
| 60 | 57 |
| 61 BaseSessionService::~BaseSessionService() { | 58 BaseSessionService::~BaseSessionService() {} |
| 59 |
| 60 void BaseSessionService::MoveCurrentSessionToLastSession() { |
| 61 Save(); |
| 62 RunTaskOnBackendThread( |
| 63 FROM_HERE, base::Bind(&SessionBackend::MoveCurrentSessionToLastSession, |
| 64 backend_)); |
| 62 } | 65 } |
| 63 | 66 |
| 64 void BaseSessionService::DeleteLastSession() { | 67 void BaseSessionService::DeleteLastSession() { |
| 65 RunTaskOnBackendThread( | 68 RunTaskOnBackendThread( |
| 66 FROM_HERE, | 69 FROM_HERE, |
| 67 base::Bind(&SessionBackend::DeleteLastSession, backend())); | 70 base::Bind(&SessionBackend::DeleteLastSession, backend_)); |
| 68 } | 71 } |
| 69 | 72 |
| 70 void BaseSessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) { | 73 void BaseSessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) { |
| 71 DCHECK(command); | 74 DCHECK(command); |
| 72 commands_since_reset_++; | 75 commands_since_reset_++; |
| 73 pending_commands_.push_back(command.release()); | 76 pending_commands_.push_back(command.release()); |
| 74 StartSaveTimer(); | 77 StartSaveTimer(); |
| 75 } | 78 } |
| 76 | 79 |
| 80 void BaseSessionService::AppendRebuildCommand( |
| 81 scoped_ptr<SessionCommand> command) { |
| 82 DCHECK(command); |
| 83 pending_commands_.push_back(command.release()); |
| 84 } |
| 85 |
| 86 void BaseSessionService::EraseCommand(scoped_ptr<SessionCommand> old_command) { |
| 87 ScopedVector<SessionCommand>::iterator it = |
| 88 std::find(pending_commands_.begin(), |
| 89 pending_commands_.end(), |
| 90 old_command.release()); |
| 91 CHECK(it != pending_commands_.end()); |
| 92 pending_commands_.erase(it); |
| 93 } |
| 94 |
| 95 void BaseSessionService::SwapCommand(scoped_ptr<SessionCommand> old_command, |
| 96 scoped_ptr<SessionCommand> new_command) { |
| 97 ScopedVector<SessionCommand>::iterator it = |
| 98 std::find(pending_commands_.begin(), |
| 99 pending_commands_.end(), |
| 100 old_command.get()); |
| 101 CHECK(it != pending_commands_.end()); |
| 102 *it = new_command.release(); |
| 103 } |
| 104 |
| 105 void BaseSessionService::ClearPendingCommands() { |
| 106 pending_commands_.clear(); |
| 107 } |
| 108 |
| 77 void BaseSessionService::StartSaveTimer() { | 109 void BaseSessionService::StartSaveTimer() { |
| 78 // Don't start a timer when testing. | 110 // Don't start a timer when testing. |
| 79 if (delegate_->ShouldUseDelayedSave() && base::MessageLoop::current() && | 111 if (delegate_->ShouldUseDelayedSave() && base::MessageLoop::current() && |
| 80 !weak_factory_.HasWeakPtrs()) { | 112 !weak_factory_.HasWeakPtrs()) { |
| 81 base::MessageLoop::current()->PostDelayedTask( | 113 base::MessageLoop::current()->PostDelayedTask( |
| 82 FROM_HERE, | 114 FROM_HERE, |
| 83 base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()), | 115 base::Bind(&BaseSessionService::Save, |
| 116 weak_factory_.GetWeakPtr()), |
| 84 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 117 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
| 85 } | 118 } |
| 86 } | 119 } |
| 87 | 120 |
| 88 void BaseSessionService::Save() { | 121 void BaseSessionService::Save() { |
| 89 DCHECK(backend()); | 122 // Inform the delegate that we will save the commands now, giving it the |
| 123 // opportunity to append more commands. |
| 124 delegate_->OnWillSaveCommands(); |
| 90 | 125 |
| 91 if (pending_commands_.empty()) | 126 if (pending_commands_.empty()) |
| 92 return; | 127 return; |
| 93 | 128 |
| 129 // We create a new ScopedVector which will receive all elements from the |
| 130 // current commands. This will also clear the current list. |
| 94 RunTaskOnBackendThread( | 131 RunTaskOnBackendThread( |
| 95 FROM_HERE, | 132 FROM_HERE, |
| 96 base::Bind(&SessionBackend::AppendCommands, backend(), | 133 base::Bind(&SessionBackend::AppendCommands, backend_, |
| 97 base::Passed(&pending_commands_), | 134 base::Passed(&pending_commands_), |
| 98 pending_reset_)); | 135 pending_reset_)); |
| 99 | 136 |
| 100 if (pending_reset_) { | 137 if (pending_reset_) { |
| 101 commands_since_reset_ = 0; | 138 commands_since_reset_ = 0; |
| 102 pending_reset_ = false; | 139 pending_reset_ = false; |
| 103 } | 140 } |
| 104 } | |
| 105 | 141 |
| 106 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { | 142 delegate_->OnSavedCommands(); |
| 107 return url.is_valid() && delegate_->ShouldTrackEntry(url); | |
| 108 } | 143 } |
| 109 | 144 |
| 110 base::CancelableTaskTracker::TaskId | 145 base::CancelableTaskTracker::TaskId |
| 111 BaseSessionService::ScheduleGetLastSessionCommands( | 146 BaseSessionService::ScheduleGetLastSessionCommands( |
| 112 const InternalGetCommandsCallback& callback, | 147 const GetCommandsCallback& callback, |
| 113 base::CancelableTaskTracker* tracker) { | 148 base::CancelableTaskTracker* tracker) { |
| 114 base::CancelableTaskTracker::IsCanceledCallback is_canceled; | 149 base::CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 115 base::CancelableTaskTracker::TaskId id = | 150 base::CancelableTaskTracker::TaskId id = |
| 116 tracker->NewTrackedTaskId(&is_canceled); | 151 tracker->NewTrackedTaskId(&is_canceled); |
| 117 | 152 |
| 118 InternalGetCommandsCallback run_if_not_canceled = | 153 GetCommandsCallback run_if_not_canceled = |
| 119 base::Bind(&RunIfNotCanceled, is_canceled, callback); | 154 base::Bind(&RunIfNotCanceled, is_canceled, callback); |
| 120 | 155 |
| 121 InternalGetCommandsCallback callback_runner = | 156 GetCommandsCallback callback_runner = |
| 122 base::Bind(&PostOrRunInternalGetCommandsCallback, | 157 base::Bind(&PostOrRunInternalGetCommandsCallback, |
| 123 base::MessageLoopProxy::current(), run_if_not_canceled); | 158 base::MessageLoopProxy::current(), run_if_not_canceled); |
| 124 | 159 |
| 125 RunTaskOnBackendThread( | 160 RunTaskOnBackendThread( |
| 126 FROM_HERE, | 161 FROM_HERE, |
| 127 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), | 162 base::Bind(&SessionBackend::ReadLastSessionCommands, backend_, |
| 128 is_canceled, callback_runner)); | 163 is_canceled, callback_runner)); |
| 129 return id; | 164 return id; |
| 130 } | 165 } |
| 131 | 166 |
| 132 void BaseSessionService::RunTaskOnBackendThread( | 167 void BaseSessionService::RunTaskOnBackendThread( |
| 133 const tracked_objects::Location& from_here, | 168 const tracked_objects::Location& from_here, |
| 134 const base::Closure& task) { | 169 const base::Closure& task) { |
| 135 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); | 170 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); |
| 136 if (!pool->IsShutdownInProgress()) { | 171 if (!pool->IsShutdownInProgress()) { |
| 137 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); | 172 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); |
| 138 } else { | 173 } else { |
| 139 // Fall back to executing on the main thread if the sequence | 174 // Fall back to executing on the main thread if the sequence |
| 140 // worker pool has been requested to shutdown (around shutdown | 175 // worker pool has been requested to shutdown (around shutdown |
| 141 // time). | 176 // time). |
| 142 task.Run(); | 177 task.Run(); |
| 143 } | 178 } |
| 144 } | 179 } |
| 180 |
| 181 bool BaseSessionService::ProcessedAnyCommandsForTest() { |
| 182 return !pending_commands().empty(); |
| 183 } |
| 184 |
| 185 bool BaseSessionService::BackendInitializedForTest() { |
| 186 return backend_->inited(); |
| 187 } |
| 188 |
| 189 bool BaseSessionService::ReadLastSessionCommandsForTest( |
| 190 ScopedVector<SessionCommand>* commands) { |
| 191 return backend_->ReadLastSessionCommandsImpl(commands); |
| 192 } |
| OLD | NEW |