- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / fake_profile_oauth2_token_service.cc
1 // Copyright 2013 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/signin/fake_profile_oauth2_token_service.h"
6
7 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
8 }
9
10 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
11 }
12
13 // static
14 BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
15     content::BrowserContext* profile) {
16   FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
17   service->Initialize(reinterpret_cast<Profile*>(profile));
18   return service;
19 }
20
21 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() {
22 }
23
24 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
25 }
26
27 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
28     const std::string& account_id) {
29   return !GetRefreshToken(account_id).empty();
30 }
31
32 void FakeProfileOAuth2TokenService::IssueRefreshToken(
33     const std::string& token) {
34   IssueRefreshTokenForUser("account_id", token);
35 }
36
37 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
38     const std::string& account_id,
39     const std::string& token) {
40   refresh_token_ = token;
41   if (refresh_token_.empty())
42     FireRefreshTokenRevoked(account_id);
43   else
44     FireRefreshTokenAvailable(account_id);
45   // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
46 }
47
48 void FakeProfileOAuth2TokenService::IssueTokenForScope(
49     const ScopeSet& scope,
50     const std::string& access_token,
51     const base::Time& expiration) {
52   CompleteRequests(false,
53                    scope,
54                    GoogleServiceAuthError::AuthErrorNone(),
55                    access_token,
56                    expiration);
57 }
58
59 void FakeProfileOAuth2TokenService::IssueErrorForScope(
60     const ScopeSet& scope,
61     const GoogleServiceAuthError& error) {
62   CompleteRequests(false, scope, error, std::string(), base::Time());
63 }
64
65 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
66     const GoogleServiceAuthError& error) {
67   CompleteRequests(true, ScopeSet(), error, std::string(), base::Time());
68 }
69
70 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
71     const std::string& access_token,
72     const base::Time& expiration) {
73   CompleteRequests(true,
74                    ScopeSet(),
75                    GoogleServiceAuthError::AuthErrorNone(),
76                    access_token,
77                    expiration);
78 }
79
80 void FakeProfileOAuth2TokenService::CompleteRequests(
81     bool all_scopes,
82     const ScopeSet& scope,
83     const GoogleServiceAuthError& error,
84     const std::string& access_token,
85     const base::Time& expiration) {
86   std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
87       GetPendingRequests();
88
89   // Walk the requests and notify the callbacks.
90   for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
91        it != pending_requests_.end(); ++it) {
92     if (it->request && (all_scopes || it->scopes == scope))
93       it->request->InformConsumer(error, access_token, expiration);
94   }
95 }
96
97 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
98     const std::string& account_id) {
99   return refresh_token_;
100 }
101
102 net::URLRequestContextGetter*
103 FakeProfileOAuth2TokenService::GetRequestContext() {
104   return NULL;
105 }
106
107 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
108 FakeProfileOAuth2TokenService::GetPendingRequests() {
109   std::vector<PendingRequest> valid_requests;
110   for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
111        it != pending_requests_.end(); ++it) {
112     if (it->request)
113       valid_requests.push_back(*it);
114   }
115   return valid_requests;
116 }
117
118 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
119     RequestImpl* request,
120     const std::string& account_id,
121     net::URLRequestContextGetter* getter,
122     const std::string& client_id,
123     const std::string& client_secret,
124     const ScopeSet& scopes) {
125   PendingRequest pending_request;
126   pending_request.account_id = account_id;
127   pending_request.client_id = client_id;
128   pending_request.client_secret = client_secret;
129   pending_request.scopes = scopes;
130   pending_request.request = request->AsWeakPtr();
131   pending_requests_.push_back(pending_request);
132 }