Chromium Code Reviews| 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 IsLastNormalBrowser(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()); |
| 35 } | 39 } |
| 36 | 40 |
| 37 void PinnedTabService::Observe(int type, | 41 void PinnedTabService::Observe(int type, |
| 38 const content::NotificationSource& source, | 42 const content::NotificationSource& source, |
| 39 const content::NotificationDetails& details) { | 43 const content::NotificationDetails& details) { |
| 40 if (got_exiting_) | 44 // Saving of tabs happens when saving is enabled, and when either the user |
| 41 return; | 45 // exits the application or closes the last browser window. |
| 42 | 46 // Saving is disabled when the user exits the application to prevent the |
| 47 // pin state of all the open browsers being overwritten by the state of the | |
| 48 // last browser window to close. | |
| 49 // Saving is re-enabled if a browser window is opened again, which may happen | |
| 50 // if the close is cancelled from an onbeforeunload event and the user | |
| 51 // continues their session, or if the process is kept alive by the background | |
|
benwells
2012/07/19 11:16:28
Is this logic too tricky? I'm not sure if it's the
sky
2012/07/19 16:31:27
Can you instead listen for NOTIFICATION_BROWSER_CL
benwells
2012/07/20 01:41:57
I could, I'm not sure if it's right or not. I've u
| |
| 52 // mode or packaged apps. | |
| 43 switch (type) { | 53 switch (type) { |
| 44 case chrome::NOTIFICATION_BROWSER_OPENED: { | 54 case chrome::NOTIFICATION_BROWSER_OPENED: { |
| 45 Browser* browser = content::Source<Browser>(source).ptr(); | 55 Browser* browser = content::Source<Browser>(source).ptr(); |
| 46 if (!has_normal_browser_ && browser->is_type_tabbed() && | 56 if (!has_normal_browser_ && browser->is_type_tabbed() && |
| 47 browser->profile() == profile_) { | 57 browser->profile() == profile_) { |
| 48 has_normal_browser_ = true; | 58 has_normal_browser_ = true; |
| 59 save_pinned_tabs_ = true; | |
| 49 } | 60 } |
| 50 break; | 61 break; |
| 51 } | 62 } |
| 52 | 63 |
| 53 case chrome::NOTIFICATION_BROWSER_CLOSING: { | 64 case chrome::NOTIFICATION_BROWSER_CLOSING: { |
| 54 Browser* browser = content::Source<Browser>(source).ptr(); | 65 Browser* browser = content::Source<Browser>(source).ptr(); |
| 55 if (has_normal_browser_ && browser->profile() == profile_) { | 66 if (has_normal_browser_ && save_pinned_tabs_ && |
| 56 if (*(content::Details<bool>(details)).ptr()) { | 67 browser->profile() == profile_) { |
| 57 GotExit(); | 68 if (IsLastNormalBrowser(browser)) { |
| 58 } else if (IsLastNormalBrowser(browser)) { | |
| 59 has_normal_browser_ = false; | 69 has_normal_browser_ = false; |
| 60 PinnedTabCodec::WritePinnedTabs(profile_); | 70 PinnedTabCodec::WritePinnedTabs(profile_); |
| 61 } | 71 } |
| 62 } | 72 } |
| 63 break; | 73 break; |
| 64 } | 74 } |
| 65 | 75 |
| 66 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { | 76 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
| 67 if (has_normal_browser_) | 77 if (has_normal_browser_ && save_pinned_tabs_) { |
| 68 GotExit(); | 78 PinnedTabCodec::WritePinnedTabs(profile_); |
| 79 save_pinned_tabs_ = false; | |
| 80 } | |
| 69 break; | 81 break; |
| 70 } | 82 } |
| 71 | 83 |
| 72 default: | 84 default: |
| 73 NOTREACHED(); | 85 NOTREACHED(); |
| 74 } | 86 } |
| 75 } | 87 } |
| 76 | |
| 77 void PinnedTabService::GotExit() { | |
| 78 DCHECK(!got_exiting_); | |
| 79 got_exiting_ = true; | |
| 80 PinnedTabCodec::WritePinnedTabs(profile_); | |
| 81 } | |
| OLD | NEW |