summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tabs/pinned_tab_service.cc
blob: 063399872157d4d158ca662cd8d1ea0f7ab66541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tabs/pinned_tab_service.h"

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tabs/pinned_tab_codec.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "content/common/notification_service.h"
#include "content/common/notification_type.h"

static bool IsLastNormalBrowser(Browser* browser) {
  for (BrowserList::const_iterator i = BrowserList::begin();
       i != BrowserList::end(); ++i) {
    if (*i != browser && (*i)->type() == Browser::TYPE_NORMAL &&
        (*i)->profile() == browser->profile()) {
      return false;
    }
  }
  return true;
}

PinnedTabService::PinnedTabService(Profile* profile)
    : profile_(profile),
      got_exiting_(false),
      has_normal_browser_(false) {
  registrar_.Add(this, NotificationType::BROWSER_OPENED,
                 NotificationService::AllSources());
  registrar_.Add(this, NotificationType::BROWSER_CLOSING,
                 NotificationService::AllSources());
  registrar_.Add(this, NotificationType::APP_EXITING,
                 NotificationService::AllSources());
}

void PinnedTabService::Observe(NotificationType type,
                               const NotificationSource& source,
                               const NotificationDetails& details) {
  if (got_exiting_)
    return;

  switch (type.value) {
    case NotificationType::BROWSER_OPENED: {
      Browser* browser = Source<Browser>(source).ptr();
      if (!has_normal_browser_ && browser->type() == Browser::TYPE_NORMAL &&
          browser->profile() == profile_) {
        has_normal_browser_ = true;
      }
      break;
    }

    case NotificationType::BROWSER_CLOSING: {
      Browser* browser = Source<Browser>(source).ptr();
      if (has_normal_browser_ && browser->profile() == profile_) {
        if (*(Details<bool>(details)).ptr()) {
          GotExit();
        } else if (IsLastNormalBrowser(browser)) {
          has_normal_browser_ = false;
          PinnedTabCodec::WritePinnedTabs(profile_);
        }
      }
      break;
    }

    case NotificationType::APP_EXITING: {
      if (has_normal_browser_)
        GotExit();
      break;
    }

    default:
      NOTREACHED();
  }
}

void PinnedTabService::GotExit() {
  DCHECK(!got_exiting_);
  got_exiting_ = true;
  PinnedTabCodec::WritePinnedTabs(profile_);
}