Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / startup / session_crashed_infobar_delegate.cc
blobdadad034f1911102582f19143cd499e5974b87dd
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_service.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 "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/infobars/core/infobar.h"
18 #include "content/public/browser/dom_storage_context.h"
19 #include "content/public/browser/storage_partition.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/vector_icons_public.h"
24 // static
25 void SessionCrashedInfoBarDelegate::Create(Browser* browser) {
26 // Assume that if the user is launching incognito they were previously running
27 // incognito so that we have nothing to restore from.
28 // Also, in ChromeBot tests, there might be a race. This code appears to be
29 // called during shutdown when there is no active WebContents.
30 Profile* profile = browser->profile();
31 content::WebContents* web_contents =
32 browser->tab_strip_model()->GetActiveWebContents();
33 if (profile->IsOffTheRecord() || !web_contents)
34 return;
36 InfoBarService* infobar_service =
37 InfoBarService::FromWebContents(web_contents);
38 infobar_service->AddInfoBar(
39 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
40 new SessionCrashedInfoBarDelegate(profile))));
43 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate(Profile* profile)
44 : ConfirmInfoBarDelegate(),
45 accepted_(false),
46 profile_(profile) {
49 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() {
50 // If the info bar wasn't accepted, it was either dismissed or expired. In
51 // that case, session restore won't happen.
52 if (!accepted_) {
53 content::BrowserContext::GetDefaultStoragePartition(profile_)->
54 GetDOMStorageContext()->StartScavengingUnusedSessionStorage();
58 int SessionCrashedInfoBarDelegate::GetIconId() const {
59 return IDR_INFOBAR_RESTORE_SESSION;
62 gfx::VectorIconId SessionCrashedInfoBarDelegate::GetVectorIconId() const {
63 #if defined(OS_MACOSX)
64 return gfx::VectorIconId::VECTOR_ICON_NONE;
65 #else
66 return gfx::VectorIconId::SAD_TAB;
67 #endif
70 base::string16 SessionCrashedInfoBarDelegate::GetMessageText() const {
71 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE);
74 int SessionCrashedInfoBarDelegate::GetButtons() const {
75 return BUTTON_OK;
78 base::string16 SessionCrashedInfoBarDelegate::GetButtonLabel(
79 InfoBarButton button) const {
80 DCHECK_EQ(BUTTON_OK, button);
81 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON);
84 bool SessionCrashedInfoBarDelegate::Accept() {
85 Browser* browser = chrome::FindBrowserWithWebContents(
86 InfoBarService::WebContentsFromInfoBar(infobar()));
87 SessionRestore::RestoreSessionAfterCrash(browser);
88 accepted_ = true;
89 return true;