8d3ffa9e1c6f8eb59d6bebf07239f698710148ee
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / startup / session_crashed_infobar_delegate.cc
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.
4
5 #include "chrome/browser/ui/startup/session_crashed_infobar_delegate.h"
6
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"
21
22
23 // static
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)
33     return;
34
35   InfoBarService::FromWebContents(web_contents)->AddInfoBar(
36       ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
37           new SessionCrashedInfoBarDelegate(profile))));
38 }
39
40 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate(Profile* profile)
41     : ConfirmInfoBarDelegate(),
42       accepted_(false),
43       profile_(profile) {
44 }
45
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.
49   if (!accepted_) {
50     content::BrowserContext::GetDefaultStoragePartition(profile_)->
51         GetDOMStorageContext()->StartScavengingUnusedSessionStorage();
52   }
53 }
54
55 int SessionCrashedInfoBarDelegate::GetIconID() const {
56   return IDR_INFOBAR_RESTORE_SESSION;
57 }
58
59 base::string16 SessionCrashedInfoBarDelegate::GetMessageText() const {
60   return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE);
61 }
62
63 int SessionCrashedInfoBarDelegate::GetButtons() const {
64   return BUTTON_OK;
65 }
66
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);
71 }
72
73 bool SessionCrashedInfoBarDelegate::Accept() {
74   uint32 behavior = 0;
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
82       // clobber it.
83       behavior = SessionRestore::CLOBBER_CURRENT_TAB;
84     }
85   }
86   SessionRestore::RestoreSession(browser->profile(), browser,
87                                  browser->host_desktop_type(), behavior,
88                                  std::vector<GURL>());
89   accepted_ = true;
90   return true;
91 }