• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #include "chrome/browser/tabs/pinned_tab_service.h"
6 
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/tabs/pinned_tab_codec.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_list.h"
11 #include "content/common/notification_service.h"
12 #include "content/common/notification_type.h"
13 
IsLastNormalBrowser(Browser * browser)14 static bool IsLastNormalBrowser(Browser* browser) {
15   for (BrowserList::const_iterator i = BrowserList::begin();
16        i != BrowserList::end(); ++i) {
17     if (*i != browser && (*i)->type() == Browser::TYPE_NORMAL &&
18         (*i)->profile() == browser->profile()) {
19       return false;
20     }
21   }
22   return true;
23 }
24 
PinnedTabService(Profile * profile)25 PinnedTabService::PinnedTabService(Profile* profile)
26     : profile_(profile),
27       got_exiting_(false),
28       has_normal_browser_(false) {
29   registrar_.Add(this, NotificationType::BROWSER_OPENED,
30                  NotificationService::AllSources());
31   registrar_.Add(this, NotificationType::BROWSER_CLOSING,
32                  NotificationService::AllSources());
33   registrar_.Add(this, NotificationType::APP_EXITING,
34                  NotificationService::AllSources());
35 }
36 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)37 void PinnedTabService::Observe(NotificationType type,
38                                const NotificationSource& source,
39                                const NotificationDetails& details) {
40   if (got_exiting_)
41     return;
42 
43   switch (type.value) {
44     case NotificationType::BROWSER_OPENED: {
45       Browser* browser = Source<Browser>(source).ptr();
46       if (!has_normal_browser_ && browser->type() == Browser::TYPE_NORMAL &&
47           browser->profile() == profile_) {
48         has_normal_browser_ = true;
49       }
50       break;
51     }
52 
53     case NotificationType::BROWSER_CLOSING: {
54       Browser* browser = Source<Browser>(source).ptr();
55       if (has_normal_browser_ && browser->profile() == profile_) {
56         if (*(Details<bool>(details)).ptr()) {
57           GotExit();
58         } else if (IsLastNormalBrowser(browser)) {
59           has_normal_browser_ = false;
60           PinnedTabCodec::WritePinnedTabs(profile_);
61         }
62       }
63       break;
64     }
65 
66     case NotificationType::APP_EXITING: {
67       if (has_normal_browser_)
68         GotExit();
69       break;
70     }
71 
72     default:
73       NOTREACHED();
74   }
75 }
76 
GotExit()77 void PinnedTabService::GotExit() {
78   DCHECK(!got_exiting_);
79   got_exiting_ = true;
80   PinnedTabCodec::WritePinnedTabs(profile_);
81 }
82