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" |
7 #include "chrome/browser/infobars/infobar.h" | 8 #include "chrome/browser/infobars/infobar.h" |
8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/search/search.h" | 10 #include "chrome/browser/search/search.h" |
10 #include "chrome/browser/sessions/session_restore.h" | 11 #include "chrome/browser/sessions/session_restore.h" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
15 #include "content/public/browser/dom_storage_context.h" | 16 #include "content/public/browser/dom_storage_context.h" |
| 17 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/storage_partition.h" | 18 #include "content/public/browser/storage_partition.h" |
17 #include "grit/chromium_strings.h" | 19 #include "grit/chromium_strings.h" |
18 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
21 | 23 |
22 | 24 |
23 // static | 25 // static |
24 void SessionCrashedInfoBarDelegate::Create(Browser* browser) { | 26 void SessionCrashedInfoBarDelegate::Create(Browser* browser) { |
25 // Assume that if the user is launching incognito they were previously running | 27 // Assume that if the user is launching incognito they were previously running |
26 // incognito so that we have nothing to restore from. | 28 // incognito so that we have nothing to restore from. |
27 // Also, in ChromeBot tests, there might be a race. This code appears to be | 29 // Also, in ChromeBot tests, there might be a race. This code appears to be |
28 // called during shutdown when there is no active WebContents. | 30 // called during shutdown when there is no active WebContents. |
29 Profile* profile = browser->profile(); | 31 Profile* profile = browser->profile(); |
30 content::WebContents* web_contents = | 32 content::WebContents* web_contents = |
31 browser->tab_strip_model()->GetActiveWebContents(); | 33 browser->tab_strip_model()->GetActiveWebContents(); |
32 if (profile->IsOffTheRecord() || !web_contents) | 34 if (profile->IsOffTheRecord() || !web_contents) |
33 return; | 35 return; |
34 | 36 |
35 InfoBarService::FromWebContents(web_contents)->AddInfoBar( | 37 InfoBarService* infobar_service = |
36 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>( | 38 InfoBarService::FromWebContents(web_contents); |
37 new SessionCrashedInfoBarDelegate(profile)))); | 39 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( |
| 40 new SessionCrashedInfoBarDelegate(infobar_service, profile))); |
38 } | 41 } |
39 | 42 |
40 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate(Profile* profile) | 43 SessionCrashedInfoBarDelegate::SessionCrashedInfoBarDelegate( |
41 : ConfirmInfoBarDelegate(), | 44 InfoBarService* infobar_service, |
| 45 Profile* profile) |
| 46 : ConfirmInfoBarDelegate(infobar_service), |
42 accepted_(false), | 47 accepted_(false), |
| 48 removed_notification_received_(false), |
43 profile_(profile) { | 49 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()); |
44 } | 56 } |
45 | 57 |
46 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { | 58 SessionCrashedInfoBarDelegate::~SessionCrashedInfoBarDelegate() { |
47 // If the info bar wasn't accepted, it was either dismissed or expired. In | 59 // If the info bar wasn't accepted, it was either dismissed or expired. In |
48 // that case, session restore won't happen. | 60 // that case, session restore won't happen. |
49 if (!accepted_) { | 61 if (!accepted_ && !removed_notification_received_) { |
50 content::BrowserContext::GetDefaultStoragePartition(profile_)-> | 62 content::BrowserContext::GetDefaultStoragePartition(profile_)-> |
51 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); | 63 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); |
52 } | 64 } |
53 } | 65 } |
54 | 66 |
55 int SessionCrashedInfoBarDelegate::GetIconID() const { | 67 int SessionCrashedInfoBarDelegate::GetIconID() const { |
56 return IDR_INFOBAR_RESTORE_SESSION; | 68 return IDR_INFOBAR_RESTORE_SESSION; |
57 } | 69 } |
58 | 70 |
59 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { | 71 string16 SessionCrashedInfoBarDelegate::GetMessageText() const { |
60 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); | 72 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_MESSAGE); |
61 } | 73 } |
62 | 74 |
63 int SessionCrashedInfoBarDelegate::GetButtons() const { | 75 int SessionCrashedInfoBarDelegate::GetButtons() const { |
64 return BUTTON_OK; | 76 return BUTTON_OK; |
65 } | 77 } |
66 | 78 |
67 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( | 79 string16 SessionCrashedInfoBarDelegate::GetButtonLabel( |
68 InfoBarButton button) const { | 80 InfoBarButton button) const { |
69 DCHECK_EQ(BUTTON_OK, button); | 81 DCHECK_EQ(BUTTON_OK, button); |
70 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); | 82 return l10n_util::GetStringUTF16(IDS_SESSION_CRASHED_VIEW_RESTORE_BUTTON); |
71 } | 83 } |
72 | 84 |
73 bool SessionCrashedInfoBarDelegate::Accept() { | 85 bool SessionCrashedInfoBarDelegate::Accept() { |
74 uint32 behavior = 0; | 86 uint32 behavior = 0; |
75 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 87 Browser* browser = |
| 88 chrome::FindBrowserWithWebContents(owner()->web_contents()); |
76 if (browser->tab_strip_model()->count() == 1) { | 89 if (browser->tab_strip_model()->count() == 1) { |
77 const content::WebContents* active_tab = | 90 const content::WebContents* active_tab = |
78 browser->tab_strip_model()->GetWebContentsAt(0); | 91 browser->tab_strip_model()->GetWebContentsAt(0); |
79 if (active_tab->GetURL() == GURL(chrome::kChromeUINewTabURL) || | 92 if (active_tab->GetURL() == GURL(chrome::kChromeUINewTabURL) || |
80 chrome::IsInstantNTP(active_tab)) { | 93 chrome::IsInstantNTP(active_tab)) { |
81 // 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 |
82 // clobber it. | 95 // clobber it. |
83 behavior = SessionRestore::CLOBBER_CURRENT_TAB; | 96 behavior = SessionRestore::CLOBBER_CURRENT_TAB; |
84 } | 97 } |
85 } | 98 } |
86 SessionRestore::RestoreSession(browser->profile(), browser, | 99 SessionRestore::RestoreSession(browser->profile(), browser, |
87 browser->host_desktop_type(), behavior, | 100 browser->host_desktop_type(), behavior, |
88 std::vector<GURL>()); | 101 std::vector<GURL>()); |
89 accepted_ = true; | 102 accepted_ = true; |
90 return true; | 103 return true; |
91 } | 104 } |
| 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 |