No longer register app window placement preference keys on
[chromium-blink-merge.git] / chrome / browser / ui / tabs / pinned_tab_service.cc
blob0762dcd9ff0379efe7e5b368d399f8cb2b5e161e
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/tabs/pinned_tab_service.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_iterator.h"
11 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
12 #include "content/public/browser/notification_service.h"
14 namespace {
16 // Returns true if |browser| is the only normal (tabbed) browser for |browser|'s
17 // profile (across all desktops).
18 bool IsOnlyNormalBrowser(Browser* browser) {
19 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
20 if (*it != browser && it->is_type_tabbed() &&
21 it->profile() == browser->profile()) {
22 return false;
25 return true;
28 } // namespace
30 PinnedTabService::PinnedTabService(Profile* profile)
31 : profile_(profile),
32 save_pinned_tabs_(true),
33 has_normal_browser_(false) {
34 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
35 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,
39 content::NotificationService::AllSources());
40 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED,
41 content::NotificationService::AllSources());
44 void PinnedTabService::Observe(int type,
45 const content::NotificationSource& source,
46 const content::NotificationDetails& details) {
47 // Saving of tabs happens when saving is enabled, and when either the user
48 // exits the application or closes the last browser window.
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
51 // last browser window to close.
52 // 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
54 // tab saving immediately, to prevent the following situation:
55 // * two windows are open, one with pinned tabs
56 // * user exits
57 // * pinned tabs are saved
58 // * window with pinned tabs is closed
59 // * other window blocks close with onbeforeunload
60 // * user saves work, etc. then closes the window
61 // * pinned tabs are saved, without the window with the pinned tabs,
62 // over-writing the correct state.
63 // Saving is re-enabled if a new tab or window is opened.
64 switch (type) {
65 case chrome::NOTIFICATION_BROWSER_OPENED: {
66 Browser* browser = content::Source<Browser>(source).ptr();
67 if (!has_normal_browser_ && browser->is_type_tabbed() &&
68 browser->profile() == profile_) {
69 has_normal_browser_ = true;
71 save_pinned_tabs_ = true;
72 break;
75 case chrome::NOTIFICATION_TAB_ADDED: {
76 save_pinned_tabs_ = true;
77 break;
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_);
89 break;
92 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: {
93 if (has_normal_browser_ && save_pinned_tabs_) {
94 PinnedTabCodec::WritePinnedTabs(profile_);
95 save_pinned_tabs_ = false;
97 break;
100 default:
101 NOTREACHED();