OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/ui/startup/session_crashed_prompt.h" | 5 #include "chrome/browser/ui/startup/session_crashed_prompt.h" |
6 | 6 |
7 #include "chrome/browser/infobars/infobar_tab_helper.h" | 7 #include "chrome/browser/infobars/infobar_tab_helper.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/sessions/session_restore.h" | 9 #include "chrome/browser/sessions/session_restore.h" |
10 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 10 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/browser_tabstrip.h" | 13 #include "chrome/browser/ui/browser_tabstrip.h" |
14 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 17 #include "content/public/browser/dom_storage_context.h" |
| 18 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
17 #include "grit/chromium_strings.h" | 20 #include "grit/chromium_strings.h" |
18 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 // A delegate for the InfoBar shown when the previous session has crashed. | 28 // A delegate for the InfoBar shown when the previous session has crashed. |
26 class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate { | 29 class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate, |
| 30 public content::NotificationObserver { |
27 public: | 31 public: |
28 explicit SessionCrashedInfoBarDelegate(InfoBarTabHelper* infobar_helper); | 32 explicit SessionCrashedInfoBarDelegate(InfoBarTabHelper* infobar_helper); |
29 | 33 |
30 private: | 34 private: |
31 virtual ~SessionCrashedInfoBarDelegate(); | 35 virtual ~SessionCrashedInfoBarDelegate(); |
32 | 36 |
33 // ConfirmInfoBarDelegate: | 37 // ConfirmInfoBarDelegate: |
34 virtual gfx::Image* GetIcon() const OVERRIDE; | 38 virtual gfx::Image* GetIcon() const OVERRIDE; |
35 virtual string16 GetMessageText() const OVERRIDE; | 39 virtual string16 GetMessageText() const OVERRIDE; |
36 virtual int GetButtons() const OVERRIDE; | 40 virtual int GetButtons() const OVERRIDE; |
37 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | 41 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
38 virtual bool Accept() OVERRIDE; | 42 virtual bool Accept() OVERRIDE; |
39 | 43 |
| 44 // content::NotificationObserver: |
| 45 virtual void Observe(int type, |
| 46 const content::NotificationSource& source, |
| 47 const content::NotificationDetails& details) OVERRIDE; |
| 48 |
| 49 content::NotificationRegistrar registrar_; |
| 50 bool accepted_; |
| 51 bool removed_notification_received_; |
| 52 Browser* browser_; |
| 53 |
40 DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); | 54 DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); |
41 }; | 55 }; |
42 | 56 |
43 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( | 57 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( |
44 InfoBarTabHelper* infobar_helper) | 58 InfoBarTabHelper* infobar_helper) |
45 : ConfirmInfoBarDelegate(infobar_helper) { | 59 : ConfirmInfoBarDelegate(infobar_helper), |
| 60 accepted_(false), |
| 61 removed_notification_received_(false), |
| 62 browser_(browser::FindBrowserWithWebContents(owner()->web_contents())) { |
| 63 // TODO(pkasting,marja): Once InfoBars own they delegates, this is not needed |
| 64 // any more. Then we can rely on delegates getting destroyed, and we can |
| 65 // initiate the session storage scavenging only in the destructor. (Currently, |
| 66 // info bars are leaked if they get closed while they're in background tabs.) |
| 67 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
| 68 content::NotificationService::AllSources()); |
46 } | 69 } |
47 | 70 |
48 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { | 71 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { |
| 72 // If the info bar wasn't accepted, it was either dismissed or expired. In |
| 73 // that case, session restore won't happen. |
| 74 if (!accepted_ && !removed_notification_received_) { |
| 75 content::BrowserContext::GetDefaultDOMStorageContext( |
| 76 browser_->profile())->StartScavengingUnusedSessionStorage(); |
| 77 } |
49 } | 78 } |
50 | 79 |
51 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { | 80 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { |
52 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 81 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
53 IDR_INFOBAR_RESTORE_SESSION); | 82 IDR_INFOBAR_RESTORE_SESSION); |
54 } | 83 } |
55 | 84 |
56 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { | 85 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { |
57 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); | 86 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); |
58 } | 87 } |
59 | 88 |
60 int SessionCrashedInfoBarDelegate::GetButtons() const { | 89 int SessionCrashedInfoBarDelegate::GetButtons() const { |
61 return BUTTON_OK; | 90 return BUTTON_OK; |
62 } | 91 } |
63 | 92 |
64 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( | 93 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( |
65 InfoBarButton button) const { | 94 InfoBarButton button) const { |
66 DCHECK_EQ(BUTTON_OK, button); | 95 DCHECK_EQ(BUTTON_OK, button); |
67 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); | 96 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); |
68 } | 97 } |
69 | 98 |
70 bool SessionCrashedInfoBarDelegate::Accept() { | 99 bool SessionCrashedInfoBarDelegate::Accept() { |
71 uint32 behavior = 0; | 100 uint32 behavior = 0; |
72 Browser* browser = | 101 if (browser_->tab_count() == 1 && |
73 browser::FindBrowserWithWebContents(owner()->web_contents()); | 102 chrome::GetWebContentsAt(browser_, 0)->GetURL() == |
74 if (browser->tab_count() == 1 && | |
75 chrome::GetWebContentsAt(browser, 0)->GetURL() == | |
76 GURL(chrome::kChromeUINewTabURL)) { | 103 GURL(chrome::kChromeUINewTabURL)) { |
77 // There is only one tab and its the new tab page, make session restore | 104 // There is only one tab and its the new tab page, make session restore |
78 // clobber it. | 105 // clobber it. |
79 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | 106 behavior = SessionRestore::CLOBBER_CURRENT_TAB; |
80 } | 107 } |
81 SessionRestore::RestoreSession( | 108 SessionRestore::RestoreSession( |
82 browser->profile(), browser, behavior, std::vector<GURL>()); | 109 browser_->profile(), browser_, behavior, std::vector<GURL>()); |
| 110 accepted_ = true; |
83 return true; | 111 return true; |
84 } | 112 } |
85 | 113 |
| 114 void SessionCrashedInfoBarDelegate::Observe( |
| 115 int type, |
| 116 const content::NotificationSource& source, |
| 117 const content::NotificationDetails& details) { |
| 118 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED); |
| 119 if (content::Details<std::pair<InfoBarDelegate*, bool> >(details)->first != |
| 120 this) |
| 121 return; |
| 122 if (!accepted_) { |
| 123 content::BrowserContext::GetDefaultDOMStorageContext( |
| 124 browser_->profile())->StartScavengingUnusedSessionStorage(); |
| 125 removed_notification_received_ = true; |
| 126 } |
| 127 } |
| 128 |
86 } // namespace | 129 } // namespace |
87 | 130 |
88 namespace chrome { | 131 namespace chrome { |
89 | 132 |
90 void ShowSessionCrashedPrompt(Browser* browser) { | 133 void ShowSessionCrashedPrompt(Browser* browser) { |
91 // Assume that if the user is launching incognito they were previously | 134 // Assume that if the user is launching incognito they were previously |
92 // running incognito so that we have nothing to restore from. | 135 // running incognito so that we have nothing to restore from. |
93 if (browser->profile()->IsOffTheRecord()) | 136 if (browser->profile()->IsOffTheRecord()) |
94 return; | 137 return; |
95 | 138 |
96 // In ChromeBot tests, there might be a race. This line appears to get | 139 // In ChromeBot tests, there might be a race. This line appears to get |
97 // called during shutdown and |tab| can be NULL. | 140 // called during shutdown and |tab| can be NULL. |
98 TabContents* tab = chrome::GetActiveTabContents(browser); | 141 TabContents* tab = chrome::GetActiveTabContents(browser); |
99 if (!tab) | 142 if (!tab) |
100 return; | 143 return; |
101 | 144 |
102 // Don't show the info-bar if there are already info-bars showing. | 145 // Don't show the info-bar if there are already info-bars showing. |
103 if (tab->infobar_tab_helper()->infobar_count() > 0) | 146 if (tab->infobar_tab_helper()->infobar_count() > 0) |
104 return; | 147 return; |
105 | 148 |
106 tab->infobar_tab_helper()->AddInfoBar( | 149 tab->infobar_tab_helper()->AddInfoBar( |
107 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); | 150 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); |
108 } | 151 } |
109 | 152 |
110 } // namespace chrome | 153 } // namespace chrome |
OLD | NEW |