blob: 8acb90cfe1cbbc656ff07c4cd4ef21f3abd3f386 [file] [log] [blame]
// Copyright 2017 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/chromeos/note_taking_controller_client.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
namespace chromeos {
NoteTakingControllerClient::NoteTakingControllerClient(NoteTakingHelper* helper)
: helper_(helper) {
registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
}
NoteTakingControllerClient::~NoteTakingControllerClient() = default;
bool NoteTakingControllerClient::CanCreateNote() {
return profile_ && helper_->IsAppAvailable(profile_);
}
void NoteTakingControllerClient::CreateNote() {
helper_->LaunchAppForNewNote(profile_, base::FilePath());
}
void NoteTakingControllerClient::ActiveUserChanged(
const user_manager::User* active_user) {
SetProfile(ProfileHelper::Get()->GetProfileByUser(active_user));
}
void NoteTakingControllerClient::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_SESSION_STARTED:
// Update |profile_| when entering a session.
SetProfile(ProfileManager::GetActiveUserProfile());
// Add a session state observer to be able to monitor session changes.
if (!session_state_observer_.get()) {
session_state_observer_.reset(
new user_manager::ScopedUserSessionStateObserver(this));
}
break;
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
// Update |profile_| when exiting a session or shutting down.
Profile* profile = content::Source<Profile>(source).ptr();
if (profile_ == profile)
SetProfile(nullptr);
break;
}
}
}
void NoteTakingControllerClient::SetProfile(Profile* profile) {
profile_ = profile;
}
} // namespace chromeos