This source file includes following definitions.
- IsOnlyNormalBrowser
- has_normal_browser_
- Observe
#include "chrome/browser/ui/tabs/pinned_tab_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/tabs/pinned_tab_codec.h"
#include "content/public/browser/notification_service.h"
namespace {
bool IsOnlyNormalBrowser(Browser* browser) {
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
if (*it != browser && it->is_type_tabbed() &&
it->profile() == browser->profile()) {
return false;
}
}
return true;
}
}
PinnedTabService::PinnedTabService(Profile* profile)
: profile_(profile),
save_pinned_tabs_(true),
has_normal_browser_(false) {
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED,
content::NotificationService::AllSources());
}
void PinnedTabService::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_BROWSER_OPENED: {
Browser* browser = content::Source<Browser>(source).ptr();
if (!has_normal_browser_ && browser->is_type_tabbed() &&
browser->profile() == profile_) {
has_normal_browser_ = true;
}
save_pinned_tabs_ = true;
break;
}
case chrome::NOTIFICATION_TAB_ADDED: {
save_pinned_tabs_ = true;
break;
}
case chrome::NOTIFICATION_BROWSER_CLOSING: {
Browser* browser = content::Source<Browser>(source).ptr();
if (has_normal_browser_ && save_pinned_tabs_ &&
browser->profile() == profile_) {
if (IsOnlyNormalBrowser(browser)) {
has_normal_browser_ = false;
PinnedTabCodec::WritePinnedTabs(profile_);
}
}
break;
}
case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: {
if (has_normal_browser_ && save_pinned_tabs_) {
PinnedTabCodec::WritePinnedTabs(profile_);
save_pinned_tabs_ = false;
}
break;
}
default:
NOTREACHED();
}
}