1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#include "chrome/browser/sessions/session_service.h"
#include <limits>
#include "base/callback.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/pickle.h"
#include "base/scoped_vector.h"
#include "base/thread.h"
#include "chrome/browser/browser_init.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/sessions/session_backend.h"
#include "chrome/browser/sessions/session_command.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/session_types.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#if defined(OS_MACOSX)
#include "chrome/browser/app_controller_cppsafe_mac.h"
#endif
using base::Time;
// Identifier for commands written to file.
static const SessionCommand::id_type kCommandSetTabWindow = 0;
// kCommandSetWindowBounds is no longer used (it's superseded by
// kCommandSetWindowBounds2). I leave it here to document what it was.
// static const SessionCommand::id_type kCommandSetWindowBounds = 1;
static const SessionCommand::id_type kCommandSetTabIndexInWindow = 2;
static const SessionCommand::id_type kCommandTabClosed = 3;
static const SessionCommand::id_type kCommandWindowClosed = 4;
static const SessionCommand::id_type
kCommandTabNavigationPathPrunedFromBack = 5;
static const SessionCommand::id_type kCommandUpdateTabNavigation = 6;
static const SessionCommand::id_type kCommandSetSelectedNavigationIndex = 7;
static const SessionCommand::id_type kCommandSetSelectedTabInIndex = 8;
static const SessionCommand::id_type kCommandSetWindowType = 9;
static const SessionCommand::id_type kCommandSetWindowBounds2 = 10;
static const SessionCommand::id_type
kCommandTabNavigationPathPrunedFromFront = 11;
static const SessionCommand::id_type kCommandSetPinnedState = 12;
static const SessionCommand::id_type kCommandSetExtensionAppID = 13;
// Every kWritesPerReset commands triggers recreating the file.
static const int kWritesPerReset = 250;
namespace {
// The callback from GetLastSession is internally routed to SessionService
// first and then the caller. This is done so that the SessionWindows can be
// recreated from the SessionCommands and the SessionWindows passed to the
// caller. The following class is used for this.
class InternalSessionRequest
: public BaseSessionService::InternalGetCommandsRequest {
public:
InternalSessionRequest(
CallbackType* callback,
SessionService::SessionCallback* real_callback)
: BaseSessionService::InternalGetCommandsRequest(callback),
real_callback(real_callback) {
}
// The callback supplied to GetLastSession and GetCurrentSession.
scoped_ptr<SessionService::SessionCallback> real_callback;
private:
~InternalSessionRequest() {}
DISALLOW_COPY_AND_ASSIGN(InternalSessionRequest);
};
// Various payload structures.
struct ClosedPayload {
SessionID::id_type id;
int64 close_time;
};
struct WindowBoundsPayload2 {
SessionID::id_type window_id;
int32 x;
int32 y;
int32 w;
int32 h;
bool is_maximized;
};
struct IDAndIndexPayload {
SessionID::id_type id;
int32 index;
};
typedef IDAndIndexPayload TabIndexInWindowPayload;
typedef IDAndIndexPayload TabNavigationPathPrunedFromBackPayload;
typedef IDAndIndexPayload SelectedNavigationIndexPayload;
typedef IDAndIndexPayload SelectedTabInIndexPayload;
typedef IDAndIndexPayload WindowTypePayload;
typedef IDAndIndexPayload TabNavigationPathPrunedFromFrontPayload;
struct PinnedStatePayload {
SessionID::id_type tab_id;
bool pinned_state;
};
} // namespace
// SessionService -------------------------------------------------------------
SessionService::SessionService(Profile* profile)
: BaseSessionService(SESSION_RESTORE, profile, FilePath()),
has_open_trackable_browsers_(false),
move_on_new_browser_(false),
save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
save_delay_in_hrs_(base::TimeDelta::FromHours(8)) {
Init();
}
SessionService::SessionService(const FilePath& save_path)
: BaseSessionService(SESSION_RESTORE, NULL, save_path),
has_open_trackable_browsers_(false),
move_on_new_browser_(false),
save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
save_delay_in_hrs_(base::TimeDelta::FromHours(8)) {
Init();
}
SessionService::~SessionService() {
Save();
}
bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open) {
return RestoreIfNecessary(urls_to_open, NULL);
}
void SessionService::ResetFromCurrentBrowsers() {
ScheduleReset();
}
void SessionService::MoveCurrentSessionToLastSession() {
pending_tab_close_ids_.clear();
window_closing_ids_.clear();
pending_window_close_ids_.clear();
Save();
if (!backend_thread()) {
backend()->MoveCurrentSessionToLastSession();
} else {
backend_thread()->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
backend(), &SessionBackend::MoveCurrentSessionToLastSession));
}
}
void SessionService::SetTabWindow(const SessionID& window_id,
const SessionID& tab_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreateSetTabWindowCommand(window_id, tab_id));
}
void SessionService::SetWindowBounds(const SessionID& window_id,
const gfx::Rect& bounds,
bool is_maximized) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreateSetWindowBoundsCommand(window_id, bounds,
is_maximized));
}
void SessionService::SetTabIndexInWindow(const SessionID& window_id,
const SessionID& tab_id,
int new_index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreateSetTabIndexInWindowCommand(tab_id, new_index));
}
void SessionService::SetPinnedState(const SessionID& window_id,
const SessionID& tab_id,
bool is_pinned) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreatePinnedStateCommand(tab_id, is_pinned));
}
void SessionService::TabClosed(const SessionID& window_id,
const SessionID& tab_id,
bool closed_by_user_gesture) {
if (!tab_id.id())
return; // Hapens when the tab is replaced.
if (!ShouldTrackChangesToWindow(window_id))
return;
IdToRange::iterator i = tab_to_available_range_.find(tab_id.id());
if (i != tab_to_available_range_.end())
tab_to_available_range_.erase(i);
if (find(pending_window_close_ids_.begin(), pending_window_close_ids_.end(),
window_id.id()) != pending_window_close_ids_.end()) {
// Tab is in last window. Don't commit it immediately, instead add it to the
// list of tabs to close. If the user creates another window, the close is
// committed.
pending_tab_close_ids_.insert(tab_id.id());
} else if (find(window_closing_ids_.begin(), window_closing_ids_.end(),
window_id.id()) != window_closing_ids_.end() ||
!IsOnlyOneTabLeft() ||
closed_by_user_gesture) {
// Close is the result of one of the following:
// . window close (and it isn't the last window).
// . closing a tab and there are other windows/tabs open.
// . closed by a user gesture.
// In all cases we need to mark the tab as explicitly closed.
ScheduleCommand(CreateTabClosedCommand(tab_id.id()));
} else {
// User closed the last tab in the last tabbed browser. Don't mark the
// tab closed.
pending_tab_close_ids_.insert(tab_id.id());
has_open_trackable_browsers_ = false;
}
}
void SessionService::WindowClosing(const SessionID& window_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// The window is about to close. If there are other tabbed browsers with the
// same original profile commit the close immediately.
//
// NOTE: if the user chooses the exit menu item session service is destroyed
// and this code isn't hit.
if (has_open_trackable_browsers_) {
// Closing a window can never make has_open_trackable_browsers_ go from
// false to true, so only update it if already true.
has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id);
}
if (should_record_close_as_pending())
pending_window_close_ids_.insert(window_id.id());
else
window_closing_ids_.insert(window_id.id());
}
void SessionService::WindowClosed(const SessionID& window_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
windows_tracking_.erase(window_id.id());
if (window_closing_ids_.find(window_id.id()) != window_closing_ids_.end()) {
window_closing_ids_.erase(window_id.id());
ScheduleCommand(CreateWindowClosedCommand(window_id.id()));
} else if (pending_window_close_ids_.find(window_id.id()) ==
pending_window_close_ids_.end()) {
// We'll hit this if user closed the last tab in a window.
has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id);
if (should_record_close_as_pending())
pending_window_close_ids_.insert(window_id.id());
else
ScheduleCommand(CreateWindowClosedCommand(window_id.id()));
}
}
void SessionService::SetWindowType(const SessionID& window_id,
Browser::Type type) {
if (!should_track_changes_for_browser_type(type))
return;
windows_tracking_.insert(window_id.id());
// The user created a new tabbed browser with our profile. Commit any
// pending closes.
CommitPendingCloses();
has_open_trackable_browsers_ = true;
move_on_new_browser_ = true;
ScheduleCommand(
CreateSetWindowTypeCommand(window_id, WindowTypeForBrowserType(type)));
}
void SessionService::TabNavigationPathPrunedFromBack(const SessionID& window_id,
const SessionID& tab_id,
int count) {
if (!ShouldTrackChangesToWindow(window_id))
return;
TabNavigationPathPrunedFromBackPayload payload = { 0 };
payload.id = tab_id.id();
payload.index = count;
SessionCommand* command =
new SessionCommand(kCommandTabNavigationPathPrunedFromBack,
sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
ScheduleCommand(command);
}
void SessionService::TabNavigationPathPrunedFromFront(
const SessionID& window_id,
const SessionID& tab_id,
int count) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// Update the range of indices.
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id.id()];
range.first = std::max(0, range.first - count);
range.second = std::max(0, range.second - count);
}
TabNavigationPathPrunedFromFrontPayload payload = { 0 };
payload.id = tab_id.id();
payload.index = count;
SessionCommand* command =
new SessionCommand(kCommandTabNavigationPathPrunedFromFront,
sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
ScheduleCommand(command);
}
void SessionService::UpdateTabNavigation(const SessionID& window_id,
const SessionID& tab_id,
int index,
const NavigationEntry& entry) {
if (!ShouldTrackEntry(entry) || !ShouldTrackChangesToWindow(window_id))
return;
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id.id()];
range.first = std::min(index, range.first);
range.second = std::max(index, range.second);
}
ScheduleCommand(CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation,
tab_id.id(), index, entry));
}
void SessionService::TabRestored(NavigationController* controller,
bool pinned) {
if (!ShouldTrackChangesToWindow(controller->window_id()))
return;
BuildCommandsForTab(controller->window_id(), controller, -1,
pinned, &pending_commands(), NULL);
StartSaveTimer();
}
void SessionService::SetSelectedNavigationIndex(const SessionID& window_id,
const SessionID& tab_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
if (index < tab_to_available_range_[tab_id.id()].first ||
index > tab_to_available_range_[tab_id.id()].second) {
// The new index is outside the range of what we've archived, schedule
// a reset.
ResetFromCurrentBrowsers();
return;
}
}
ScheduleCommand(CreateSetSelectedNavigationIndexCommand(tab_id, index));
}
void SessionService::SetSelectedTabInWindow(const SessionID& window_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreateSetSelectedTabInWindow(window_id, index));
}
SessionService::Handle SessionService::GetLastSession(
CancelableRequestConsumerBase* consumer,
SessionCallback* callback) {
return ScheduleGetLastSessionCommands(
new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
callback), consumer);
}
SessionService::Handle SessionService::GetCurrentSession(
CancelableRequestConsumerBase* consumer,
SessionCallback* callback) {
if (pending_window_close_ids_.empty()) {
// If there are no pending window closes, we can get the current session
// from memory.
scoped_refptr<InternalSessionRequest> request = new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
callback);
AddRequest(request, consumer);
IdToRange tab_to_available_range;
std::set<SessionID::id_type> windows_to_track;
BuildCommandsFromBrowsers(&(request->commands),
&tab_to_available_range,
&windows_to_track);
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
return request->handle();
} else {
// If there are pending window closes, read the current session from disk.
return ScheduleGetCurrentSessionCommands(
new InternalSessionRequest(
NewCallback(this, &SessionService::OnGotSessionCommands),
callback), consumer);
}
}
void SessionService::Save() {
bool had_commands = !pending_commands().empty();
BaseSessionService::Save();
if (had_commands) {
RecordSessionUpdateHistogramData(NotificationType::SESSION_SERVICE_SAVED,
&last_updated_save_time_);
NotificationService::current()->Notify(
NotificationType::SESSION_SERVICE_SAVED,
Source<Profile>(profile()),
NotificationService::NoDetails());
}
}
void SessionService::Init() {
// Register for the notifications we're interested in.
registrar_.Add(this, NotificationType::TAB_PARENTED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::TAB_CLOSED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::NAV_LIST_PRUNED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::NAV_ENTRY_CHANGED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::BROWSER_OPENED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
NotificationService::AllSources());
}
bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open,
Browser* browser) {
if (!has_open_trackable_browsers_ && !BrowserInit::InProcessStartup() &&
!SessionRestore::IsRestoring()
#if defined(OS_MACOSX)
// OSX has a fairly different idea of application lifetime than the
// other platforms. We need to check that we aren't opening a window
// from the dock or the menubar.
&& !app_controller_mac::IsOpeningNewWindow()
#endif
) {
// We're going from no tabbed browsers to a tabbed browser (and not in
// process startup), restore the last session.
if (move_on_new_browser_) {
// Make the current session the last.
MoveCurrentSessionToLastSession();
move_on_new_browser_ = false;
}
SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile());
if (pref.type == SessionStartupPref::LAST) {
SessionRestore::RestoreSession(
profile(), browser, false, browser ? false : true, urls_to_open);
return true;
}
}
return false;
}
void SessionService::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
// All of our messages have the NavigationController as the source.
switch (type.value) {
case NotificationType::BROWSER_OPENED: {
Browser* browser = Source<Browser>(source).ptr();
if (browser->profile() != profile() ||
!should_track_changes_for_browser_type(browser->type())) {
return;
}
RestoreIfNecessary(std::vector<GURL>(), browser);
SetWindowType(browser->session_id(), browser->type());
break;
}
case NotificationType::TAB_PARENTED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
SetTabWindow(controller->window_id(), controller->session_id());
if (controller->tab_contents()->extension_app()) {
SetTabExtensionAppID(
controller->window_id(),
controller->session_id(),
controller->tab_contents()->extension_app()->id());
}
break;
}
case NotificationType::TAB_CLOSED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
TabClosed(controller->window_id(), controller->session_id(),
controller->tab_contents()->closed_by_user_gesture());
RecordSessionUpdateHistogramData(NotificationType::TAB_CLOSED,
&last_updated_tab_closed_time_);
break;
}
case NotificationType::NAV_LIST_PRUNED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
Details<NavigationController::PrunedDetails> pruned_details(details);
if (pruned_details->from_front) {
TabNavigationPathPrunedFromFront(controller->window_id(),
controller->session_id(),
pruned_details->count);
} else {
TabNavigationPathPrunedFromBack(controller->window_id(),
controller->session_id(),
controller->entry_count());
}
RecordSessionUpdateHistogramData(NotificationType::NAV_LIST_PRUNED,
&last_updated_nav_list_pruned_time_);
break;
}
case NotificationType::NAV_ENTRY_CHANGED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
Details<NavigationController::EntryChangedDetails> changed(details);
UpdateTabNavigation(controller->window_id(), controller->session_id(),
changed->index, *changed->changed_entry);
break;
}
case NotificationType::NAV_ENTRY_COMMITTED: {
NavigationController* controller =
Source<NavigationController>(source).ptr();
int current_entry_index = controller->GetCurrentEntryIndex();
SetSelectedNavigationIndex(controller->window_id(),
controller->session_id(),
current_entry_index);
UpdateTabNavigation(controller->window_id(), controller->session_id(),
current_entry_index,
*controller->GetEntryAtIndex(current_entry_index));
Details<NavigationController::LoadCommittedDetails> changed(details);
if (changed->type == NavigationType::NEW_PAGE ||
changed->type == NavigationType::EXISTING_PAGE) {
RecordSessionUpdateHistogramData(NotificationType::NAV_ENTRY_COMMITTED,
&last_updated_nav_entry_commit_time_);
}
break;
}
case NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED: {
TabContents* tab_contents = Source<TabContents>(source).ptr();
DCHECK(tab_contents);
if (tab_contents->extension_app()) {
SetTabExtensionAppID(tab_contents->controller().window_id(),
tab_contents->controller().session_id(),
tab_contents->extension_app()->id());
}
break;
}
default:
NOTREACHED();
}
}
void SessionService::SetTabExtensionAppID(
const SessionID& window_id,
const SessionID& tab_id,
const std::string& extension_app_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(CreateSetTabExtensionAppIDCommand(
kCommandSetExtensionAppID,
tab_id.id(),
extension_app_id));
}
SessionCommand* SessionService::CreateSetSelectedTabInWindow(
const SessionID& window_id,
int index) {
SelectedTabInIndexPayload payload = { 0 };
payload.id = window_id.id();
payload.index = index;
SessionCommand* command = new SessionCommand(kCommandSetSelectedTabInIndex,
sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateSetTabWindowCommand(
const SessionID& window_id,
const SessionID& tab_id) {
SessionID::id_type payload[] = { window_id.id(), tab_id.id() };
SessionCommand* command =
new SessionCommand(kCommandSetTabWindow, sizeof(payload));
memcpy(command->contents(), payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateSetWindowBoundsCommand(
const SessionID& window_id,
const gfx::Rect& bounds,
bool is_maximized) {
WindowBoundsPayload2 payload = { 0 };
payload.window_id = window_id.id();
payload.x = bounds.x();
payload.y = bounds.y();
payload.w = bounds.width();
payload.h = bounds.height();
payload.is_maximized = is_maximized;
SessionCommand* command = new SessionCommand(kCommandSetWindowBounds2,
sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateSetTabIndexInWindowCommand(
const SessionID& tab_id,
int new_index) {
TabIndexInWindowPayload payload = { 0 };
payload.id = tab_id.id();
payload.index = new_index;
SessionCommand* command =
new SessionCommand(kCommandSetTabIndexInWindow, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateTabClosedCommand(
const SessionID::id_type tab_id) {
ClosedPayload payload;
// Because of what appears to be a compiler bug setting payload to {0} doesn't
// set the padding to 0, resulting in Purify reporting an UMR when we write
// the structure to disk. To avoid this we explicitly memset the struct.
memset(&payload, 0, sizeof(payload));
payload.id = tab_id;
payload.close_time = Time::Now().ToInternalValue();
SessionCommand* command =
new SessionCommand(kCommandTabClosed, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateWindowClosedCommand(
const SessionID::id_type window_id) {
ClosedPayload payload;
// See comment in CreateTabClosedCommand as to why we do this.
memset(&payload, 0, sizeof(payload));
payload.id = window_id;
payload.close_time = Time::Now().ToInternalValue();
SessionCommand* command =
new SessionCommand(kCommandWindowClosed, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateSetSelectedNavigationIndexCommand(
const SessionID& tab_id,
int index) {
SelectedNavigationIndexPayload payload = { 0 };
payload.id = tab_id.id();
payload.index = index;
SessionCommand* command = new SessionCommand(
kCommandSetSelectedNavigationIndex, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreateSetWindowTypeCommand(
const SessionID& window_id,
WindowType type) {
WindowTypePayload payload = { 0 };
payload.id = window_id.id();
payload.index = static_cast<int32>(type);
SessionCommand* command = new SessionCommand(
kCommandSetWindowType, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
SessionCommand* SessionService::CreatePinnedStateCommand(
const SessionID& tab_id,
bool is_pinned) {
PinnedStatePayload payload = { 0 };
payload.tab_id = tab_id.id();
payload.pinned_state = is_pinned;
SessionCommand* command =
new SessionCommand(kCommandSetPinnedState, sizeof(payload));
memcpy(command->contents(), &payload, sizeof(payload));
return command;
}
void SessionService::OnGotSessionCommands(
Handle handle,
scoped_refptr<InternalGetCommandsRequest> request) {
if (request->canceled())
return;
ScopedVector<SessionWindow> valid_windows;
RestoreSessionFromCommands(
request->commands, &(valid_windows.get()));
static_cast<InternalSessionRequest*>(request.get())->
real_callback->RunWithParams(
SessionCallback::TupleType(request->handle(),
&(valid_windows.get())));
}
void SessionService::RestoreSessionFromCommands(
const std::vector<SessionCommand*>& commands,
std::vector<SessionWindow*>* valid_windows) {
std::map<int, SessionTab*> tabs;
std::map<int, SessionWindow*> windows;
if (CreateTabsAndWindows(commands, &tabs, &windows)) {
AddTabsToWindows(&tabs, &windows);
SortTabsBasedOnVisualOrderAndPrune(&windows, valid_windows);
UpdateSelectedTabIndex(valid_windows);
}
STLDeleteValues(&tabs);
// Don't delete conents of windows, that is done by the caller as all
// valid windows are added to valid_windows.
}
void SessionService::UpdateSelectedTabIndex(
std::vector<SessionWindow*>* windows) {
for (std::vector<SessionWindow*>::const_iterator i = windows->begin();
i != windows->end(); ++i) {
// See note in SessionWindow as to why we do this.
int new_index = 0;
for (std::vector<SessionTab*>::const_iterator j = (*i)->tabs.begin();
j != (*i)->tabs.end(); ++j) {
if ((*j)->tab_visual_index == (*i)->selected_tab_index) {
new_index = static_cast<int>(j - (*i)->tabs.begin());
break;
}
}
(*i)->selected_tab_index = new_index;
}
}
SessionWindow* SessionService::GetWindow(
SessionID::id_type window_id,
IdToSessionWindow* windows) {
std::map<int, SessionWindow*>::iterator i = windows->find(window_id);
if (i == windows->end()) {
SessionWindow* window = new SessionWindow();
window->window_id.set_id(window_id);
(*windows)[window_id] = window;
return window;
}
return i->second;
}
SessionTab* SessionService::GetTab(
SessionID::id_type tab_id,
IdToSessionTab* tabs) {
DCHECK(tabs);
std::map<int, SessionTab*>::iterator i = tabs->find(tab_id);
if (i == tabs->end()) {
SessionTab* tab = new SessionTab();
tab->tab_id.set_id(tab_id);
(*tabs)[tab_id] = tab;
return tab;
}
return i->second;
}
std::vector<TabNavigation>::iterator
SessionService::FindClosestNavigationWithIndex(
std::vector<TabNavigation>* navigations,
int index) {
DCHECK(navigations);
for (std::vector<TabNavigation>::iterator i = navigations->begin();
i != navigations->end(); ++i) {
if (i->index() >= index)
return i;
}
return navigations->end();
}
// Function used in sorting windows. Sorting is done based on window id. As
// window ids increment for each new window, this effectively sorts by creation
// time.
static bool WindowOrderSortFunction(const SessionWindow* w1,
const SessionWindow* w2) {
return w1->window_id.id() < w2->window_id.id();
}
// Compares the two tabs based on visual index.
static bool TabVisualIndexSortFunction(const SessionTab* t1,
const SessionTab* t2) {
const int delta = t1->tab_visual_index - t2->tab_visual_index;
return delta == 0 ? (t1->tab_id.id() < t2->tab_id.id()) : (delta < 0);
}
void SessionService::SortTabsBasedOnVisualOrderAndPrune(
std::map<int, SessionWindow*>* windows,
std::vector<SessionWindow*>* valid_windows) {
std::map<int, SessionWindow*>::iterator i = windows->begin();
while (i != windows->end()) {
if (i->second->tabs.empty() || i->second->is_constrained ||
!should_track_changes_for_browser_type(
static_cast<Browser::Type>(i->second->type))) {
delete i->second;
windows->erase(i++);
} else {
// Valid window; sort the tabs and add it to the list of valid windows.
std::sort(i->second->tabs.begin(), i->second->tabs.end(),
&TabVisualIndexSortFunction);
// Add the window such that older windows appear first.
if (valid_windows->empty()) {
valid_windows->push_back(i->second);
} else {
valid_windows->insert(
std::upper_bound(valid_windows->begin(), valid_windows->end(),
i->second, &WindowOrderSortFunction),
i->second);
}
++i;
}
}
}
void SessionService::AddTabsToWindows(std::map<int, SessionTab*>* tabs,
std::map<int, SessionWindow*>* windows) {
std::map<int, SessionTab*>::iterator i = tabs->begin();
while (i != tabs->end()) {
SessionTab* tab = i->second;
if (tab->window_id.id() && !tab->navigations.empty()) {
SessionWindow* window = GetWindow(tab->window_id.id(), windows);
window->tabs.push_back(tab);
tabs->erase(i++);
// See note in SessionTab as to why we do this.
std::vector<TabNavigation>::iterator j =
FindClosestNavigationWithIndex(&(tab->navigations),
tab->current_navigation_index);
if (j == tab->navigations.end()) {
tab->current_navigation_index =
static_cast<int>(tab->navigations.size() - 1);
} else {
tab->current_navigation_index =
static_cast<int>(j - tab->navigations.begin());
}
} else {
// Never got a set tab index in window, or tabs are empty, nothing
// to do.
++i;
}
}
}
bool SessionService::CreateTabsAndWindows(
const std::vector<SessionCommand*>& data,
std::map<int, SessionTab*>* tabs,
std::map<int, SessionWindow*>* windows) {
// If the file is corrupt (command with wrong size, or unknown command), we
// still return true and attempt to restore what we we can.
for (std::vector<SessionCommand*>::const_iterator i = data.begin();
i != data.end(); ++i) {
const SessionCommand* command = *i;
switch (command->id()) {
case kCommandSetTabWindow: {
SessionID::id_type payload[2];
if (!command->GetPayload(payload, sizeof(payload)))
return true;
GetTab(payload[1], tabs)->window_id.set_id(payload[0]);
break;
}
case kCommandSetWindowBounds2: {
WindowBoundsPayload2 payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetWindow(payload.window_id, windows)->bounds.SetRect(payload.x,
payload.y,
payload.w,
payload.h);
GetWindow(payload.window_id, windows)->is_maximized =
payload.is_maximized;
break;
}
case kCommandSetTabIndexInWindow: {
TabIndexInWindowPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetTab(payload.id, tabs)->tab_visual_index = payload.index;
break;
}
case kCommandTabClosed:
case kCommandWindowClosed: {
ClosedPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
if (command->id() == kCommandTabClosed) {
delete GetTab(payload.id, tabs);
tabs->erase(payload.id);
} else {
delete GetWindow(payload.id, windows);
windows->erase(payload.id);
}
break;
}
case kCommandTabNavigationPathPrunedFromBack: {
TabNavigationPathPrunedFromBackPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
SessionTab* tab = GetTab(payload.id, tabs);
tab->navigations.erase(
FindClosestNavigationWithIndex(&(tab->navigations), payload.index),
tab->navigations.end());
break;
}
case kCommandTabNavigationPathPrunedFromFront: {
TabNavigationPathPrunedFromFrontPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)) ||
payload.index <= 0) {
return true;
}
SessionTab* tab = GetTab(payload.id, tabs);
// Update the selected navigation index.
tab->current_navigation_index =
std::max(-1, tab->current_navigation_index - payload.index);
// And update the index of existing navigations.
for (std::vector<TabNavigation>::iterator i = tab->navigations.begin();
i != tab->navigations.end();) {
i->set_index(i->index() - payload.index);
if (i->index() < 0)
i = tab->navigations.erase(i);
else
++i;
}
break;
}
case kCommandUpdateTabNavigation: {
TabNavigation navigation;
SessionID::id_type tab_id;
if (!RestoreUpdateTabNavigationCommand(*command, &navigation, &tab_id))
return true;
SessionTab* tab = GetTab(tab_id, tabs);
std::vector<TabNavigation>::iterator i =
FindClosestNavigationWithIndex(&(tab->navigations),
navigation.index());
if (i != tab->navigations.end() && i->index() == navigation.index())
*i = navigation;
else
tab->navigations.insert(i, navigation);
break;
}
case kCommandSetSelectedNavigationIndex: {
SelectedNavigationIndexPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetTab(payload.id, tabs)->current_navigation_index = payload.index;
break;
}
case kCommandSetSelectedTabInIndex: {
SelectedTabInIndexPayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetWindow(payload.id, windows)->selected_tab_index = payload.index;
break;
}
case kCommandSetWindowType: {
WindowTypePayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetWindow(payload.id, windows)->is_constrained = false;
GetWindow(payload.id, windows)->type =
BrowserTypeForWindowType(
static_cast<WindowType>(payload.index));
break;
}
case kCommandSetPinnedState: {
PinnedStatePayload payload;
if (!command->GetPayload(&payload, sizeof(payload)))
return true;
GetTab(payload.tab_id, tabs)->pinned = payload.pinned_state;
break;
}
case kCommandSetExtensionAppID: {
SessionID::id_type tab_id;
std::string extension_app_id;
if (!RestoreSetTabExtensionAppIDCommand(
*command, &tab_id, &extension_app_id)) {
return true;
}
GetTab(tab_id, tabs)->extension_app_id.swap(extension_app_id);
break;
}
default:
return true;
}
}
return true;
}
void SessionService::BuildCommandsForTab(
const SessionID& window_id,
NavigationController* controller,
int index_in_window,
bool is_pinned,
std::vector<SessionCommand*>* commands,
IdToRange* tab_to_available_range) {
DCHECK(controller && commands && window_id.id());
commands->push_back(
CreateSetTabWindowCommand(window_id, controller->session_id()));
const int current_index = controller->GetCurrentEntryIndex();
const int min_index = std::max(0,
current_index - max_persist_navigation_count);
const int max_index = std::min(current_index + max_persist_navigation_count,
controller->entry_count());
const int pending_index = controller->pending_entry_index();
if (tab_to_available_range) {
(*tab_to_available_range)[controller->session_id().id()] =
std::pair<int, int>(min_index, max_index);
}
if (is_pinned) {
commands->push_back(
CreatePinnedStateCommand(controller->session_id(), true));
}
if (controller->tab_contents()->extension_app()) {
commands->push_back(
CreateSetTabExtensionAppIDCommand(
kCommandSetExtensionAppID,
controller->session_id().id(),
controller->tab_contents()->extension_app()->id()));
}
for (int i = min_index; i < max_index; ++i) {
const NavigationEntry* entry = (i == pending_index) ?
controller->pending_entry() : controller->GetEntryAtIndex(i);
DCHECK(entry);
if (ShouldTrackEntry(*entry)) {
commands->push_back(
CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation,
controller->session_id().id(),
i,
*entry));
}
}
commands->push_back(
CreateSetSelectedNavigationIndexCommand(controller->session_id(),
current_index));
if (index_in_window != -1) {
commands->push_back(
CreateSetTabIndexInWindowCommand(controller->session_id(),
index_in_window));
}
}
void SessionService::BuildCommandsForBrowser(
Browser* browser,
std::vector<SessionCommand*>* commands,
IdToRange* tab_to_available_range,
std::set<SessionID::id_type>* windows_to_track) {
DCHECK(browser && commands);
DCHECK(browser->session_id().id());
commands->push_back(
CreateSetWindowBoundsCommand(browser->session_id(),
browser->window()->GetRestoredBounds(),
browser->window()->IsMaximized()));
commands->push_back(CreateSetWindowTypeCommand(
browser->session_id(), WindowTypeForBrowserType(browser->type())));
bool added_to_windows_to_track = false;
for (int i = 0; i < browser->tab_count(); ++i) {
TabContents* tab = browser->GetTabContentsAt(i);
DCHECK(tab);
if (tab->profile() == profile() || profile() == NULL) {
BuildCommandsForTab(browser->session_id(), &tab->controller(), i,
browser->tabstrip_model()->IsTabPinned(i),
commands, tab_to_available_range);
if (windows_to_track && !added_to_windows_to_track) {
windows_to_track->insert(browser->session_id().id());
added_to_windows_to_track = true;
}
}
}
commands->push_back(
CreateSetSelectedTabInWindow(browser->session_id(),
browser->selected_index()));
}
void SessionService::BuildCommandsFromBrowsers(
std::vector<SessionCommand*>* commands,
IdToRange* tab_to_available_range,
std::set<SessionID::id_type>* windows_to_track) {
DCHECK(commands);
for (BrowserList::const_iterator i = BrowserList::begin();
i != BrowserList::end(); ++i) {
// Make sure the browser has tabs and a window. Browsers destructor
// removes itself from the BrowserList. When a browser is closed the
// destructor is not necessarily run immediately. This means its possible
// for us to get a handle to a browser that is about to be removed. If
// the tab count is 0 or the window is NULL, the browser is about to be
// deleted, so we ignore it.
if (should_track_changes_for_browser_type((*i)->type()) &&
(*i)->tab_count() && (*i)->window()) {
BuildCommandsForBrowser(*i, commands, tab_to_available_range,
windows_to_track);
}
}
}
void SessionService::ScheduleReset() {
set_pending_reset(true);
STLDeleteElements(&pending_commands());
tab_to_available_range_.clear();
windows_tracking_.clear();
BuildCommandsFromBrowsers(&pending_commands(), &tab_to_available_range_,
&windows_tracking_);
if (!windows_tracking_.empty()) {
// We're lazily created on startup and won't get an initial batch of
// SetWindowType messages. Set these here to make sure our state is correct.
has_open_trackable_browsers_ = true;
move_on_new_browser_ = true;
}
StartSaveTimer();
}
bool SessionService::ReplacePendingCommand(SessionCommand* command) {
// We only optimize page navigations, which can happen quite frequently and
// are expensive. If necessary, other commands could be searched for as
// well.
if (command->id() != kCommandUpdateTabNavigation)
return false;
void* iterator = NULL;
scoped_ptr<Pickle> command_pickle(command->PayloadAsPickle());
SessionID::id_type command_tab_id;
int command_nav_index;
if (!command_pickle->ReadInt(&iterator, &command_tab_id) ||
!command_pickle->ReadInt(&iterator, &command_nav_index)) {
return false;
}
for (std::vector<SessionCommand*>::reverse_iterator i =
pending_commands().rbegin(); i != pending_commands().rend(); ++i) {
SessionCommand* existing_command = *i;
if (existing_command->id() == kCommandUpdateTabNavigation) {
SessionID::id_type existing_tab_id;
int existing_nav_index;
{
// Creating a pickle like this means the Pickle references the data from
// the command. Make sure we delete the pickle before the command, else
// the pickle references deleted memory.
scoped_ptr<Pickle> existing_pickle(existing_command->PayloadAsPickle());
iterator = NULL;
if (!existing_pickle->ReadInt(&iterator, &existing_tab_id) ||
!existing_pickle->ReadInt(&iterator, &existing_nav_index)) {
return false;
}
}
if (existing_tab_id == command_tab_id &&
existing_nav_index == command_nav_index) {
// existing_command is an update for the same tab/index pair. Replace
// it with the new one. We need to add to the end of the list just in
// case there is a prune command after the update command.
delete existing_command;
pending_commands().erase(i.base() - 1);
pending_commands().push_back(command);
return true;
}
return false;
}
}
return false;
}
void SessionService::ScheduleCommand(SessionCommand* command) {
DCHECK(command);
if (ReplacePendingCommand(command))
return;
BaseSessionService::ScheduleCommand(command);
// Don't schedule a reset on tab closed/window closed. Otherwise we may
// lose tabs/windows we want to restore from if we exit right after this.
if (!pending_reset() && pending_window_close_ids_.empty() &&
commands_since_reset() >= kWritesPerReset &&
(command->id() != kCommandTabClosed &&
command->id() != kCommandWindowClosed)) {
ScheduleReset();
}
}
void SessionService::CommitPendingCloses() {
for (PendingTabCloseIDs::iterator i = pending_tab_close_ids_.begin();
i != pending_tab_close_ids_.end(); ++i) {
ScheduleCommand(CreateTabClosedCommand(*i));
}
pending_tab_close_ids_.clear();
for (PendingWindowCloseIDs::iterator i = pending_window_close_ids_.begin();
i != pending_window_close_ids_.end(); ++i) {
ScheduleCommand(CreateWindowClosedCommand(*i));
}
pending_window_close_ids_.clear();
}
bool SessionService::IsOnlyOneTabLeft() {
if (!profile()) {
// We're testing, always return false.
return false;
}
// NOTE: This uses the original profile so that closing the last non-off the
// record window while there are open off the record windows resets state).
int window_count = 0;
for (BrowserList::const_iterator i = BrowserList::begin();
i != BrowserList::end(); ++i) {
const SessionID::id_type window_id = (*i)->session_id().id();
if (should_track_changes_for_browser_type((*i)->type()) &&
(*i)->profile()->GetOriginalProfile() == profile() &&
window_closing_ids_.find(window_id) == window_closing_ids_.end()) {
if (++window_count > 1)
return false;
// By the time this is invoked the tab has been removed. As such, we use
// > 0 here rather than > 1.
if ((*i)->tab_count() > 0)
return false;
}
}
return true;
}
bool SessionService::HasOpenTrackableBrowsers(const SessionID& window_id) {
if (!profile()) {
// We're testing, always return false.
return true;
}
// NOTE: This uses the original profile so that closing the last non-off the
// record window while there are open off the record window resets state).
for (BrowserList::const_iterator i = BrowserList::begin();
i != BrowserList::end(); ++i) {
Browser* browser = *i;
const SessionID::id_type browser_id = browser->session_id().id();
if (browser_id != window_id.id() &&
window_closing_ids_.find(browser_id) == window_closing_ids_.end() &&
should_track_changes_for_browser_type(browser->type()) &&
browser->profile()->GetOriginalProfile() == profile()) {
return true;
}
}
return false;
}
bool SessionService::ShouldTrackChangesToWindow(const SessionID& window_id) {
return windows_tracking_.find(window_id.id()) != windows_tracking_.end();
}
SessionService::WindowType SessionService::WindowTypeForBrowserType(
Browser::Type type) {
// We don't support masks here, only discrete types.
switch (type) {
case Browser::TYPE_POPUP:
return TYPE_POPUP;
case Browser::TYPE_APP:
return TYPE_APP;
case Browser::TYPE_APP_POPUP:
return TYPE_APP_POPUP;
case Browser::TYPE_DEVTOOLS:
return TYPE_DEVTOOLS;
case Browser::TYPE_APP_PANEL:
return TYPE_APP_PANEL;
case Browser::TYPE_NORMAL:
default:
return TYPE_NORMAL;
}
}
Browser::Type SessionService::BrowserTypeForWindowType(
SessionService::WindowType type) {
switch (type) {
case TYPE_POPUP:
return Browser::TYPE_POPUP;
case TYPE_APP:
return Browser::TYPE_APP;
case TYPE_APP_POPUP:
return Browser::TYPE_APP_POPUP;
case TYPE_DEVTOOLS:
return Browser::TYPE_DEVTOOLS;
case TYPE_APP_PANEL:
return Browser::TYPE_APP_PANEL;
case TYPE_NORMAL:
default:
return Browser::TYPE_NORMAL;
}
}
void SessionService::RecordSessionUpdateHistogramData(NotificationType type,
base::TimeTicks* last_updated_time) {
if (!last_updated_time->is_null()) {
base::TimeDelta delta = base::TimeTicks::Now() - *last_updated_time;
// We're interested in frequent updates periods longer than
// 10 minutes.
bool use_long_period = false;
if (delta >= save_delay_in_mins_) {
use_long_period = true;
}
switch (type.value) {
case NotificationType::SESSION_SERVICE_SAVED :
RecordUpdatedSaveTime(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case NotificationType::TAB_CLOSED:
RecordUpdatedTabClosed(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case NotificationType::NAV_LIST_PRUNED:
RecordUpdatedNavListPruned(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case NotificationType::NAV_ENTRY_COMMITTED:
RecordUpdatedNavEntryCommit(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
default:
NOTREACHED() << "Bad type sent to RecordSessionUpdateHistogramData";
break;
}
}
(*last_updated_time) = base::TimeTicks::Now();
}
void SessionService::RecordUpdatedTabClosed(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.TabClosedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.TabClosedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedNavListPruned(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavigationListPrunedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavigationListPrunedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedNavEntryCommit(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavEntryCommittedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavEntryCommittedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavOrTabUpdatePeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavOrTabUpdateLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedSaveTime(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.SavePeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.SaveLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
|