1 // Copyright 2017 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/chromeos/note_taking_controller_client.h"
6 
7 #include "chrome/browser/chromeos/profiles/profile_helper.h"
8 
9 namespace chromeos {
10 
NoteTakingControllerClient(NoteTakingHelper * helper)11 NoteTakingControllerClient::NoteTakingControllerClient(NoteTakingHelper* helper)
12     : helper_(helper) {
13   user_manager::UserManager::Get()->AddSessionStateObserver(this);
14 }
15 
~NoteTakingControllerClient()16 NoteTakingControllerClient::~NoteTakingControllerClient() {
17   user_manager::UserManager::Get()->RemoveSessionStateObserver(this);
18 }
19 
CanCreateNote()20 bool NoteTakingControllerClient::CanCreateNote() {
21   return profile_ && helper_->IsAppAvailable(profile_);
22 }
23 
CreateNote()24 void NoteTakingControllerClient::CreateNote() {
25   helper_->LaunchAppForNewNote(profile_, base::FilePath());
26 }
27 
ActiveUserChanged(user_manager::User * active_user)28 void NoteTakingControllerClient::ActiveUserChanged(
29     user_manager::User* active_user) {
30   if (!active_user)
31     return;
32 
33   active_user->AddProfileCreatedObserver(
34       base::BindOnce(&NoteTakingControllerClient::SetProfileByUser,
35                      weak_ptr_factory_.GetWeakPtr(), active_user));
36 }
37 
OnProfileWillBeDestroyed(Profile * profile)38 void NoteTakingControllerClient::OnProfileWillBeDestroyed(Profile* profile) {
39   // Update |profile_| when exiting a session or shutting down.
40   DCHECK_EQ(profile_, profile);
41   profile_observer_.Remove(profile_);
42   profile_ = nullptr;
43 }
44 
SetProfileByUser(const user_manager::User * user)45 void NoteTakingControllerClient::SetProfileByUser(
46     const user_manager::User* user) {
47   profile_ = ProfileHelper::Get()->GetProfileByUser(user);
48   profile_observer_.RemoveAll();
49   profile_observer_.Add(profile_);
50 }
51 
52 }  // namespace chromeos
53