15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Use of this source code is governed by a BSD-style license that can be
35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// found in the LICENSE file.
45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
55c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/services/gcm/fake_signin_manager.h"
65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
75c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/observer_list.h"
85c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/prefs/pref_service.h"
95c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/profiles/profile.h"
105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/signin/chrome_signin_client_factory.h"
115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "components/keyed_service/core/keyed_service.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "components/signin/core/common/signin_pref_names.h"
135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/public/browser/browser_context.h"
145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if !defined(OS_CHROMEOS)
165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif
185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace gcm {
205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuFakeSigninManager::FakeSigninManager(Profile* profile)
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if defined(OS_CHROMEOS)
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    : SigninManagerBase(
245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        ChromeSigninClientFactory::GetInstance()->GetForProfile(profile)),
255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#else
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    : SigninManager(
275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        ChromeSigninClientFactory::GetInstance()->GetForProfile(profile),
285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        ProfileOAuth2TokenServiceFactory::GetForProfile(profile)),
295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif
305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      profile_(profile) {
315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  Initialize(NULL);
325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuFakeSigninManager::~FakeSigninManager() {
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid FakeSigninManager::SignIn(const std::string& username) {
385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SetAuthenticatedUsername(username);
395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  FOR_EACH_OBSERVER(Observer,
405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                    observer_list_,
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    GoogleSigninSucceeded(username, username, std::string()));
425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid FakeSigninManager::SignOut(
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    signin_metrics::ProfileSignout signout_source_metric) {
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const std::string account_id = GetAuthenticatedAccountId();
475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  const std::string username = GetAuthenticatedUsername();
485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  clear_authenticated_username();
495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  FOR_EACH_OBSERVER(Observer,
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    observer_list_,
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    GoogleSignedOut(account_id, username));
535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// static
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuKeyedService* FakeSigninManager::Build(content::BrowserContext* context) {
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  return new FakeSigninManager(Profile::FromBrowserContext(context));
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}  // namespace gcm
61