OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_commands.h" | 5 #include "chrome/browser/sessions/session_service_commands.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "chrome/browser/sessions/base_session_service_commands.h" | 10 #include "chrome/browser/sessions/base_session_service_commands.h" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 260 } |
261 ++i; | 261 ++i; |
262 } | 262 } |
263 } | 263 } |
264 } | 264 } |
265 | 265 |
266 // Adds tabs to their parent window based on the tab's window_id. This | 266 // Adds tabs to their parent window based on the tab's window_id. This |
267 // ignores tabs with no navigations. | 267 // ignores tabs with no navigations. |
268 void AddTabsToWindows(std::map<int, SessionTab*>* tabs, | 268 void AddTabsToWindows(std::map<int, SessionTab*>* tabs, |
269 std::map<int, SessionWindow*>* windows) { | 269 std::map<int, SessionWindow*>* windows) { |
270 VLOG(1) << "AddTabsToWindws"; | 270 DVLOG(1) << "AddTabsToWindws"; |
271 VLOG(1) << "Tabs " << tabs->size() << ", windows " << windows->size(); | 271 DVLOG(1) << "Tabs " << tabs->size() << ", windows " << windows->size(); |
272 std::map<int, SessionTab*>::iterator i = tabs->begin(); | 272 std::map<int, SessionTab*>::iterator i = tabs->begin(); |
273 while (i != tabs->end()) { | 273 while (i != tabs->end()) { |
274 SessionTab* tab = i->second; | 274 SessionTab* tab = i->second; |
275 if (tab->window_id.id() && !tab->navigations.empty()) { | 275 if (tab->window_id.id() && !tab->navigations.empty()) { |
276 SessionWindow* window = GetWindow(tab->window_id.id(), windows); | 276 SessionWindow* window = GetWindow(tab->window_id.id(), windows); |
277 window->tabs.push_back(tab); | 277 window->tabs.push_back(tab); |
278 tabs->erase(i++); | 278 tabs->erase(i++); |
279 | 279 |
280 // See note in SessionTab as to why we do this. | 280 // See note in SessionTab as to why we do this. |
281 std::vector<sessions::SerializedNavigationEntry>::iterator j = | 281 std::vector<sessions::SerializedNavigationEntry>::iterator j = |
(...skipping 20 matching lines...) Expand all Loading... |
302 // to delete the tabs and windows added to |tabs| and |windows|. | 302 // to delete the tabs and windows added to |tabs| and |windows|. |
303 // | 303 // |
304 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is | 304 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is |
305 // done by AddTabsToWindows. | 305 // done by AddTabsToWindows. |
306 bool CreateTabsAndWindows(const ScopedVector<SessionCommand>& data, | 306 bool CreateTabsAndWindows(const ScopedVector<SessionCommand>& data, |
307 std::map<int, SessionTab*>* tabs, | 307 std::map<int, SessionTab*>* tabs, |
308 std::map<int, SessionWindow*>* windows, | 308 std::map<int, SessionWindow*>* windows, |
309 SessionID::id_type* active_window_id) { | 309 SessionID::id_type* active_window_id) { |
310 // If the file is corrupt (command with wrong size, or unknown command), we | 310 // If the file is corrupt (command with wrong size, or unknown command), we |
311 // still return true and attempt to restore what we we can. | 311 // still return true and attempt to restore what we we can. |
312 VLOG(1) << "CreateTabsAndWindows"; | 312 DVLOG(1) << "CreateTabsAndWindows"; |
313 | 313 |
314 for (std::vector<SessionCommand*>::const_iterator i = data.begin(); | 314 for (std::vector<SessionCommand*>::const_iterator i = data.begin(); |
315 i != data.end(); ++i) { | 315 i != data.end(); ++i) { |
316 const SessionCommand::id_type kCommandSetWindowBounds2 = 10; | 316 const SessionCommand::id_type kCommandSetWindowBounds2 = 10; |
317 const SessionCommand* command = *i; | 317 const SessionCommand* command = *i; |
318 | 318 |
319 VLOG(1) << "Read command " << (int) command->id(); | 319 DVLOG(1) << "Read command " << (int) command->id(); |
320 switch (command->id()) { | 320 switch (command->id()) { |
321 case kCommandSetTabWindow: { | 321 case kCommandSetTabWindow: { |
322 SessionID::id_type payload[2]; | 322 SessionID::id_type payload[2]; |
323 if (!command->GetPayload(payload, sizeof(payload))) { | 323 if (!command->GetPayload(payload, sizeof(payload))) { |
324 VLOG(1) << "Failed reading command " << command->id(); | 324 DVLOG(1) << "Failed reading command " << command->id(); |
325 return true; | 325 return true; |
326 } | 326 } |
327 GetTab(payload[1], tabs)->window_id.set_id(payload[0]); | 327 GetTab(payload[1], tabs)->window_id.set_id(payload[0]); |
328 break; | 328 break; |
329 } | 329 } |
330 | 330 |
331 // This is here for forward migration only. New data is saved with | 331 // This is here for forward migration only. New data is saved with |
332 // |kCommandSetWindowBounds3|. | 332 // |kCommandSetWindowBounds3|. |
333 case kCommandSetWindowBounds2: { | 333 case kCommandSetWindowBounds2: { |
334 WindowBoundsPayload2 payload; | 334 WindowBoundsPayload2 payload; |
335 if (!command->GetPayload(&payload, sizeof(payload))) { | 335 if (!command->GetPayload(&payload, sizeof(payload))) { |
336 VLOG(1) << "Failed reading command " << command->id(); | 336 DVLOG(1) << "Failed reading command " << command->id(); |
337 return true; | 337 return true; |
338 } | 338 } |
339 GetWindow(payload.window_id, windows)->bounds.SetRect(payload.x, | 339 GetWindow(payload.window_id, windows)->bounds.SetRect(payload.x, |
340 payload.y, | 340 payload.y, |
341 payload.w, | 341 payload.w, |
342 payload.h); | 342 payload.h); |
343 GetWindow(payload.window_id, windows)->show_state = | 343 GetWindow(payload.window_id, windows)->show_state = |
344 payload.is_maximized ? | 344 payload.is_maximized ? |
345 ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL; | 345 ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL; |
346 break; | 346 break; |
347 } | 347 } |
348 | 348 |
349 case kCommandSetWindowBounds3: { | 349 case kCommandSetWindowBounds3: { |
350 WindowBoundsPayload3 payload; | 350 WindowBoundsPayload3 payload; |
351 if (!command->GetPayload(&payload, sizeof(payload))) { | 351 if (!command->GetPayload(&payload, sizeof(payload))) { |
352 VLOG(1) << "Failed reading command " << command->id(); | 352 DVLOG(1) << "Failed reading command " << command->id(); |
353 return true; | 353 return true; |
354 } | 354 } |
355 GetWindow(payload.window_id, windows)->bounds.SetRect(payload.x, | 355 GetWindow(payload.window_id, windows)->bounds.SetRect(payload.x, |
356 payload.y, | 356 payload.y, |
357 payload.w, | 357 payload.w, |
358 payload.h); | 358 payload.h); |
359 GetWindow(payload.window_id, windows)->show_state = | 359 GetWindow(payload.window_id, windows)->show_state = |
360 PersistedShowStateToShowState(payload.show_state); | 360 PersistedShowStateToShowState(payload.show_state); |
361 break; | 361 break; |
362 } | 362 } |
363 | 363 |
364 case kCommandSetTabIndexInWindow: { | 364 case kCommandSetTabIndexInWindow: { |
365 TabIndexInWindowPayload payload; | 365 TabIndexInWindowPayload payload; |
366 if (!command->GetPayload(&payload, sizeof(payload))) { | 366 if (!command->GetPayload(&payload, sizeof(payload))) { |
367 VLOG(1) << "Failed reading command " << command->id(); | 367 DVLOG(1) << "Failed reading command " << command->id(); |
368 return true; | 368 return true; |
369 } | 369 } |
370 GetTab(payload.id, tabs)->tab_visual_index = payload.index; | 370 GetTab(payload.id, tabs)->tab_visual_index = payload.index; |
371 break; | 371 break; |
372 } | 372 } |
373 | 373 |
374 case kCommandTabClosed: | 374 case kCommandTabClosed: |
375 case kCommandWindowClosed: { | 375 case kCommandWindowClosed: { |
376 ClosedPayload payload; | 376 ClosedPayload payload; |
377 if (!command->GetPayload(&payload, sizeof(payload))) { | 377 if (!command->GetPayload(&payload, sizeof(payload))) { |
378 VLOG(1) << "Failed reading command " << command->id(); | 378 DVLOG(1) << "Failed reading command " << command->id(); |
379 return true; | 379 return true; |
380 } | 380 } |
381 if (command->id() == kCommandTabClosed) { | 381 if (command->id() == kCommandTabClosed) { |
382 delete GetTab(payload.id, tabs); | 382 delete GetTab(payload.id, tabs); |
383 tabs->erase(payload.id); | 383 tabs->erase(payload.id); |
384 } else { | 384 } else { |
385 delete GetWindow(payload.id, windows); | 385 delete GetWindow(payload.id, windows); |
386 windows->erase(payload.id); | 386 windows->erase(payload.id); |
387 } | 387 } |
388 break; | 388 break; |
389 } | 389 } |
390 | 390 |
391 case kCommandTabNavigationPathPrunedFromBack: { | 391 case kCommandTabNavigationPathPrunedFromBack: { |
392 TabNavigationPathPrunedFromBackPayload payload; | 392 TabNavigationPathPrunedFromBackPayload payload; |
393 if (!command->GetPayload(&payload, sizeof(payload))) { | 393 if (!command->GetPayload(&payload, sizeof(payload))) { |
394 VLOG(1) << "Failed reading command " << command->id(); | 394 DVLOG(1) << "Failed reading command " << command->id(); |
395 return true; | 395 return true; |
396 } | 396 } |
397 SessionTab* tab = GetTab(payload.id, tabs); | 397 SessionTab* tab = GetTab(payload.id, tabs); |
398 tab->navigations.erase( | 398 tab->navigations.erase( |
399 FindClosestNavigationWithIndex(&(tab->navigations), payload.index), | 399 FindClosestNavigationWithIndex(&(tab->navigations), payload.index), |
400 tab->navigations.end()); | 400 tab->navigations.end()); |
401 break; | 401 break; |
402 } | 402 } |
403 | 403 |
404 case kCommandTabNavigationPathPrunedFromFront: { | 404 case kCommandTabNavigationPathPrunedFromFront: { |
405 TabNavigationPathPrunedFromFrontPayload payload; | 405 TabNavigationPathPrunedFromFrontPayload payload; |
406 if (!command->GetPayload(&payload, sizeof(payload)) || | 406 if (!command->GetPayload(&payload, sizeof(payload)) || |
407 payload.index <= 0) { | 407 payload.index <= 0) { |
408 VLOG(1) << "Failed reading command " << command->id(); | 408 DVLOG(1) << "Failed reading command " << command->id(); |
409 return true; | 409 return true; |
410 } | 410 } |
411 SessionTab* tab = GetTab(payload.id, tabs); | 411 SessionTab* tab = GetTab(payload.id, tabs); |
412 | 412 |
413 // Update the selected navigation index. | 413 // Update the selected navigation index. |
414 tab->current_navigation_index = | 414 tab->current_navigation_index = |
415 std::max(-1, tab->current_navigation_index - payload.index); | 415 std::max(-1, tab->current_navigation_index - payload.index); |
416 | 416 |
417 // And update the index of existing navigations. | 417 // And update the index of existing navigations. |
418 for (std::vector<sessions::SerializedNavigationEntry>::iterator | 418 for (std::vector<sessions::SerializedNavigationEntry>::iterator |
419 i = tab->navigations.begin(); | 419 i = tab->navigations.begin(); |
420 i != tab->navigations.end();) { | 420 i != tab->navigations.end();) { |
421 i->set_index(i->index() - payload.index); | 421 i->set_index(i->index() - payload.index); |
422 if (i->index() < 0) | 422 if (i->index() < 0) |
423 i = tab->navigations.erase(i); | 423 i = tab->navigations.erase(i); |
424 else | 424 else |
425 ++i; | 425 ++i; |
426 } | 426 } |
427 break; | 427 break; |
428 } | 428 } |
429 | 429 |
430 case kCommandUpdateTabNavigation: { | 430 case kCommandUpdateTabNavigation: { |
431 sessions::SerializedNavigationEntry navigation; | 431 sessions::SerializedNavigationEntry navigation; |
432 SessionID::id_type tab_id; | 432 SessionID::id_type tab_id; |
433 if (!RestoreUpdateTabNavigationCommand(*command, | 433 if (!RestoreUpdateTabNavigationCommand(*command, |
434 &navigation, | 434 &navigation, |
435 &tab_id)) { | 435 &tab_id)) { |
436 VLOG(1) << "Failed reading command " << command->id(); | 436 DVLOG(1) << "Failed reading command " << command->id(); |
437 return true; | 437 return true; |
438 } | 438 } |
439 SessionTab* tab = GetTab(tab_id, tabs); | 439 SessionTab* tab = GetTab(tab_id, tabs); |
440 std::vector<sessions::SerializedNavigationEntry>::iterator i = | 440 std::vector<sessions::SerializedNavigationEntry>::iterator i = |
441 FindClosestNavigationWithIndex(&(tab->navigations), | 441 FindClosestNavigationWithIndex(&(tab->navigations), |
442 navigation.index()); | 442 navigation.index()); |
443 if (i != tab->navigations.end() && i->index() == navigation.index()) | 443 if (i != tab->navigations.end() && i->index() == navigation.index()) |
444 *i = navigation; | 444 *i = navigation; |
445 else | 445 else |
446 tab->navigations.insert(i, navigation); | 446 tab->navigations.insert(i, navigation); |
447 break; | 447 break; |
448 } | 448 } |
449 | 449 |
450 case kCommandSetSelectedNavigationIndex: { | 450 case kCommandSetSelectedNavigationIndex: { |
451 SelectedNavigationIndexPayload payload; | 451 SelectedNavigationIndexPayload payload; |
452 if (!command->GetPayload(&payload, sizeof(payload))) { | 452 if (!command->GetPayload(&payload, sizeof(payload))) { |
453 VLOG(1) << "Failed reading command " << command->id(); | 453 DVLOG(1) << "Failed reading command " << command->id(); |
454 return true; | 454 return true; |
455 } | 455 } |
456 GetTab(payload.id, tabs)->current_navigation_index = payload.index; | 456 GetTab(payload.id, tabs)->current_navigation_index = payload.index; |
457 break; | 457 break; |
458 } | 458 } |
459 | 459 |
460 case kCommandSetSelectedTabInIndex: { | 460 case kCommandSetSelectedTabInIndex: { |
461 SelectedTabInIndexPayload payload; | 461 SelectedTabInIndexPayload payload; |
462 if (!command->GetPayload(&payload, sizeof(payload))) { | 462 if (!command->GetPayload(&payload, sizeof(payload))) { |
463 VLOG(1) << "Failed reading command " << command->id(); | 463 DVLOG(1) << "Failed reading command " << command->id(); |
464 return true; | 464 return true; |
465 } | 465 } |
466 GetWindow(payload.id, windows)->selected_tab_index = payload.index; | 466 GetWindow(payload.id, windows)->selected_tab_index = payload.index; |
467 break; | 467 break; |
468 } | 468 } |
469 | 469 |
470 case kCommandSetWindowType: { | 470 case kCommandSetWindowType: { |
471 WindowTypePayload payload; | 471 WindowTypePayload payload; |
472 if (!command->GetPayload(&payload, sizeof(payload))) { | 472 if (!command->GetPayload(&payload, sizeof(payload))) { |
473 VLOG(1) << "Failed reading command " << command->id(); | 473 DVLOG(1) << "Failed reading command " << command->id(); |
474 return true; | 474 return true; |
475 } | 475 } |
476 GetWindow(payload.id, windows)->is_constrained = false; | 476 GetWindow(payload.id, windows)->is_constrained = false; |
477 GetWindow(payload.id, windows)->type = | 477 GetWindow(payload.id, windows)->type = |
478 static_cast<SessionWindow::WindowType>(payload.index); | 478 static_cast<SessionWindow::WindowType>(payload.index); |
479 break; | 479 break; |
480 } | 480 } |
481 | 481 |
482 case kCommandSetPinnedState: { | 482 case kCommandSetPinnedState: { |
483 PinnedStatePayload payload; | 483 PinnedStatePayload payload; |
484 if (!command->GetPayload(&payload, sizeof(payload))) { | 484 if (!command->GetPayload(&payload, sizeof(payload))) { |
485 VLOG(1) << "Failed reading command " << command->id(); | 485 DVLOG(1) << "Failed reading command " << command->id(); |
486 return true; | 486 return true; |
487 } | 487 } |
488 GetTab(payload.tab_id, tabs)->pinned = payload.pinned_state; | 488 GetTab(payload.tab_id, tabs)->pinned = payload.pinned_state; |
489 break; | 489 break; |
490 } | 490 } |
491 | 491 |
492 case kCommandSetWindowAppName: { | 492 case kCommandSetWindowAppName: { |
493 SessionID::id_type window_id; | 493 SessionID::id_type window_id; |
494 std::string app_name; | 494 std::string app_name; |
495 if (!RestoreSetWindowAppNameCommand(*command, &window_id, &app_name)) | 495 if (!RestoreSetWindowAppNameCommand(*command, &window_id, &app_name)) |
496 return true; | 496 return true; |
497 | 497 |
498 GetWindow(window_id, windows)->app_name.swap(app_name); | 498 GetWindow(window_id, windows)->app_name.swap(app_name); |
499 break; | 499 break; |
500 } | 500 } |
501 | 501 |
502 case kCommandSetExtensionAppID: { | 502 case kCommandSetExtensionAppID: { |
503 SessionID::id_type tab_id; | 503 SessionID::id_type tab_id; |
504 std::string extension_app_id; | 504 std::string extension_app_id; |
505 if (!RestoreSetTabExtensionAppIDCommand(*command, | 505 if (!RestoreSetTabExtensionAppIDCommand(*command, |
506 &tab_id, | 506 &tab_id, |
507 &extension_app_id)) { | 507 &extension_app_id)) { |
508 VLOG(1) << "Failed reading command " << command->id(); | 508 DVLOG(1) << "Failed reading command " << command->id(); |
509 return true; | 509 return true; |
510 } | 510 } |
511 | 511 |
512 GetTab(tab_id, tabs)->extension_app_id.swap(extension_app_id); | 512 GetTab(tab_id, tabs)->extension_app_id.swap(extension_app_id); |
513 break; | 513 break; |
514 } | 514 } |
515 | 515 |
516 case kCommandSetTabUserAgentOverride: { | 516 case kCommandSetTabUserAgentOverride: { |
517 SessionID::id_type tab_id; | 517 SessionID::id_type tab_id; |
518 std::string user_agent_override; | 518 std::string user_agent_override; |
(...skipping 18 matching lines...) Expand all Loading... |
537 return true; | 537 return true; |
538 // Associate the session storage back. | 538 // Associate the session storage back. |
539 GetTab(command_tab_id, tabs)->session_storage_persistent_id = | 539 GetTab(command_tab_id, tabs)->session_storage_persistent_id = |
540 session_storage_persistent_id; | 540 session_storage_persistent_id; |
541 break; | 541 break; |
542 } | 542 } |
543 | 543 |
544 case kCommandSetActiveWindow: { | 544 case kCommandSetActiveWindow: { |
545 ActiveWindowPayload payload; | 545 ActiveWindowPayload payload; |
546 if (!command->GetPayload(&payload, sizeof(payload))) { | 546 if (!command->GetPayload(&payload, sizeof(payload))) { |
547 VLOG(1) << "Failed reading command " << command->id(); | 547 DVLOG(1) << "Failed reading command " << command->id(); |
548 return true; | 548 return true; |
549 } | 549 } |
550 *active_window_id = payload; | 550 *active_window_id = payload; |
551 break; | 551 break; |
552 } | 552 } |
553 | 553 |
554 default: | 554 default: |
555 // TODO(skuhne): This might call back into a callback handler to extend | 555 // TODO(skuhne): This might call back into a callback handler to extend |
556 // the command set for specific implementations. | 556 // the command set for specific implementations. |
557 VLOG(1) << "Failed reading an unknown command " << command->id(); | 557 DVLOG(1) << "Failed reading an unknown command " << command->id(); |
558 return true; | 558 return true; |
559 } | 559 } |
560 } | 560 } |
561 return true; | 561 return true; |
562 } | 562 } |
563 | 563 |
564 } // namespace | 564 } // namespace |
565 | 565 |
566 scoped_ptr<SessionCommand> CreateSetSelectedTabInWindowCommand( | 566 scoped_ptr<SessionCommand> CreateSetSelectedTabInWindowCommand( |
567 const SessionID& window_id, | 567 const SessionID& window_id, |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 return command->id() == kCommandTabClosed || | 814 return command->id() == kCommandTabClosed || |
815 command->id() == kCommandWindowClosed; | 815 command->id() == kCommandWindowClosed; |
816 } | 816 } |
817 | 817 |
818 void RestoreSessionFromCommands(const ScopedVector<SessionCommand>& commands, | 818 void RestoreSessionFromCommands(const ScopedVector<SessionCommand>& commands, |
819 std::vector<SessionWindow*>* valid_windows, | 819 std::vector<SessionWindow*>* valid_windows, |
820 SessionID::id_type* active_window_id) { | 820 SessionID::id_type* active_window_id) { |
821 std::map<int, SessionTab*> tabs; | 821 std::map<int, SessionTab*> tabs; |
822 std::map<int, SessionWindow*> windows; | 822 std::map<int, SessionWindow*> windows; |
823 | 823 |
824 VLOG(1) << "RestoreSessionFromCommands " << commands.size(); | 824 DVLOG(1) << "RestoreSessionFromCommands " << commands.size(); |
825 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { | 825 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { |
826 AddTabsToWindows(&tabs, &windows); | 826 AddTabsToWindows(&tabs, &windows); |
827 SortTabsBasedOnVisualOrderAndPrune(&windows, valid_windows); | 827 SortTabsBasedOnVisualOrderAndPrune(&windows, valid_windows); |
828 UpdateSelectedTabIndex(valid_windows); | 828 UpdateSelectedTabIndex(valid_windows); |
829 } | 829 } |
830 STLDeleteValues(&tabs); | 830 STLDeleteValues(&tabs); |
831 // Don't delete contents of windows, that is done by the caller as all | 831 // Don't delete contents of windows, that is done by the caller as all |
832 // valid windows are added to valid_windows. | 832 // valid windows are added to valid_windows. |
833 } | 833 } |
OLD | NEW |