blob: 708970e081e3dc783a60ccbee9eafcdda755a74f (
plain)
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
|
// Copyright 2014 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 "base/metrics/user_metrics_action.h"
#include "base/supports_user_data.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/tab_restore_service.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/sessions/tab_restore_service_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
#include "content/public/browser/user_metrics.h"
namespace chrome {
namespace {
const char kBrowserTabRestorerKey[] = "BrowserTabRestorer";
// BrowserTabRestorer is responsible for restoring a tab when the
// TabRestoreService finishes loading. A TabRestoreService is associated with a
// single Browser and deletes itself if the Browser is destroyed.
// BrowserTabRestorer is installed on the Profile (by way of user data), only
// one instance is created per profile at a time.
class BrowserTabRestorer : public TabRestoreServiceObserver,
public chrome::BrowserListObserver,
public base::SupportsUserData::Data {
public:
virtual ~BrowserTabRestorer();
static void CreateIfNecessary(Browser* browser);
private:
explicit BrowserTabRestorer(Browser* browser);
// TabRestoreServiceObserver:
virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE;
virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE;
virtual void TabRestoreServiceLoaded(TabRestoreService* service) OVERRIDE;
// BrowserListObserver:
virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
Browser* browser_;
TabRestoreService* tab_restore_service_;
DISALLOW_COPY_AND_ASSIGN(BrowserTabRestorer);
};
BrowserTabRestorer::~BrowserTabRestorer() {
tab_restore_service_->RemoveObserver(this);
BrowserList::GetInstance(browser_->host_desktop_type())->RemoveObserver(this);
}
// static
void BrowserTabRestorer::CreateIfNecessary(Browser* browser) {
DCHECK(browser);
if (browser->profile()->GetUserData(kBrowserTabRestorerKey))
return; // Only allow one restore for a given profile at a time.
// BrowserTabRestorer is deleted at the appropriate time.
new BrowserTabRestorer(browser);
}
BrowserTabRestorer::BrowserTabRestorer(Browser* browser)
: browser_(browser),
tab_restore_service_(
TabRestoreServiceFactory::GetForProfile(browser->profile())) {
DCHECK(tab_restore_service_);
DCHECK(!tab_restore_service_->IsLoaded());
tab_restore_service_->AddObserver(this);
BrowserList::GetInstance(browser->host_desktop_type())->AddObserver(this);
browser_->profile()->SetUserData(kBrowserTabRestorerKey, this);
tab_restore_service_->LoadTabsFromLastSession();
}
void BrowserTabRestorer::TabRestoreServiceChanged(TabRestoreService* service) {
}
void BrowserTabRestorer::TabRestoreServiceDestroyed(
TabRestoreService* service) {
}
void BrowserTabRestorer::TabRestoreServiceLoaded(TabRestoreService* service) {
RestoreTab(browser_);
// This deletes us.
browser_->profile()->SetUserData(kBrowserTabRestorerKey, NULL);
}
void BrowserTabRestorer::OnBrowserRemoved(Browser* browser) {
// This deletes us.
browser_->profile()->SetUserData(kBrowserTabRestorerKey, NULL);
}
} // namespace
void RestoreTab(Browser* browser) {
content::RecordAction(base::UserMetricsAction("RestoreTab"));
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser->profile());
if (!service)
return;
if (service->IsLoaded()) {
service->RestoreMostRecentEntry(browser->tab_restore_service_delegate(),
browser->host_desktop_type());
return;
}
BrowserTabRestorer::CreateIfNecessary(browser);
}
} // namespace chrome
|