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/tabs/pinned_tab_service.h" | 5 #include "chrome/browser/ui/tabs/pinned_tab_service.h" |
6 | 6 |
7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_list.h" | 9 #include "chrome/browser/ui/browser_list.h" |
10 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" | 10 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" |
11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
12 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
13 | 13 |
14 static bool IsLastNormalBrowser(Browser* browser) { | 14 namespace { |
| 15 |
| 16 bool IsOnlyNormalBrowser(Browser* browser) { |
15 for (BrowserList::const_iterator i = BrowserList::begin(); | 17 for (BrowserList::const_iterator i = BrowserList::begin(); |
16 i != BrowserList::end(); ++i) { | 18 i != BrowserList::end(); ++i) { |
17 if (*i != browser && (*i)->is_type_tabbed() && | 19 if (*i != browser && (*i)->is_type_tabbed() && |
18 (*i)->profile() == browser->profile()) { | 20 (*i)->profile() == browser->profile()) { |
19 return false; | 21 return false; |
20 } | 22 } |
21 } | 23 } |
22 return true; | 24 return true; |
23 } | 25 } |
24 | 26 |
| 27 } // namespace |
| 28 |
25 PinnedTabService::PinnedTabService(Profile* profile) | 29 PinnedTabService::PinnedTabService(Profile* profile) |
26 : profile_(profile), | 30 : profile_(profile), |
27 got_exiting_(false), | 31 save_pinned_tabs_(true), |
28 has_normal_browser_(false) { | 32 has_normal_browser_(false) { |
29 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | 33 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, |
30 content::NotificationService::AllBrowserContextsAndSources()); | 34 content::NotificationService::AllBrowserContextsAndSources()); |
31 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 35 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
32 content::NotificationService::AllSources()); | 36 content::NotificationService::AllSources()); |
33 registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, | 37 registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
34 content::NotificationService::AllSources()); | 38 content::NotificationService::AllSources()); |
| 39 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED, |
| 40 content::NotificationService::AllSources()); |
35 } | 41 } |
36 | 42 |
37 void PinnedTabService::Observe(int type, | 43 void PinnedTabService::Observe(int type, |
38 const content::NotificationSource& source, | 44 const content::NotificationSource& source, |
39 const content::NotificationDetails& details) { | 45 const content::NotificationDetails& details) { |
40 if (got_exiting_) | 46 // Saving of tabs happens when saving is enabled, and when either the user |
41 return; | 47 // exits the application or closes the last browser window. |
42 | 48 // Saving is disabled when the user exits the application to prevent the |
| 49 // pin state of all the open browsers being overwritten by the state of the |
| 50 // last browser window to close. |
| 51 // Saving is re-enabled when a browser window or tab is opened again. |
| 52 // Note, cancelling a shutdown (via onbeforeunload) will not re-enable pinned |
| 53 // tab saving immediately, to prevent the following situation: |
| 54 // * two windows are open, one with pinned tabs |
| 55 // * user exits |
| 56 // * pinned tabs are saved |
| 57 // * window with pinned tabs is closed |
| 58 // * other window blocks close with onbeforeunload |
| 59 // * user saves work, etc. then closes the window |
| 60 // * pinned tabs are saved, without the window with the pinned tabs, |
| 61 // over-writing the correct state. |
| 62 // Saving is re-enabled if a new tab or window is opened. |
43 switch (type) { | 63 switch (type) { |
44 case chrome::NOTIFICATION_BROWSER_OPENED: { | 64 case chrome::NOTIFICATION_BROWSER_OPENED: { |
45 Browser* browser = content::Source<Browser>(source).ptr(); | 65 Browser* browser = content::Source<Browser>(source).ptr(); |
46 if (!has_normal_browser_ && browser->is_type_tabbed() && | 66 if (!has_normal_browser_ && browser->is_type_tabbed() && |
47 browser->profile() == profile_) { | 67 browser->profile() == profile_) { |
48 has_normal_browser_ = true; | 68 has_normal_browser_ = true; |
49 } | 69 } |
| 70 save_pinned_tabs_ = true; |
| 71 break; |
| 72 } |
| 73 |
| 74 case chrome::NOTIFICATION_TAB_ADDED: { |
| 75 save_pinned_tabs_ = true; |
50 break; | 76 break; |
51 } | 77 } |
52 | 78 |
53 case chrome::NOTIFICATION_BROWSER_CLOSING: { | 79 case chrome::NOTIFICATION_BROWSER_CLOSING: { |
54 Browser* browser = content::Source<Browser>(source).ptr(); | 80 Browser* browser = content::Source<Browser>(source).ptr(); |
55 if (has_normal_browser_ && browser->profile() == profile_) { | 81 if (has_normal_browser_ && save_pinned_tabs_ && |
56 if (*(content::Details<bool>(details)).ptr()) { | 82 browser->profile() == profile_) { |
57 GotExit(); | 83 if (IsOnlyNormalBrowser(browser)) { |
58 } else if (IsLastNormalBrowser(browser)) { | |
59 has_normal_browser_ = false; | 84 has_normal_browser_ = false; |
60 PinnedTabCodec::WritePinnedTabs(profile_); | 85 PinnedTabCodec::WritePinnedTabs(profile_); |
61 } | 86 } |
62 } | 87 } |
63 break; | 88 break; |
64 } | 89 } |
65 | 90 |
66 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { | 91 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
67 if (has_normal_browser_) | 92 if (has_normal_browser_ && save_pinned_tabs_) { |
68 GotExit(); | 93 PinnedTabCodec::WritePinnedTabs(profile_); |
| 94 save_pinned_tabs_ = false; |
| 95 } |
69 break; | 96 break; |
70 } | 97 } |
71 | 98 |
72 default: | 99 default: |
73 NOTREACHED(); | 100 NOTREACHED(); |
74 } | 101 } |
75 } | 102 } |
76 | |
77 void PinnedTabService::GotExit() { | |
78 DCHECK(!got_exiting_); | |
79 got_exiting_ = true; | |
80 PinnedTabCodec::WritePinnedTabs(profile_); | |
81 } | |
OLD | NEW |