OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/pickle.h" | 8 #include "base/pickle.h" |
8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sessions/session_backend.h" | 13 #include "chrome/browser/sessions/session_backend.h" |
13 #include "chrome/browser/sessions/session_types.h" | 14 #include "chrome/browser/sessions/session_types.h" |
14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
15 #include "content/browser/tab_contents/navigation_entry.h" | 16 #include "content/browser/tab_contents/navigation_entry.h" |
16 #include "webkit/glue/webkit_glue.h" | 17 #include "webkit/glue/webkit_glue.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 60 |
60 // static | 61 // static |
61 const int BaseSessionService::max_persist_navigation_count = 6; | 62 const int BaseSessionService::max_persist_navigation_count = 6; |
62 | 63 |
63 BaseSessionService::BaseSessionService(SessionType type, | 64 BaseSessionService::BaseSessionService(SessionType type, |
64 Profile* profile, | 65 Profile* profile, |
65 const FilePath& path) | 66 const FilePath& path) |
66 : profile_(profile), | 67 : profile_(profile), |
67 path_(path), | 68 path_(path), |
68 backend_thread_(NULL), | 69 backend_thread_(NULL), |
69 ALLOW_THIS_IN_INITIALIZER_LIST(save_factory_(this)), | 70 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
70 pending_reset_(false), | 71 pending_reset_(false), |
71 commands_since_reset_(0) { | 72 commands_since_reset_(0) { |
72 if (profile) { | 73 if (profile) { |
73 // We should never be created when incognito. | 74 // We should never be created when incognito. |
74 DCHECK(!profile->IsOffTheRecord()); | 75 DCHECK(!profile->IsOffTheRecord()); |
75 } | 76 } |
76 backend_ = new SessionBackend(type, | 77 backend_ = new SessionBackend(type, |
77 profile_ ? profile_->GetPath() : path_); | 78 profile_ ? profile_->GetPath() : path_); |
78 DCHECK(backend_.get()); | 79 DCHECK(backend_.get()); |
79 backend_thread_ = g_browser_process->file_thread(); | 80 backend_thread_ = g_browser_process->file_thread(); |
80 if (!backend_thread_) | 81 if (!backend_thread_) |
81 backend_->Init(); | 82 backend_->Init(); |
82 // If backend_thread is non-null, backend will init itself as appropriate. | 83 // If backend_thread is non-null, backend will init itself as appropriate. |
83 } | 84 } |
84 | 85 |
85 BaseSessionService::~BaseSessionService() { | 86 BaseSessionService::~BaseSessionService() { |
86 } | 87 } |
87 | 88 |
88 void BaseSessionService::DeleteLastSession() { | 89 void BaseSessionService::DeleteLastSession() { |
89 if (!backend_thread()) { | 90 if (!backend_thread()) { |
90 backend()->DeleteLastSession(); | 91 backend()->DeleteLastSession(); |
91 } else { | 92 } else { |
92 backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 93 backend_thread()->message_loop()->PostTask( |
93 backend(), &SessionBackend::DeleteLastSession)); | 94 FROM_HERE, base::Bind(&SessionBackend::DeleteLastSession, backend())); |
94 } | 95 } |
95 } | 96 } |
96 | 97 |
97 void BaseSessionService::ScheduleCommand(SessionCommand* command) { | 98 void BaseSessionService::ScheduleCommand(SessionCommand* command) { |
98 DCHECK(command); | 99 DCHECK(command); |
99 commands_since_reset_++; | 100 commands_since_reset_++; |
100 pending_commands_.push_back(command); | 101 pending_commands_.push_back(command); |
101 StartSaveTimer(); | 102 StartSaveTimer(); |
102 } | 103 } |
103 | 104 |
104 void BaseSessionService::StartSaveTimer() { | 105 void BaseSessionService::StartSaveTimer() { |
105 // Don't start a timer when testing (profile == NULL or | 106 // Don't start a timer when testing (profile == NULL or |
106 // MessageLoop::current() is NULL). | 107 // MessageLoop::current() is NULL). |
107 if (MessageLoop::current() && profile() && save_factory_.empty()) { | 108 if (MessageLoop::current() && profile() && !weak_factory_.HasWeakPtrs()) { |
108 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 109 MessageLoop::current()->PostDelayedTask( |
109 save_factory_.NewRunnableMethod(&BaseSessionService::Save), | 110 FROM_HERE, |
| 111 base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()), |
110 kSaveDelayMS); | 112 kSaveDelayMS); |
111 } | 113 } |
112 } | 114 } |
113 | 115 |
114 void BaseSessionService::Save() { | 116 void BaseSessionService::Save() { |
115 DCHECK(backend()); | 117 DCHECK(backend()); |
116 | 118 |
117 if (pending_commands_.empty()) | 119 if (pending_commands_.empty()) |
118 return; | 120 return; |
119 | 121 |
120 if (!backend_thread()) { | 122 if (!backend_thread()) { |
121 backend()->AppendCommands( | 123 backend()->AppendCommands( |
122 new std::vector<SessionCommand*>(pending_commands_), pending_reset_); | 124 new std::vector<SessionCommand*>(pending_commands_), pending_reset_); |
123 } else { | 125 } else { |
124 backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 126 backend_thread()->message_loop()->PostTask( |
125 backend(), &SessionBackend::AppendCommands, | 127 FROM_HERE, |
126 new std::vector<SessionCommand*>(pending_commands_), | 128 base::Bind(&SessionBackend::AppendCommands, backend(), |
127 pending_reset_)); | 129 new std::vector<SessionCommand*>(pending_commands_), |
| 130 pending_reset_)); |
128 } | 131 } |
129 // Backend took ownership of commands. | 132 // Backend took ownership of commands. |
130 pending_commands_.clear(); | 133 pending_commands_.clear(); |
131 | 134 |
132 if (pending_reset_) { | 135 if (pending_reset_) { |
133 commands_since_reset_ = 0; | 136 commands_since_reset_ = 0; |
134 pending_reset_ = false; | 137 pending_reset_ = false; |
135 } | 138 } |
136 } | 139 } |
137 | 140 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // just display a non-functional print preview page. | 255 // just display a non-functional print preview page. |
253 return url.is_valid() && url != GURL(chrome::kChromeUIPrintURL); | 256 return url.is_valid() && url != GURL(chrome::kChromeUIPrintURL); |
254 } | 257 } |
255 | 258 |
256 BaseSessionService::Handle BaseSessionService::ScheduleGetLastSessionCommands( | 259 BaseSessionService::Handle BaseSessionService::ScheduleGetLastSessionCommands( |
257 InternalGetCommandsRequest* request, | 260 InternalGetCommandsRequest* request, |
258 CancelableRequestConsumerBase* consumer) { | 261 CancelableRequestConsumerBase* consumer) { |
259 scoped_refptr<InternalGetCommandsRequest> request_wrapper(request); | 262 scoped_refptr<InternalGetCommandsRequest> request_wrapper(request); |
260 AddRequest(request, consumer); | 263 AddRequest(request, consumer); |
261 if (backend_thread()) { | 264 if (backend_thread()) { |
262 backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 265 backend_thread()->message_loop()->PostTask( |
263 backend(), &SessionBackend::ReadLastSessionCommands, request_wrapper)); | 266 FROM_HERE, |
| 267 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), |
| 268 request_wrapper)); |
264 } else { | 269 } else { |
265 backend()->ReadLastSessionCommands(request); | 270 backend()->ReadLastSessionCommands(request); |
266 } | 271 } |
267 return request->handle(); | 272 return request->handle(); |
268 } | 273 } |
269 | 274 |
270 BaseSessionService::Handle | 275 BaseSessionService::Handle |
271 BaseSessionService::ScheduleGetCurrentSessionCommands( | 276 BaseSessionService::ScheduleGetCurrentSessionCommands( |
272 InternalGetCommandsRequest* request, | 277 InternalGetCommandsRequest* request, |
273 CancelableRequestConsumerBase* consumer) { | 278 CancelableRequestConsumerBase* consumer) { |
274 scoped_refptr<InternalGetCommandsRequest> request_wrapper(request); | 279 scoped_refptr<InternalGetCommandsRequest> request_wrapper(request); |
275 AddRequest(request, consumer); | 280 AddRequest(request, consumer); |
276 if (backend_thread()) { | 281 if (backend_thread()) { |
277 backend_thread()->message_loop()->PostTask(FROM_HERE, | 282 backend_thread()->message_loop()->PostTask( |
278 NewRunnableMethod(backend(), | 283 FROM_HERE, |
279 &SessionBackend::ReadCurrentSessionCommands, | 284 base::Bind(&SessionBackend::ReadCurrentSessionCommands, backend(), |
280 request_wrapper)); | 285 request_wrapper)); |
281 } else { | 286 } else { |
282 backend()->ReadCurrentSessionCommands(request); | 287 backend()->ReadCurrentSessionCommands(request); |
283 } | 288 } |
284 return request->handle(); | 289 return request->handle(); |
285 } | 290 } |
OLD | NEW |