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 "grit/theme_resources_standard.h" | 23 #include "grit/theme_resources_standard.h" |
21 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
23 | 26 |
24 namespace { | 27 namespace { |
25 | 28 |
26 // A delegate for the InfoBar shown when the previous session has crashed. | 29 // A delegate for the InfoBar shown when the previous session has crashed. |
27 class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate { | 30 class SessionCrashedInfoBarDelegate : public ConfirmInfoBarDelegate, |
31 public content::NotificationObserver { | |
28 public: | 32 public: |
29 explicit SessionCrashedInfoBarDelegate(InfoBarTabHelper* infobar_helper); | 33 explicit SessionCrashedInfoBarDelegate(InfoBarTabHelper* infobar_helper); |
30 | 34 |
31 private: | 35 private: |
32 virtual ~SessionCrashedInfoBarDelegate(); | 36 virtual ~SessionCrashedInfoBarDelegate(); |
33 | 37 |
34 // ConfirmInfoBarDelegate: | 38 // ConfirmInfoBarDelegate: |
35 virtual gfx::Image* GetIcon() const OVERRIDE; | 39 virtual gfx::Image* GetIcon() const OVERRIDE; |
36 virtual string16 GetMessageText() const OVERRIDE; | 40 virtual string16 GetMessageText() const OVERRIDE; |
37 virtual int GetButtons() const OVERRIDE; | 41 virtual int GetButtons() const OVERRIDE; |
38 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | 42 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
39 virtual bool Accept() OVERRIDE; | 43 virtual bool Accept() OVERRIDE; |
40 | 44 |
45 // content::NotificationObserver implementation. | |
46 virtual void Observe(int type, | |
47 const content::NotificationSource& source, | |
48 const content::NotificationDetails& details) OVERRIDE; | |
49 | |
50 content::NotificationRegistrar registrar_; | |
51 bool accepted_; | |
52 Browser* browser_; | |
53 | |
41 DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); | 54 DISALLOW_COPY_AND_ASSIGN(SessionCrashedInfoBarDelegate); |
42 }; | 55 }; |
43 | 56 |
44 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( | 57 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( |
45 InfoBarTabHelper* infobar_helper) | 58 InfoBarTabHelper* infobar_helper) |
46 : ConfirmInfoBarDelegate(infobar_helper) { | 59 : ConfirmInfoBarDelegate(infobar_helper), |
60 accepted_(false), | |
61 browser_(browser::FindBrowserWithWebContents(owner()->web_contents())) { | |
62 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
63 content::NotificationService::AllSources()); | |
47 } | 64 } |
48 | 65 |
49 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { | 66 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { |
50 } | 67 } |
51 | 68 |
52 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { | 69 gfx::Image* SessionCrashedInfoBarDelegate::GetIcon() const { |
53 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 70 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
54 IDR_INFOBAR_RESTORE_SESSION); | 71 IDR_INFOBAR_RESTORE_SESSION); |
55 } | 72 } |
56 | 73 |
57 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { | 74 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { |
58 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); | 75 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); |
59 } | 76 } |
60 | 77 |
61 int SessionCrashedInfoBarDelegate::GetButtons() const { | 78 int SessionCrashedInfoBarDelegate::GetButtons() const { |
62 return BUTTON_OK; | 79 return BUTTON_OK; |
63 } | 80 } |
64 | 81 |
65 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( | 82 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( |
66 InfoBarButton button) const { | 83 InfoBarButton button) const { |
67 DCHECK_EQ(BUTTON_OK, button); | 84 DCHECK_EQ(BUTTON_OK, button); |
68 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); | 85 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); |
69 } | 86 } |
70 | 87 |
71 bool SessionCrashedInfoBarDelegate::Accept() { | 88 bool SessionCrashedInfoBarDelegate::Accept() { |
72 uint32 behavior = 0; | 89 uint32 behavior = 0; |
73 Browser* browser = | 90 if (browser_->tab_count() == 1 && |
74 browser::FindBrowserWithWebContents(owner()->web_contents()); | 91 chrome::GetWebContentsAt(browser_, 0)->GetURL() == |
75 if (browser->tab_count() == 1 && | |
76 chrome::GetWebContentsAt(browser, 0)->GetURL() == | |
77 GURL(chrome::kChromeUINewTabURL)) { | 92 GURL(chrome::kChromeUINewTabURL)) { |
78 // There is only one tab and its the new tab page, make session restore | 93 // There is only one tab and its the new tab page, make session restore |
79 // clobber it. | 94 // clobber it. |
80 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | 95 behavior = SessionRestore::CLOBBER_CURRENT_TAB; |
81 } | 96 } |
82 SessionRestore::RestoreSession( | 97 SessionRestore::RestoreSession( |
83 browser->profile(), browser, behavior, std::vector<GURL>()); | 98 browser_->profile(), browser_, behavior, std::vector<GURL>()); |
99 accepted_ = true; | |
84 return true; | 100 return true; |
85 } | 101 } |
86 | 102 |
103 void SessionCrashedInfoBarDelegate::Observe( | |
104 int type, | |
105 const content::NotificationSource& source, | |
106 const content::NotificationDetails& details) { | |
107 switch (type) { | |
Peter Kasting
2012/07/16 17:41:02
Nit: Do a DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTE
marja
2012/08/06 11:53:33
Done.
| |
108 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: | |
109 // If the info bar wasn't accepted, it was either dismissed or expired. In | |
110 // that case, session restore won't happen. | |
111 if (!accepted_) { | |
Peter Kasting
2012/07/16 17:41:02
You're running this code when any infobar is close
marja
2012/08/06 11:53:33
Ahh, thanks for pointing that out.
Done.
| |
112 content::DOMStorageContext* dom_storage_context = | |
113 content::BrowserContext::GetDOMStorageContext(browser_->profile()); | |
114 dom_storage_context->StartScavengingUnusedSessionStorage(); | |
115 } | |
116 break; | |
117 default: | |
118 NOTREACHED(); | |
119 break; | |
120 } | |
121 } | |
122 | |
87 } // namespace | 123 } // namespace |
88 | 124 |
89 namespace chrome { | 125 namespace chrome { |
90 | 126 |
91 void ShowSessionCrashedPrompt(Browser* browser) { | 127 void ShowSessionCrashedPrompt(Browser* browser) { |
92 // Assume that if the user is launching incognito they were previously | 128 // Assume that if the user is launching incognito they were previously |
93 // running incognito so that we have nothing to restore from. | 129 // running incognito so that we have nothing to restore from. |
94 if (browser->profile()->IsOffTheRecord()) | 130 if (browser->profile()->IsOffTheRecord()) |
95 return; | 131 return; |
96 | 132 |
97 // In ChromeBot tests, there might be a race. This line appears to get | 133 // In ChromeBot tests, there might be a race. This line appears to get |
98 // called during shutdown and |tab| can be NULL. | 134 // called during shutdown and |tab| can be NULL. |
99 TabContents* tab = chrome::GetActiveTabContents(browser); | 135 TabContents* tab = chrome::GetActiveTabContents(browser); |
100 if (!tab) | 136 if (!tab) |
101 return; | 137 return; |
102 | 138 |
103 // Don't show the info-bar if there are already info-bars showing. | 139 // Don't show the info-bar if there are already info-bars showing. |
104 if (tab->infobar_tab_helper()->infobar_count() > 0) | 140 if (tab->infobar_tab_helper()->infobar_count() > 0) |
105 return; | 141 return; |
106 | 142 |
107 tab->infobar_tab_helper()->AddInfoBar( | 143 tab->infobar_tab_helper()->AddInfoBar( |
108 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); | 144 new SessionCrashedInfoBarDelegate(tab->infobar_tab_helper())); |
109 } | 145 } |
110 | 146 |
111 } // namespace chrome | 147 } // namespace chrome |
OLD | NEW |