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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/startup/session_crashed_infobar_delegate.h"
7 #include "chrome/browser/infobars/infobar.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/search/search.h"
10 #include "chrome/browser/sessions/session_restore.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/dom_storage_context.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/generated_resources.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
24 void SessionCrashedInfoBarDelegate::Create(Browser
* browser
) {
25 // Assume that if the user is launching incognito they were previously running
26 // incognito so that we have nothing to restore from.
27 // Also, in ChromeBot tests, there might be a race. This code appears to be
28 // called during shutdown when there is no active WebContents.
29 Profile
* profile
= browser
->profile();
30 content::WebContents
* web_contents
=
31 browser
->tab_strip_model()->GetActiveWebContents();
32 if (profile
->IsOffTheRecord() || !web_contents
)
35 InfoBarService::FromWebContents(web_contents
)->AddInfoBar(
36 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
37 new SessionCrashedInfoBarDelegate(profile
))));
40 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate(Profile
* profile
)
41 : ConfirmInfoBarDelegate(),
46 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() {
47 // If the info bar wasn't accepted, it was either dismissed or expired. In
48 // that case, session restore won't happen.
50 content::BrowserContext::GetDefaultStoragePartition(profile_
)->
51 GetDOMStorageContext()->StartScavengingUnusedSessionStorage();
55 int SessionCrashedInfoBarDelegate::GetIconID() const {
56 return IDR_INFOBAR_RESTORE_SESSION
;
59 base::string16
SessionCrashedInfoBarDelegate::GetMessageText() const {
60 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE
);
63 int SessionCrashedInfoBarDelegate::GetButtons() const {
67 base::string16
SessionCrashedInfoBarDelegate::GetButtonLabel(
68 InfoBarButton button
) const {
69 DCHECK_EQ(BUTTON_OK
, button
);
70 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON
);
73 bool SessionCrashedInfoBarDelegate::Accept() {
75 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents());
76 if (browser
->tab_strip_model()->count() == 1) {
77 const content::WebContents
* active_tab
=
78 browser
->tab_strip_model()->GetWebContentsAt(0);
79 if (active_tab
->GetURL() == GURL(chrome::kChromeUINewTabURL
) ||
80 chrome::IsInstantNTP(active_tab
)) {
81 // There is only one tab and its the new tab page, make session restore
83 behavior
= SessionRestore::CLOBBER_CURRENT_TAB
;
86 SessionRestore::RestoreSession(browser
->profile(), browser
,
87 browser
->host_desktop_type(), behavior
,