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 | 45 // static |
46 const int BaseSessionService::max_persist_navigation_count = 6; | 46 const int BaseSessionService::max_persist_navigation_count = 6; |
47 | 47 |
48 BaseSessionService::BaseSessionService( | 48 BaseSessionService::BaseSessionService( |
49 SessionType type, | 49 SessionType type, |
50 const base::FilePath& path, | 50 const base::FilePath& path, |
51 scoped_ptr<BaseSessionServiceDelegate> delegate) | 51 BaseSessionServiceDelegate* delegate) |
52 : pending_reset_(false), | 52 : pending_reset_(false), |
53 commands_since_reset_(0), | 53 commands_since_reset_(0), |
54 delegate_(delegate.Pass()), | 54 delegate_(delegate), |
55 sequence_token_(delegate_->GetBlockingPool()->GetSequenceToken()), | 55 sequence_token_(delegate_->GetBlockingPool()->GetSequenceToken()), |
56 weak_factory_(this) { | 56 weak_factory_(this) { |
57 backend_ = new SessionBackend(type, path); | 57 backend_ = new SessionBackend(type, path); |
58 DCHECK(backend_.get()); | 58 DCHECK(backend_.get()); |
59 } | 59 } |
60 | 60 |
61 BaseSessionService::~BaseSessionService() { | 61 BaseSessionService::~BaseSessionService() {} |
62 | |
63 void BaseSessionService::MoveCurrentSessionToLastSession() { | |
64 SaveNow(); | |
65 RunTaskOnBackendThread( | |
66 FROM_HERE, base::Bind(&SessionBackend::MoveCurrentSessionToLastSession, | |
67 backend())); | |
62 } | 68 } |
63 | 69 |
64 void BaseSessionService::DeleteLastSession() { | 70 void BaseSessionService::DeleteLastSession() { |
65 RunTaskOnBackendThread( | 71 RunTaskOnBackendThread( |
66 FROM_HERE, | 72 FROM_HERE, |
67 base::Bind(&SessionBackend::DeleteLastSession, backend())); | 73 base::Bind(&SessionBackend::DeleteLastSession, backend())); |
68 } | 74 } |
69 | 75 |
70 void BaseSessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) { | 76 void BaseSessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) { |
71 DCHECK(command); | 77 DCHECK(command); |
72 commands_since_reset_++; | 78 commands_since_reset_++; |
73 pending_commands_.push_back(command.release()); | 79 pending_commands_.push_back(command.release()); |
74 StartSaveTimer(); | 80 StartSaveTimer(); |
75 } | 81 } |
76 | 82 |
77 void BaseSessionService::StartSaveTimer() { | 83 void BaseSessionService::StartSaveTimer() { |
78 // Don't start a timer when testing. | 84 // Don't start a timer when testing. |
79 if (delegate_->ShouldUseDelayedSave() && base::MessageLoop::current() && | 85 if (delegate_->ShouldUseDelayedSave() && base::MessageLoop::current() && |
80 !weak_factory_.HasWeakPtrs()) { | 86 !weak_factory_.HasWeakPtrs()) { |
81 base::MessageLoop::current()->PostDelayedTask( | 87 base::MessageLoop::current()->PostDelayedTask( |
82 FROM_HERE, | 88 FROM_HERE, |
83 base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()), | 89 base::Bind(&BaseSessionService::SaveInternal, |
90 weak_factory_.GetWeakPtr()), | |
84 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); | 91 base::TimeDelta::FromMilliseconds(kSaveDelayMS)); |
85 } | 92 } |
86 } | 93 } |
87 | 94 |
88 void BaseSessionService::Save() { | 95 void BaseSessionService::SaveNow() { |
89 DCHECK(backend()); | 96 if (backend()) |
sky
2014/10/31 22:36:16
Why the check here? backend_ is basically never re
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Main reason for this change was to remove as many
| |
90 | 97 SaveInternal(); |
91 if (pending_commands_.empty()) | |
92 return; | |
93 | |
94 // We create a new ScopedVector which will receive all elements from the | |
95 // current commands. This will also clear the current list. | |
96 RunTaskOnBackendThread( | |
97 FROM_HERE, | |
98 base::Bind(&SessionBackend::AppendCommands, backend(), | |
99 new ScopedVector<SessionCommand>(pending_commands_.Pass()), | |
100 pending_reset_)); | |
101 | |
102 if (pending_reset_) { | |
103 commands_since_reset_ = 0; | |
104 pending_reset_ = false; | |
105 } | |
106 } | |
107 | |
108 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { | |
109 return url.is_valid() && delegate_->ShouldTrackEntry(url); | |
110 } | 98 } |
111 | 99 |
112 base::CancelableTaskTracker::TaskId | 100 base::CancelableTaskTracker::TaskId |
113 BaseSessionService::ScheduleGetLastSessionCommands( | 101 BaseSessionService::ScheduleGetLastSessionCommands( |
114 const InternalGetCommandsCallback& callback, | 102 const GetCommandsCallback& callback, |
115 base::CancelableTaskTracker* tracker) { | 103 base::CancelableTaskTracker* tracker) { |
116 base::CancelableTaskTracker::IsCanceledCallback is_canceled; | 104 base::CancelableTaskTracker::IsCanceledCallback is_canceled; |
117 base::CancelableTaskTracker::TaskId id = | 105 base::CancelableTaskTracker::TaskId id = |
118 tracker->NewTrackedTaskId(&is_canceled); | 106 tracker->NewTrackedTaskId(&is_canceled); |
119 | 107 |
120 InternalGetCommandsCallback run_if_not_canceled = | 108 GetCommandsCallback run_if_not_canceled = |
121 base::Bind(&RunIfNotCanceled, is_canceled, callback); | 109 base::Bind(&RunIfNotCanceled, is_canceled, callback); |
122 | 110 |
123 InternalGetCommandsCallback callback_runner = | 111 GetCommandsCallback callback_runner = |
124 base::Bind(&PostOrRunInternalGetCommandsCallback, | 112 base::Bind(&PostOrRunInternalGetCommandsCallback, |
125 base::MessageLoopProxy::current(), run_if_not_canceled); | 113 base::MessageLoopProxy::current(), run_if_not_canceled); |
126 | 114 |
127 RunTaskOnBackendThread( | 115 RunTaskOnBackendThread( |
128 FROM_HERE, | 116 FROM_HERE, |
129 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), | 117 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), |
130 is_canceled, callback_runner)); | 118 is_canceled, callback_runner)); |
131 return id; | 119 return id; |
132 } | 120 } |
133 | 121 |
122 bool BaseSessionService::ProcessedAnyCommandsForTest() { | |
123 return backend()->inited() || !pending_commands().empty(); | |
sky
2014/10/31 22:36:16
This is racy as inited_ is set on another thread.
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Again - this was part of a test (NoStartupWindowTe
sky
2014/11/03 15:53:22
Tests can make sure to wait for the right events.
Mr4D (OOO till 08-26)
2014/11/03 22:05:50
Done.
| |
124 } | |
125 | |
126 void BaseSessionService::SaveInternal() { | |
127 DCHECK(backend()); | |
sky
2014/10/31 22:36:16
Again, why the dcheck?
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Same reasoning as above. But this is gone now for
sky
2014/11/03 15:53:22
A DCHECK isn't going to stop crashers.
| |
128 | |
129 // Inform the delegate that we will save the commands now, giving it the | |
130 // opportunity to append more commands. | |
131 delegate_->OnWillSaveCommands(); | |
132 | |
133 if (pending_commands_.empty()) | |
134 return; | |
135 | |
136 // We create a new ScopedVector which will receive all elements from the | |
137 // current commands. This will also clear the current list. | |
138 RunTaskOnBackendThread( | |
139 FROM_HERE, | |
140 base::Bind(&SessionBackend::AppendCommands, backend(), | |
141 new ScopedVector<SessionCommand>(pending_commands_.Pass()), | |
142 pending_reset_)); | |
143 | |
144 if (pending_reset_) { | |
145 commands_since_reset_ = 0; | |
146 pending_reset_ = false; | |
147 } | |
148 | |
149 delegate_->OnSavedCommands(); | |
150 } | |
151 | |
134 void BaseSessionService::RunTaskOnBackendThread( | 152 void BaseSessionService::RunTaskOnBackendThread( |
135 const tracked_objects::Location& from_here, | 153 const tracked_objects::Location& from_here, |
136 const base::Closure& task) { | 154 const base::Closure& task) { |
137 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); | 155 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); |
138 if (!pool->IsShutdownInProgress()) { | 156 if (!pool->IsShutdownInProgress()) { |
139 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); | 157 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); |
140 } else { | 158 } else { |
141 // Fall back to executing on the main thread if the sequence | 159 // Fall back to executing on the main thread if the sequence |
142 // worker pool has been requested to shutdown (around shutdown | 160 // worker pool has been requested to shutdown (around shutdown |
143 // time). | 161 // time). |
144 task.Run(); | 162 task.Run(); |
145 } | 163 } |
146 } | 164 } |
OLD | NEW |