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/session_service.h" | 5 #include "chrome/browser/sessions/session_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/memory/scoped_vector.h" | |
17 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
18 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
19 #include "base/pickle.h" | 18 #include "base/pickle.h" |
20 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
21 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
22 #include "chrome/browser/prefs/session_startup_pref.h" | 21 #include "chrome/browser/prefs/session_startup_pref.h" |
23 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/sessions/session_backend.h" | 23 #include "chrome/browser/sessions/session_backend.h" |
25 #include "chrome/browser/sessions/session_command.h" | 24 #include "chrome/browser/sessions/session_command.h" |
26 #include "chrome/browser/sessions/session_restore.h" | 25 #include "chrome/browser/sessions/session_restore.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 static const SessionCommand::id_type kCommandWindowClosed = 17; | 75 static const SessionCommand::id_type kCommandWindowClosed = 17; |
77 static const SessionCommand::id_type kCommandSetTabUserAgentOverride = 18; | 76 static const SessionCommand::id_type kCommandSetTabUserAgentOverride = 18; |
78 static const SessionCommand::id_type kCommandSessionStorageAssociated = 19; | 77 static const SessionCommand::id_type kCommandSessionStorageAssociated = 19; |
79 static const SessionCommand::id_type kCommandSetActiveWindow = 20; | 78 static const SessionCommand::id_type kCommandSetActiveWindow = 20; |
80 | 79 |
81 // Every kWritesPerReset commands triggers recreating the file. | 80 // Every kWritesPerReset commands triggers recreating the file. |
82 static const int kWritesPerReset = 250; | 81 static const int kWritesPerReset = 250; |
83 | 82 |
84 namespace { | 83 namespace { |
85 | 84 |
86 // The callback from GetLastSession is internally routed to SessionService | |
87 // first and then the caller. This is done so that the SessionWindows can be | |
88 // recreated from the SessionCommands and the SessionWindows passed to the | |
89 // caller. The following class is used for this. | |
90 class InternalSessionRequest | |
91 : public BaseSessionService::InternalGetCommandsRequest { | |
92 public: | |
93 InternalSessionRequest( | |
94 const CallbackType& callback, | |
95 const SessionService::SessionCallback& real_callback) | |
96 : BaseSessionService::InternalGetCommandsRequest(callback), | |
97 real_callback(real_callback) { | |
98 } | |
99 | |
100 // The callback supplied to GetLastSession. | |
101 SessionService::SessionCallback real_callback; | |
102 | |
103 private: | |
104 ~InternalSessionRequest() {} | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(InternalSessionRequest); | |
107 }; | |
108 | |
109 // Various payload structures. | 85 // Various payload structures. |
110 struct ClosedPayload { | 86 struct ClosedPayload { |
111 SessionID::id_type id; | 87 SessionID::id_type id; |
112 int64 close_time; | 88 int64 close_time; |
113 }; | 89 }; |
114 | 90 |
115 struct WindowBoundsPayload2 { | 91 struct WindowBoundsPayload2 { |
116 SessionID::id_type window_id; | 92 SessionID::id_type window_id; |
117 int32 x; | 93 int32 x; |
118 int32 y; | 94 int32 y; |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 const SessionID& window_id, | 469 const SessionID& window_id, |
494 const SessionID& tab_id, | 470 const SessionID& tab_id, |
495 const std::string& user_agent_override) { | 471 const std::string& user_agent_override) { |
496 if (!ShouldTrackChangesToWindow(window_id)) | 472 if (!ShouldTrackChangesToWindow(window_id)) |
497 return; | 473 return; |
498 | 474 |
499 ScheduleCommand(CreateSetTabUserAgentOverrideCommand( | 475 ScheduleCommand(CreateSetTabUserAgentOverrideCommand( |
500 kCommandSetTabUserAgentOverride, tab_id.id(), user_agent_override)); | 476 kCommandSetTabUserAgentOverride, tab_id.id(), user_agent_override)); |
501 } | 477 } |
502 | 478 |
503 SessionService::Handle SessionService::GetLastSession( | 479 CancelableTaskTracker::TaskId SessionService::GetLastSession( |
504 CancelableRequestConsumerBase* consumer, | 480 const SessionCallback& callback, |
505 const SessionCallback& callback) { | 481 CancelableTaskTracker* tracker) { |
| 482 // OnGotSessionCommands maps the SessionCommands to browser state, then run |
| 483 // the callback. |
506 return ScheduleGetLastSessionCommands( | 484 return ScheduleGetLastSessionCommands( |
507 new InternalSessionRequest( | 485 base::Bind(&SessionService::OnGotSessionCommands, |
508 base::Bind(&SessionService::OnGotSessionCommands, | 486 base::Unretained(this), callback), |
509 base::Unretained(this)), | 487 tracker); |
510 callback), | |
511 consumer); | |
512 } | 488 } |
513 | 489 |
514 void SessionService::Save() { | 490 void SessionService::Save() { |
515 bool had_commands = !pending_commands().empty(); | 491 bool had_commands = !pending_commands().empty(); |
516 BaseSessionService::Save(); | 492 BaseSessionService::Save(); |
517 if (had_commands) { | 493 if (had_commands) { |
518 RecordSessionUpdateHistogramData(chrome::NOTIFICATION_SESSION_SERVICE_SAVED, | 494 RecordSessionUpdateHistogramData(chrome::NOTIFICATION_SESSION_SERVICE_SAVED, |
519 &last_updated_save_time_); | 495 &last_updated_save_time_); |
520 content::NotificationService::current()->Notify( | 496 content::NotificationService::current()->Notify( |
521 chrome::NOTIFICATION_SESSION_SERVICE_SAVED, | 497 chrome::NOTIFICATION_SESSION_SERVICE_SAVED, |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 const SessionID& window_id) { | 822 const SessionID& window_id) { |
847 ActiveWindowPayload payload = 0; | 823 ActiveWindowPayload payload = 0; |
848 payload = window_id.id(); | 824 payload = window_id.id(); |
849 SessionCommand* command = | 825 SessionCommand* command = |
850 new SessionCommand(kCommandSetActiveWindow, sizeof(payload)); | 826 new SessionCommand(kCommandSetActiveWindow, sizeof(payload)); |
851 memcpy(command->contents(), &payload, sizeof(payload)); | 827 memcpy(command->contents(), &payload, sizeof(payload)); |
852 return command; | 828 return command; |
853 } | 829 } |
854 | 830 |
855 void SessionService::OnGotSessionCommands( | 831 void SessionService::OnGotSessionCommands( |
856 Handle handle, | 832 const SessionCallback& callback, |
857 scoped_refptr<InternalGetCommandsRequest> request) { | 833 ScopedVector<SessionCommand> commands) { |
858 if (request->canceled()) | |
859 return; | |
860 | |
861 ScopedVector<SessionWindow> valid_windows; | 834 ScopedVector<SessionWindow> valid_windows; |
862 SessionID::id_type active_window_id = 0; | 835 SessionID::id_type active_window_id = 0; |
| 836 |
863 RestoreSessionFromCommands( | 837 RestoreSessionFromCommands( |
864 request->commands, &(valid_windows.get()), &active_window_id); | 838 commands.get(), &valid_windows.get(), &active_window_id); |
865 static_cast<InternalSessionRequest*>(request.get())->real_callback.Run( | 839 callback.Run(valid_windows.Pass(), active_window_id); |
866 request->handle(), &(valid_windows.get()), active_window_id); | |
867 } | 840 } |
868 | 841 |
869 void SessionService::RestoreSessionFromCommands( | 842 void SessionService::RestoreSessionFromCommands( |
870 const std::vector<SessionCommand*>& commands, | 843 const std::vector<SessionCommand*>& commands, |
871 std::vector<SessionWindow*>* valid_windows, | 844 std::vector<SessionWindow*>* valid_windows, |
872 SessionID::id_type* active_window_id) { | 845 SessionID::id_type* active_window_id) { |
873 std::map<int, SessionTab*> tabs; | 846 std::map<int, SessionTab*> tabs; |
874 std::map<int, SessionWindow*> windows; | 847 std::map<int, SessionWindow*> windows; |
875 | 848 |
876 VLOG(1) << "RestoreSessionFromCommands " << commands.size(); | 849 VLOG(1) << "RestoreSessionFromCommands " << commands.size(); |
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1776 contents->GetController().GetDefaultSessionStorageNamespace(); | 1749 contents->GetController().GetDefaultSessionStorageNamespace(); |
1777 session_storage_namespace->SetShouldPersist(false); | 1750 session_storage_namespace->SetShouldPersist(false); |
1778 SessionTabHelper* session_tab_helper = | 1751 SessionTabHelper* session_tab_helper = |
1779 SessionTabHelper::FromWebContents(contents); | 1752 SessionTabHelper::FromWebContents(contents); |
1780 TabClosed(session_tab_helper->window_id(), | 1753 TabClosed(session_tab_helper->window_id(), |
1781 session_tab_helper->session_id(), | 1754 session_tab_helper->session_id(), |
1782 contents->GetClosedByUserGesture()); | 1755 contents->GetClosedByUserGesture()); |
1783 RecordSessionUpdateHistogramData(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 1756 RecordSessionUpdateHistogramData(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
1784 &last_updated_tab_closed_time_); | 1757 &last_updated_tab_closed_time_); |
1785 } | 1758 } |
OLD | NEW |