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