summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/gaia/token_service.cc
blob: 8420fb2c29fa1981ff8b3b67611f7ba85dbbaccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2010 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/net/gaia/token_service.h"

#include "base/string_util.h"
#include "chrome/common/net/gaia/gaia_authenticator2.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/notification_service.h"

void TokenService::Initialize(
    const char* const source,
    URLRequestContextGetter* getter,
    const GaiaAuthConsumer::ClientLoginResult& credentials) {

  credentials_ = credentials;
  source_ = std::string(source);
  sync_token_fetcher_.reset(new GaiaAuthenticator2(this, source_, getter));
  talk_token_fetcher_.reset(new GaiaAuthenticator2(this, source_, getter));
}

const bool TokenService::AreCredentialsValid() const {
  return !credentials_.lsid.empty() && !credentials_.sid.empty();
}

const bool TokenService::HasLsid() const {
  return !credentials_.lsid.empty();
}

const std::string& TokenService::GetLsid() const {
  return credentials_.lsid;
}

void TokenService::StartFetchingTokens() {
  DCHECK(AreCredentialsValid());
  sync_token_fetcher_->StartIssueAuthToken(credentials_.sid,
                                           credentials_.lsid,
                                           GaiaConstants::kSyncService);
  talk_token_fetcher_->StartIssueAuthToken(credentials_.sid,
                                           credentials_.lsid,
                                           GaiaConstants::kTalkService);
}

// Services dependent on a token will check if a token is available.
// If it isn't, they'll go to sleep until they get a token event.
const bool TokenService::HasTokenForService(const char* const service) const {
  return token_map_.count(service) > 0;
}

const std::string& TokenService::GetTokenForService(
    const char* const service) const {

  if (token_map_.count(service) > 0) {
    // map[key] is not const
    return (*token_map_.find(service)).second;
  }
  return EmptyString();
}

void TokenService::OnIssueAuthTokenSuccess(const std::string& service,
                                           const std::string& auth_token) {
  LOG(INFO) << "Got an authorization token for " << service;
  token_map_[service] = auth_token;
  TokenAvailableDetails details(service, auth_token);
  NotificationService::current()->Notify(
      NotificationType::TOKEN_AVAILABLE,
      Source<TokenService>(this),
      Details<const TokenAvailableDetails>(&details));
}
void TokenService::OnIssueAuthTokenFailure(const std::string& service,
                                           const GaiaAuthError& error) {
  LOG(WARNING) << "Auth token issuing failed for service:" << service;
  TokenRequestFailedDetails details(service, error);
  NotificationService::current()->Notify(
      NotificationType::TOKEN_REQUEST_FAILED,
      Source<TokenService>(this),
      Details<const TokenRequestFailedDetails>(&details));
}