| 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_infobar_delegate.h" | 5 #include "chrome/browser/ui/startup/session_crashed_infobar_delegate.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_notification_types.h" | |
| 8 #include "chrome/browser/infobars/infobar.h" | 7 #include "chrome/browser/infobars/infobar.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/search.h" | 9 #include "chrome/browser/search/search.h" |
| 11 #include "chrome/browser/sessions/session_restore.h" | 10 #include "chrome/browser/sessions/session_restore.h" |
| 12 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 16 #include "content/public/browser/dom_storage_context.h" | 15 #include "content/public/browser/dom_storage_context.h" |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 19 #include "grit/chromium_strings.h" | 17 #include "grit/chromium_strings.h" |
| 20 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 21 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 23 | 21 |
| 24 | 22 |
| 25 // static | 23 // static |
| 26 void SessionCrashedInfoBarDelegate::Create(Browser* browser) { | 24 void SessionCrashedInfoBarDelegate::Create(Browser* browser) { |
| 27 // Assume that if the user is launching incognito they were previously running | 25 // Assume that if the user is launching incognito they were previously running |
| 28 // incognito so that we have nothing to restore from. | 26 // incognito so that we have nothing to restore from. |
| 29 // Also, in ChromeBot tests, there might be a race. This code appears to be | 27 // Also, in ChromeBot tests, there might be a race. This code appears to be |
| 30 // called during shutdown when there is no active WebContents. | 28 // called during shutdown when there is no active WebContents. |
| 31 Profile* profile = browser->profile(); | 29 Profile* profile = browser->profile(); |
| 32 content::WebContents* web_contents = | 30 content::WebContents* web_contents = |
| 33 browser->tab_strip_model()->GetActiveWebContents(); | 31 browser->tab_strip_model()->GetActiveWebContents(); |
| 34 if (profile->IsOffTheRecord() || !web_contents) | 32 if (profile->IsOffTheRecord() || !web_contents) |
| 35 return; | 33 return; |
| 36 | 34 |
| 37 InfoBarService* infobar_service = | 35 InfoBarService::FromWebContents(web_contents)->AddInfoBar( |
| 38 InfoBarService::FromWebContents(web_contents); | 36 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>( |
| 39 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( | 37 new SessionCrashedInfoBarDelegate(profile)))); |
| 40 new SessionCrashedInfoBarDelegate(infobar_service, profile))); | |
| 41 } | 38 } |
| 42 | 39 |
| 43 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( | 40 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate(Profile* profile) |
| 44 InfoBarService* infobar_service, | 41 : ConfirmInfoBarDelegate(), |
| 45 Profile* profile) | |
| 46 : ConfirmInfoBarDelegate(infobar_service), | |
| 47 accepted_(false), | 42 accepted_(false), |
| 48 removed_notification_received_(false), | |
| 49 profile_(profile) { | 43 profile_(profile) { |
| 50 // TODO(pkasting,marja): Once InfoBars own they delegates, this is not needed | |
| 51 // any more. Then we can rely on delegates getting destroyed, and we can | |
| 52 // initiate the session storage scavenging only in the destructor. (Currently, | |
| 53 // info bars are leaked if they get closed while they're in background tabs.) | |
| 54 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 55 content::NotificationService::AllSources()); | |
| 56 } | 44 } |
| 57 | 45 |
| 58 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { | 46 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { |
| 59 // If the info bar wasn't accepted, it was either dismissed or expired. In | 47 // If the info bar wasn't accepted, it was either dismissed or expired. In |
| 60 // that case, session restore won't happen. | 48 // that case, session restore won't happen. |
| 61 if (!accepted_ && !removed_notification_received_) { | 49 if (!accepted_) { |
| 62 content::BrowserContext::GetDefaultStoragePartition(profile_)-> | 50 content::BrowserContext::GetDefaultStoragePartition(profile_)-> |
| 63 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); | 51 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); |
| 64 } | 52 } |
| 65 } | 53 } |
| 66 | 54 |
| 67 int SessionCrashedInfoBarDelegate::GetIconID() const { | 55 int SessionCrashedInfoBarDelegate::GetIconID() const { |
| 68 return IDR_INFOBAR_RESTORE_SESSION; | 56 return IDR_INFOBAR_RESTORE_SESSION; |
| 69 } | 57 } |
| 70 | 58 |
| 71 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { | 59 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { |
| 72 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); | 60 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); |
| 73 } | 61 } |
| 74 | 62 |
| 75 int SessionCrashedInfoBarDelegate::GetButtons() const { | 63 int SessionCrashedInfoBarDelegate::GetButtons() const { |
| 76 return BUTTON_OK; | 64 return BUTTON_OK; |
| 77 } | 65 } |
| 78 | 66 |
| 79 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( | 67 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( |
| 80 InfoBarButton button) const { | 68 InfoBarButton button) const { |
| 81 DCHECK_EQ(BUTTON_OK, button); | 69 DCHECK_EQ(BUTTON_OK, button); |
| 82 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); | 70 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); |
| 83 } | 71 } |
| 84 | 72 |
| 85 bool SessionCrashedInfoBarDelegate::Accept() { | 73 bool SessionCrashedInfoBarDelegate::Accept() { |
| 86 uint32 behavior = 0; | 74 uint32 behavior = 0; |
| 87 Browser* browser = | 75 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 88 chrome::FindBrowserWithWebContents(owner()->web_contents()); | |
| 89 if (browser->tab_strip_model()->count() == 1) { | 76 if (browser->tab_strip_model()->count() == 1) { |
| 90 const content::WebContents* active_tab = | 77 const content::WebContents* active_tab = |
| 91 browser->tab_strip_model()->GetWebContentsAt(0); | 78 browser->tab_strip_model()->GetWebContentsAt(0); |
| 92 if (active_tab->GetURL() == GURL(chrome::kChromeUINewTabURL) || | 79 if (active_tab->GetURL() == GURL(chrome::kChromeUINewTabURL) || |
| 93 chrome::IsInstantNTP(active_tab)) { | 80 chrome::IsInstantNTP(active_tab)) { |
| 94 // There is only one tab and its the new tab page, make session restore | 81 // There is only one tab and its the new tab page, make session restore |
| 95 // clobber it. | 82 // clobber it. |
| 96 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | 83 behavior = SessionRestore::CLOBBER_CURRENT_TAB; |
| 97 } | 84 } |
| 98 } | 85 } |
| 99 SessionRestore::RestoreSession(browser->profile(), browser, | 86 SessionRestore::RestoreSession(browser->profile(), browser, |
| 100 browser->host_desktop_type(), behavior, | 87 browser->host_desktop_type(), behavior, |
| 101 std::vector<GURL>()); | 88 std::vector<GURL>()); |
| 102 accepted_ = true; | 89 accepted_ = true; |
| 103 return true; | 90 return true; |
| 104 } | 91 } |
| 105 | |
| 106 void SessionCrashedInfoBarDelegate::Observe( | |
| 107 int type, | |
| 108 const content::NotificationSource& source, | |
| 109 const content::NotificationDetails& details) { | |
| 110 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED); | |
| 111 if (content::Details<InfoBar::RemovedDetails>(details)->first != this) | |
| 112 return; | |
| 113 if (!accepted_) { | |
| 114 content::BrowserContext::GetDefaultStoragePartition(profile_)-> | |
| 115 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); | |
| 116 removed_notification_received_ = true; | |
| 117 } | |
| 118 } | |
| OLD | NEW |