Vectorize sad tab image.
[chromium-blink-merge.git] / chrome / browser / ui / browser_tab_restorer.cc
blob981e61dc3194ea56841842cdda56681a80e4293c
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
3 // found in the LICENSE file.
5 #include "base/metrics/user_metrics_action.h"
6 #include "base/supports_user_data.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/sessions/tab_restore_service_factory.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/browser_list_observer.h"
13 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
14 #include "components/sessions/core/tab_restore_service.h"
15 #include "components/sessions/core/tab_restore_service_observer.h"
16 #include "content/public/browser/user_metrics.h"
18 namespace chrome {
19 namespace {
21 const char kBrowserTabRestorerKey[] = "BrowserTabRestorer";
23 // BrowserTabRestorer is responsible for restoring a tab when the
24 // sessions::TabRestoreService finishes loading. A TabRestoreService is
25 // associated with a
26 // single Browser and deletes itself if the Browser is destroyed.
27 // BrowserTabRestorer is installed on the Profile (by way of user data), only
28 // one instance is created per profile at a time.
29 class BrowserTabRestorer : public sessions::TabRestoreServiceObserver,
30 public chrome::BrowserListObserver,
31 public base::SupportsUserData::Data {
32 public:
33 ~BrowserTabRestorer() override;
35 static void CreateIfNecessary(Browser* browser);
37 private:
38 explicit BrowserTabRestorer(Browser* browser);
40 // TabRestoreServiceObserver:
41 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
42 void TabRestoreServiceDestroyed(
43 sessions::TabRestoreService* service) override;
44 void TabRestoreServiceLoaded(sessions::TabRestoreService* service) override;
46 // BrowserListObserver:
47 void OnBrowserRemoved(Browser* browser) override;
49 Browser* browser_;
50 sessions::TabRestoreService* tab_restore_service_;
52 DISALLOW_COPY_AND_ASSIGN(BrowserTabRestorer);
55 BrowserTabRestorer::~BrowserTabRestorer() {
56 tab_restore_service_->RemoveObserver(this);
57 BrowserList::GetInstance(browser_->host_desktop_type())->RemoveObserver(this);
60 // static
61 void BrowserTabRestorer::CreateIfNecessary(Browser* browser) {
62 DCHECK(browser);
63 if (browser->profile()->GetUserData(kBrowserTabRestorerKey))
64 return; // Only allow one restore for a given profile at a time.
66 // BrowserTabRestorer is deleted at the appropriate time.
67 new BrowserTabRestorer(browser);
70 BrowserTabRestorer::BrowserTabRestorer(Browser* browser)
71 : browser_(browser),
72 tab_restore_service_(
73 TabRestoreServiceFactory::GetForProfile(browser->profile())) {
74 DCHECK(tab_restore_service_);
75 DCHECK(!tab_restore_service_->IsLoaded());
76 tab_restore_service_->AddObserver(this);
77 BrowserList::GetInstance(browser->host_desktop_type())->AddObserver(this);
78 browser_->profile()->SetUserData(kBrowserTabRestorerKey, this);
79 tab_restore_service_->LoadTabsFromLastSession();
82 void BrowserTabRestorer::TabRestoreServiceChanged(
83 sessions::TabRestoreService* service) {}
85 void BrowserTabRestorer::TabRestoreServiceDestroyed(
86 sessions::TabRestoreService* service) {}
88 void BrowserTabRestorer::TabRestoreServiceLoaded(
89 sessions::TabRestoreService* service) {
90 RestoreTab(browser_);
91 // This deletes us.
92 browser_->profile()->SetUserData(kBrowserTabRestorerKey, NULL);
95 void BrowserTabRestorer::OnBrowserRemoved(Browser* browser) {
96 // This deletes us.
97 browser_->profile()->SetUserData(kBrowserTabRestorerKey, NULL);
100 } // namespace
102 void RestoreTab(Browser* browser) {
103 content::RecordAction(base::UserMetricsAction("RestoreTab"));
104 sessions::TabRestoreService* service =
105 TabRestoreServiceFactory::GetForProfile(browser->profile());
106 if (!service)
107 return;
109 if (service->IsLoaded()) {
110 service->RestoreMostRecentEntry(browser->tab_restore_service_delegate(),
111 browser->host_desktop_type());
112 return;
115 BrowserTabRestorer::CreateIfNecessary(browser);
118 } // namespace chrome