| 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" |
| 11 #include "chrome/browser/ui/browser_list_observer.h" |
| 11 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" | 12 #include "chrome/browser/ui/tabs/pinned_tab_codec.h" |
| 12 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Returns true if |browser| is the only normal (tabbed) browser for |browser|'s | 17 // Returns true if |browser| is the only normal (tabbed) browser for |browser|'s |
| 17 // profile (across all desktops). | 18 // profile (across all desktops). |
| 18 bool IsOnlyNormalBrowser(Browser* browser) { | 19 bool IsOnlyNormalBrowser(Browser* browser) { |
| 19 for (auto* b : *BrowserList::GetInstance()) { | 20 for (auto* b : *BrowserList::GetInstance()) { |
| 20 if (b != browser && b->is_type_tabbed() && | 21 if (b != browser && b->is_type_tabbed() && |
| 21 b->profile() == browser->profile()) { | 22 b->profile() == browser->profile()) { |
| 22 return false; | 23 return false; |
| 23 } | 24 } |
| 24 } | 25 } |
| 25 return true; | 26 return true; |
| 26 } | 27 } |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 PinnedTabService::PinnedTabService(Profile* profile) | 31 PinnedTabService::PinnedTabService(Profile* profile) |
| 31 : profile_(profile), | 32 : profile_(profile), |
| 32 save_pinned_tabs_(true), | 33 save_pinned_tabs_(true), |
| 33 has_normal_browser_(false) { | 34 has_normal_browser_(false) { |
| 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 BrowserList::AddObserver(this); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void PinnedTabService::Observe(int type, | 44 void PinnedTabService::Observe(int type, |
| 45 const content::NotificationSource& source, | 45 const content::NotificationSource& source, |
| 46 const content::NotificationDetails& details) { | 46 const content::NotificationDetails& details) { |
| 47 // Saving of tabs happens when saving is enabled, and when either the user | 47 // Saving of tabs happens when saving is enabled, and when either the user |
| 48 // exits the application or closes the last browser window. | 48 // exits the application or closes the last browser window. |
| 49 // Saving is disabled when the user exits the application to prevent the | 49 // 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 | 50 // pin state of all the open browsers being overwritten by the state of the |
| 51 // last browser window to close. | 51 // last browser window to close. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 } | 70 } |
| 71 save_pinned_tabs_ = true; | 71 save_pinned_tabs_ = true; |
| 72 break; | 72 break; |
| 73 } | 73 } |
| 74 | 74 |
| 75 case chrome::NOTIFICATION_TAB_ADDED: { | 75 case chrome::NOTIFICATION_TAB_ADDED: { |
| 76 save_pinned_tabs_ = true; | 76 save_pinned_tabs_ = true; |
| 77 break; | 77 break; |
| 78 } | 78 } |
| 79 | 79 |
| 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: { | 80 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
| 93 if (has_normal_browser_ && save_pinned_tabs_) { | 81 if (has_normal_browser_ && save_pinned_tabs_) { |
| 94 PinnedTabCodec::WritePinnedTabs(profile_); | 82 PinnedTabCodec::WritePinnedTabs(profile_); |
| 95 save_pinned_tabs_ = false; | 83 save_pinned_tabs_ = false; |
| 96 } | 84 } |
| 97 break; | 85 break; |
| 98 } | 86 } |
| 99 | 87 |
| 100 default: | 88 default: |
| 101 NOTREACHED(); | 89 NOTREACHED(); |
| 102 } | 90 } |
| 103 } | 91 } |
| 92 |
| 93 void PinnedTabService::OnBrowserCloseStarted(Browser* browser) { |
| 94 if (has_normal_browser_ && save_pinned_tabs_ && |
| 95 browser->profile() == profile_) { |
| 96 if (IsOnlyNormalBrowser(browser)) { |
| 97 has_normal_browser_ = false; |
| 98 PinnedTabCodec::WritePinnedTabs(profile_); |
| 99 } |
| 100 } |
| 101 } |
| OLD | NEW |