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/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/browser_list.h" | 10 #include "chrome/browser/ui/browser_list.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 } | 23 } |
24 } | 24 } |
25 return true; | 25 return true; |
26 } | 26 } |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 PinnedTabService::PinnedTabService(Profile* profile) | 30 PinnedTabService::PinnedTabService(Profile* profile) |
31 : profile_(profile), | 31 : profile_(profile), |
32 save_pinned_tabs_(true), | 32 save_pinned_tabs_(true), |
33 has_normal_browser_(false) { | 33 has_normal_browser_(false), |
| 34 browser_list_observer_(this) { |
34 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, | 35 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED, |
35 content::NotificationService::AllBrowserContextsAndSources()); | 36 content::NotificationService::AllBrowserContextsAndSources()); |
36 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
37 content::NotificationService::AllSources()); | |
38 registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, | 37 registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
39 content::NotificationService::AllSources()); | 38 content::NotificationService::AllSources()); |
40 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED, | 39 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED, |
41 content::NotificationService::AllSources()); | 40 content::NotificationService::AllSources()); |
| 41 browser_list_observer_.Add(BrowserList::GetInstance()); |
42 } | 42 } |
43 | 43 |
| 44 PinnedTabService::~PinnedTabService() {} |
| 45 |
44 void PinnedTabService::Observe(int type, | 46 void PinnedTabService::Observe(int type, |
45 const content::NotificationSource& source, | 47 const content::NotificationSource& source, |
46 const content::NotificationDetails& details) { | 48 const content::NotificationDetails& details) { |
47 // Saving of tabs happens when saving is enabled, and when either the user | 49 // Saving of tabs happens when saving is enabled, and when either the user |
48 // exits the application or closes the last browser window. | 50 // exits the application or closes the last browser window. |
49 // Saving is disabled when the user exits the application to prevent the | 51 // Saving is disabled when the user exits the application to prevent the |
50 // pin state of all the open browsers being overwritten by the state of the | 52 // pin state of all the open browsers being overwritten by the state of the |
51 // last browser window to close. | 53 // last browser window to close. |
52 // Saving is re-enabled when a browser window or tab is opened again. | 54 // Saving is re-enabled when a browser window or tab is opened again. |
53 // Note, cancelling a shutdown (via onbeforeunload) will not re-enable pinned | 55 // Note, cancelling a shutdown (via onbeforeunload) will not re-enable pinned |
(...skipping 16 matching lines...) Expand all Loading... |
70 } | 72 } |
71 save_pinned_tabs_ = true; | 73 save_pinned_tabs_ = true; |
72 break; | 74 break; |
73 } | 75 } |
74 | 76 |
75 case chrome::NOTIFICATION_TAB_ADDED: { | 77 case chrome::NOTIFICATION_TAB_ADDED: { |
76 save_pinned_tabs_ = true; | 78 save_pinned_tabs_ = true; |
77 break; | 79 break; |
78 } | 80 } |
79 | 81 |
80 case chrome::NOTIFICATION_BROWSER_CLOSING: { | |
81 Browser* browser = content::Source<Browser>(source).ptr(); | |
82 if (has_normal_browser_ && save_pinned_tabs_ && | |
83 browser->profile() == profile_) { | |
84 if (IsOnlyNormalBrowser(browser)) { | |
85 has_normal_browser_ = false; | |
86 PinnedTabCodec::WritePinnedTabs(profile_); | |
87 } | |
88 } | |
89 break; | |
90 } | |
91 | |
92 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { | 82 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
93 if (has_normal_browser_ && save_pinned_tabs_) { | 83 if (has_normal_browser_ && save_pinned_tabs_) { |
94 PinnedTabCodec::WritePinnedTabs(profile_); | 84 PinnedTabCodec::WritePinnedTabs(profile_); |
95 save_pinned_tabs_ = false; | 85 save_pinned_tabs_ = false; |
96 } | 86 } |
97 break; | 87 break; |
98 } | 88 } |
99 | 89 |
100 default: | 90 default: |
101 NOTREACHED(); | 91 NOTREACHED(); |
102 } | 92 } |
103 } | 93 } |
| 94 |
| 95 void PinnedTabService::OnBrowserClosing(Browser* browser) { |
| 96 if (has_normal_browser_ && save_pinned_tabs_ && |
| 97 browser->profile() == profile_ && IsOnlyNormalBrowser(browser)) { |
| 98 has_normal_browser_ = false; |
| 99 PinnedTabCodec::WritePinnedTabs(profile_); |
| 100 } |
| 101 } |
OLD | NEW |