OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/tab_restore_service_helper.h" | 5 #include "chrome/browser/sessions/tab_restore_service_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 13 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
15 #include "chrome/browser/sessions/tab_restore_service_observer.h" | 14 #include "chrome/browser/sessions/tab_restore_service_observer.h" |
16 #include "chrome/common/url_constants.h" | |
17 #include "components/sessions/content/content_serialized_navigation_builder.h" | 15 #include "components/sessions/content/content_serialized_navigation_builder.h" |
| 16 #include "components/sessions/core/tab_restore_service_client.h" |
18 #include "components/sessions/session_types.h" | 17 #include "components/sessions/session_types.h" |
19 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
21 #include "content/public/browser/session_storage_namespace.h" | 20 #include "content/public/browser/session_storage_namespace.h" |
22 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
23 | 22 |
24 #if defined(ENABLE_EXTENSIONS) | 23 #if defined(ENABLE_EXTENSIONS) |
25 #include "chrome/browser/extensions/tab_helper.h" | 24 #include "chrome/browser/extensions/tab_helper.h" |
26 #include "chrome/common/extensions/extension_constants.h" | |
27 #include "chrome/common/extensions/extension_metrics.h" | |
28 #include "extensions/browser/extension_registry.h" | |
29 #include "extensions/common/extension.h" | |
30 #include "extensions/common/extension_set.h" | |
31 #endif | 25 #endif |
32 | 26 |
33 using content::NavigationController; | 27 using content::NavigationController; |
34 using content::NavigationEntry; | 28 using content::NavigationEntry; |
35 using content::WebContents; | 29 using content::WebContents; |
36 | 30 |
37 namespace { | |
38 | |
39 void RecordAppLaunch(Profile* profile, const TabRestoreService::Tab& tab) { | |
40 #if defined(ENABLE_EXTENSIONS) | |
41 GURL url = tab.navigations.at(tab.current_navigation_index).virtual_url(); | |
42 const extensions::Extension* extension = | |
43 extensions::ExtensionRegistry::Get(profile) | |
44 ->enabled_extensions().GetAppByURL(url); | |
45 if (!extension) | |
46 return; | |
47 | |
48 extensions::RecordAppLaunchType( | |
49 extension_misc::APP_LAUNCH_NTP_RECENTLY_CLOSED, | |
50 extension->GetType()); | |
51 #endif // defined(ENABLE_EXTENSIONS) | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 // TabRestoreServiceHelper::Observer ------------------------------------------- | 31 // TabRestoreServiceHelper::Observer ------------------------------------------- |
57 | 32 |
58 TabRestoreServiceHelper::Observer::~Observer() {} | 33 TabRestoreServiceHelper::Observer::~Observer() {} |
59 | 34 |
60 void TabRestoreServiceHelper::Observer::OnClearEntries() {} | 35 void TabRestoreServiceHelper::Observer::OnClearEntries() {} |
61 | 36 |
62 void TabRestoreServiceHelper::Observer::OnRestoreEntryById( | 37 void TabRestoreServiceHelper::Observer::OnRestoreEntryById( |
63 SessionID::id_type id, | 38 SessionID::id_type id, |
64 Entries::const_iterator entry_iterator) { | 39 Entries::const_iterator entry_iterator) { |
65 } | 40 } |
66 | 41 |
67 void TabRestoreServiceHelper::Observer::OnAddEntry() {} | 42 void TabRestoreServiceHelper::Observer::OnAddEntry() {} |
68 | 43 |
69 // TabRestoreServiceHelper ----------------------------------------------------- | 44 // TabRestoreServiceHelper ----------------------------------------------------- |
70 | 45 |
71 TabRestoreServiceHelper::TabRestoreServiceHelper( | 46 TabRestoreServiceHelper::TabRestoreServiceHelper( |
72 TabRestoreService* tab_restore_service, | 47 TabRestoreService* tab_restore_service, |
73 Observer* observer, | 48 Observer* observer, |
74 Profile* profile, | 49 Profile* profile, |
| 50 sessions::TabRestoreServiceClient* client, |
75 TabRestoreService::TimeFactory* time_factory) | 51 TabRestoreService::TimeFactory* time_factory) |
76 : tab_restore_service_(tab_restore_service), | 52 : tab_restore_service_(tab_restore_service), |
77 observer_(observer), | 53 observer_(observer), |
78 profile_(profile), | 54 profile_(profile), |
| 55 client_(client), |
79 restoring_(false), | 56 restoring_(false), |
80 time_factory_(time_factory) { | 57 time_factory_(time_factory) { |
81 DCHECK(tab_restore_service_); | 58 DCHECK(tab_restore_service_); |
82 } | 59 } |
83 | 60 |
84 TabRestoreServiceHelper::~TabRestoreServiceHelper() { | 61 TabRestoreServiceHelper::~TabRestoreServiceHelper() { |
85 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, | 62 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, |
86 TabRestoreServiceDestroyed(tab_restore_service_)); | 63 TabRestoreServiceDestroyed(tab_restore_service_)); |
87 STLDeleteElements(&entries_); | 64 STLDeleteElements(&entries_); |
88 } | 65 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 delegate->GetTabCount(), | 227 delegate->GetTabCount(), |
251 tab.current_navigation_index, | 228 tab.current_navigation_index, |
252 tab.extension_app_id, | 229 tab.extension_app_id, |
253 static_cast<int>(tab_i) == window->selected_tab_index, | 230 static_cast<int>(tab_i) == window->selected_tab_index, |
254 tab.pinned, | 231 tab.pinned, |
255 tab.from_last_session, | 232 tab.from_last_session, |
256 tab.session_storage_namespace.get(), | 233 tab.session_storage_namespace.get(), |
257 tab.user_agent_override); | 234 tab.user_agent_override); |
258 if (restored_tab) { | 235 if (restored_tab) { |
259 restored_tab->GetController().LoadIfNecessary(); | 236 restored_tab->GetController().LoadIfNecessary(); |
260 RecordAppLaunch(profile_, tab); | 237 client_->OnTabRestored( |
| 238 tab.navigations.at(tab.current_navigation_index).virtual_url()); |
261 web_contents.push_back(restored_tab); | 239 web_contents.push_back(restored_tab); |
262 } | 240 } |
263 } | 241 } |
264 // All the window's tabs had the same former browser_id. | 242 // All the window's tabs had the same former browser_id. |
265 if (window->tabs[0].has_browser()) { | 243 if (window->tabs[0].has_browser()) { |
266 UpdateTabBrowserIDs(window->tabs[0].browser_id, | 244 UpdateTabBrowserIDs(window->tabs[0].browser_id, |
267 delegate->GetSessionID().id()); | 245 delegate->GetSessionID().id()); |
268 } | 246 } |
269 } else { | 247 } else { |
270 // Restore a single tab from the window. Find the tab that matches the ID | 248 // Restore a single tab from the window. Find the tab that matches the ID |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 tab_index, | 474 tab_index, |
497 tab.current_navigation_index, | 475 tab.current_navigation_index, |
498 tab.extension_app_id, | 476 tab.extension_app_id, |
499 disposition != NEW_BACKGROUND_TAB, | 477 disposition != NEW_BACKGROUND_TAB, |
500 tab.pinned, | 478 tab.pinned, |
501 tab.from_last_session, | 479 tab.from_last_session, |
502 tab.session_storage_namespace.get(), | 480 tab.session_storage_namespace.get(), |
503 tab.user_agent_override); | 481 tab.user_agent_override); |
504 web_contents->GetController().LoadIfNecessary(); | 482 web_contents->GetController().LoadIfNecessary(); |
505 } | 483 } |
506 RecordAppLaunch(profile_, tab); | 484 client_->OnTabRestored( |
| 485 tab.navigations.at(tab.current_navigation_index).virtual_url()); |
507 if (contents) | 486 if (contents) |
508 *contents = web_contents; | 487 *contents = web_contents; |
509 | 488 |
510 return delegate; | 489 return delegate; |
511 } | 490 } |
512 | 491 |
513 | 492 |
514 bool TabRestoreServiceHelper::ValidateTab(Tab* tab) { | 493 bool TabRestoreServiceHelper::ValidateTab(Tab* tab) { |
515 if (tab->navigations.empty()) | 494 if (tab->navigations.empty()) |
516 return false; | 495 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 } | 528 } |
550 | 529 |
551 bool TabRestoreServiceHelper::IsTabInteresting(const Tab* tab) { | 530 bool TabRestoreServiceHelper::IsTabInteresting(const Tab* tab) { |
552 if (tab->navigations.empty()) | 531 if (tab->navigations.empty()) |
553 return false; | 532 return false; |
554 | 533 |
555 if (tab->navigations.size() > 1) | 534 if (tab->navigations.size() > 1) |
556 return true; | 535 return true; |
557 | 536 |
558 return tab->pinned || | 537 return tab->pinned || |
559 tab->navigations.at(0).virtual_url() != | 538 tab->navigations.at(0).virtual_url() != client_->GetNewTabURL(); |
560 GURL(chrome::kChromeUINewTabURL); | |
561 } | 539 } |
562 | 540 |
563 bool TabRestoreServiceHelper::IsWindowInteresting(const Window* window) { | 541 bool TabRestoreServiceHelper::IsWindowInteresting(const Window* window) { |
564 if (window->tabs.empty()) | 542 if (window->tabs.empty()) |
565 return false; | 543 return false; |
566 | 544 |
567 if (window->tabs.size() > 1) | 545 if (window->tabs.size() > 1) |
568 return true; | 546 return true; |
569 | 547 |
570 return IsTabInteresting(&window->tabs[0]); | 548 return IsTabInteresting(&window->tabs[0]); |
(...skipping 20 matching lines...) Expand all Loading... |
591 Tab* tab = static_cast<Tab*>(entry); | 569 Tab* tab = static_cast<Tab*>(entry); |
592 if (tab->browser_id == old_id) | 570 if (tab->browser_id == old_id) |
593 tab->browser_id = new_id; | 571 tab->browser_id = new_id; |
594 } | 572 } |
595 } | 573 } |
596 } | 574 } |
597 | 575 |
598 base::Time TabRestoreServiceHelper::TimeNow() const { | 576 base::Time TabRestoreServiceHelper::TimeNow() const { |
599 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); | 577 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); |
600 } | 578 } |
OLD | NEW |