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 implementation. | |
Peter Kasting
2012/08/07 01:31:54
Nit: s/ implementation./:/ for consistency
marja
2012/08/07 07:31:22
Done.
| |
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::DOMStorageContext* dom_storage_context = | |
Peter Kasting
2012/08/07 01:31:54
Nit: I would do:
content::BrowserContext::Get
marja
2012/08/07 07:31:22
Done.
| |
76 content::BrowserContext::GetDefaultDOMStorageContext( | |
77 browser_->profile()); | |
78 dom_storage_context->StartScavengingUnusedSessionStorage(); | |
79 } | |
49 } | 80 } |
50 | 81 |
51 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { | 82 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { |
52 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 83 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
53 IDR_INFOBAR_RESTORE_SESSION); | 84 IDR_INFOBAR_RESTORE_SESSION); |
54 } | 85 } |
55 | 86 |
56 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { | 87 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { |
57 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); | 88 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); |
58 } | 89 } |
59 | 90 |
60 int SessionCrashedInfoBarDelegate::GetButtons() const { | 91 int SessionCrashedInfoBarDelegate::GetButtons() const { |
61 return BUTTON_OK; | 92 return BUTTON_OK; |
62 } | 93 } |
63 | 94 |
64 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( | 95 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( |
65 InfoBarButton button) const { | 96 InfoBarButton button) const { |
66 DCHECK_EQ(BUTTON_OK, button); | 97 DCHECK_EQ(BUTTON_OK, button); |
67 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); | 98 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); |
68 } | 99 } |
69 | 100 |
70 bool SessionCrashedInfoBarDelegate::Accept() { | 101 bool SessionCrashedInfoBarDelegate::Accept() { |
71 uint32 behavior = 0; | 102 uint32 behavior = 0; |
72 Browser* browser = | 103 if (browser_->tab_count() == 1 && |
73 browser::FindBrowserWithWebContents(owner()->web_contents()); | 104 chrome::GetWebContentsAt(browser_, 0)->GetURL() == |
74 if (browser->tab_count() == 1 && | |
75 chrome::GetWebContentsAt(browser, 0)->GetURL() == | |
76 GURL(chrome::kChromeUINewTabURL)) { | 105 GURL(chrome::kChromeUINewTabURL)) { |
77 // There is only one tab and its the new tab page, make session restore | 106 // There is only one tab and its the new tab page, make session restore |
78 // clobber it. | 107 // clobber it. |
79 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | 108 behavior = SessionRestore::CLOBBER_CURRENT_TAB; |
80 } | 109 } |
81 SessionRestore::RestoreSession( | 110 SessionRestore::RestoreSession( |
82 browser->profile(), browser, behavior, std::vector<GURL>()); | 111 browser_->profile(), browser_, behavior, std::vector<GURL>()); |
112 accepted_ = true; | |
83 return true; | 113 return true; |
84 } | 114 } |
85 | 115 |
116 void SessionCrashedInfoBarDelegate::Observe( | |
117 int type, | |
118 const content::NotificationSource& source, | |
119 const content::NotificationDetails& details) { | |
120 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED); | |
121 content::Details<std::pair<InfoBarDelegate*, bool> > det(details); | |
Peter Kasting
2012/08/07 01:31:54
Nit: |det| is not a good variable name, and you sh
marja
2012/08/07 07:31:22
Done.
| |
122 if (det->first != this) | |
123 return; | |
124 // See ~SessionCrashedInfoBarDelegate and the TODO in | |
Peter Kasting
2012/08/07 01:31:54
Nit: No need for this comment, the TODO in the con
marja
2012/08/07 07:31:22
Done.
| |
125 // SessionCrashedInfoBarDelegate. | |
126 if (!accepted_) { | |
127 content::DOMStorageContext* dom_storage_context = | |
128 content::BrowserContext::GetDefaultDOMStorageContext( | |
129 browser_->profile()); | |
130 dom_storage_context->StartScavengingUnusedSessionStorage(); | |
131 removed_notification_received_ = true; | |
132 } | |
133 } | |
134 | |
86 } // namespace | 135 } // namespace |
87 | 136 |
88 namespace chrome { | 137 namespace chrome { |
89 | 138 |
90 void ShowSessionCrashedPrompt(Browser* browser) { | 139 void ShowSessionCrashedPrompt(Browser* browser) { |
91 // Assume that if the user is launching incognito they were previously | 140 // Assume that if the user is launching incognito they were previously |
92 // running incognito so that we have nothing to restore from. | 141 // running incognito so that we have nothing to restore from. |
93 if (browser->profile()->IsOffTheRecord()) | 142 if (browser->profile()->IsOffTheRecord()) |
94 return; | 143 return; |
95 | 144 |
96 // In ChromeBot tests, there might be a race. This line appears to get | 145 // In ChromeBot tests, there might be a race. This line appears to get |
97 // called during shutdown and |tab| can be NULL. | 146 // called during shutdown and |tab| can be NULL. |
98 TabContents* tab = chrome::GetActiveTabContents(browser); | 147 TabContents* tab = chrome::GetActiveTabContents(browser); |
99 if (!tab) | 148 if (!tab) |
100 return; | 149 return; |
101 | 150 |
102 // Don't show the info-bar if there are already info-bars showing. | 151 // Don't show the info-bar if there are already info-bars showing. |
103 if (tab->infobar_tab_helper()->infobar_count() > 0) | 152 if (tab->infobar_tab_helper()->infobar_count() > 0) |
104 return; | 153 return; |
105 | 154 |
106 tab->infobar_tab_helper()->AddInfoBar( | 155 tab->infobar_tab_helper()->AddInfoBar( |
107 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); | 156 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); |
108 } | 157 } |
109 | 158 |
110 } // namespace chrome | 159 } // namespace chrome |
OLD | NEW |