1// Copyright 2014 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 "base/logging.h"
6#include "components/signin/core/browser/test_signin_client.h"
7#include "components/signin/core/browser/webdata/token_service_table.h"
8#include "components/webdata/common/web_data_service_base.h"
9#include "components/webdata/common/web_database_service.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12#if defined(OS_IOS)
13#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
14#endif
15
16namespace {
17
18// Helper for testing.
19const int kInvalidProcessId = -1;
20}
21
22TestSigninClient::TestSigninClient()
23    : request_context_(new net::TestURLRequestContextGetter(
24          base::MessageLoopProxy::current())),
25      pref_service_(NULL) {
26  LoadDatabase();
27}
28
29TestSigninClient::TestSigninClient(PrefService* pref_service)
30    : pref_service_(pref_service) {}
31
32TestSigninClient::~TestSigninClient() {}
33
34PrefService* TestSigninClient::GetPrefs() {
35  return pref_service_;
36}
37
38scoped_refptr<TokenWebData> TestSigninClient::GetDatabase() {
39  return database_;
40}
41
42bool TestSigninClient::CanRevokeCredentials() { return true; }
43
44std::string TestSigninClient::GetSigninScopedDeviceId() {
45  return std::string();
46}
47
48void TestSigninClient::ClearSigninScopedDeviceId() {
49}
50
51net::URLRequestContextGetter* TestSigninClient::GetURLRequestContext() {
52  return request_context_.get();
53}
54
55void TestSigninClient::SetURLRequestContext(
56    net::URLRequestContextGetter* request_context) {
57  request_context_ = request_context;
58}
59
60std::string TestSigninClient::GetProductVersion() { return ""; }
61
62void TestSigninClient::LoadDatabase() {
63  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
64  base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB");
65  scoped_refptr<WebDatabaseService> web_database =
66      new WebDatabaseService(path,
67                             base::MessageLoopProxy::current(),
68                             base::MessageLoopProxy::current());
69  web_database->AddTable(scoped_ptr<WebDatabaseTable>(new TokenServiceTable()));
70  web_database->LoadDatabase();
71  database_ = new TokenWebData(web_database,
72                               base::MessageLoopProxy::current(),
73                               base::MessageLoopProxy::current(),
74                               WebDataServiceBase::ProfileErrorCallback());
75  database_->Init();
76}
77
78bool TestSigninClient::ShouldMergeSigninCredentialsIntoCookieJar() {
79  return true;
80}
81
82scoped_ptr<SigninClient::CookieChangedCallbackList::Subscription>
83TestSigninClient::AddCookieChangedCallback(
84    const SigninClient::CookieChangedCallback& callback) {
85  return cookie_callbacks_.Add(callback);
86}
87
88#if defined(OS_IOS)
89ios::ProfileOAuth2TokenServiceIOSProvider* TestSigninClient::GetIOSProvider() {
90  return GetIOSProviderAsFake();
91}
92
93ios::FakeProfileOAuth2TokenServiceIOSProvider*
94TestSigninClient::GetIOSProviderAsFake() {
95  if (!iosProvider_) {
96    iosProvider_.reset(new ios::FakeProfileOAuth2TokenServiceIOSProvider());
97  }
98  return iosProvider_.get();
99}
100#endif
101
102void TestSigninClient::SetSigninProcess(int process_id) {
103  if (process_id == signin_host_id_)
104    return;
105  DLOG_IF(WARNING, signin_host_id_ != kInvalidProcessId)
106      << "Replacing in-use signin process.";
107  signin_host_id_ = process_id;
108}
109
110void TestSigninClient::ClearSigninProcess() {
111  signin_host_id_ = kInvalidProcessId;
112}
113
114bool TestSigninClient::IsSigninProcess(int process_id) const {
115  return process_id == signin_host_id_;
116}
117
118bool TestSigninClient::HasSigninProcess() const {
119  return signin_host_id_ != kInvalidProcessId;
120}
121
122bool TestSigninClient::IsFirstRun() const {
123  return false;
124}
125
126base::Time TestSigninClient::GetInstallDate() {
127  return base::Time::Now();
128}
129