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