gcm_profile_service.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1// Copyright (c) 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/services/gcm/gcm_profile_service.h"
6
7#include "base/files/file_path.h"
8#include "base/logging.h"
9#include "base/prefs/pref_service.h"
10#include "base/values.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/signin/profile_identity_provider.h"
13#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14#include "chrome/browser/signin/signin_manager_factory.h"
15#include "chrome/common/chrome_constants.h"
16#include "chrome/common/chrome_version_info.h"
17#include "chrome/common/pref_names.h"
18#include "components/signin/core/browser/signin_manager.h"
19#include "components/user_prefs/pref_registry_syncable.h"
20#include "google_apis/gaia/identity_provider.h"
21#include "net/url_request/url_request_context_getter.h"
22
23#if !defined(OS_ANDROID)
24#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
25#endif
26
27namespace gcm {
28
29// static
30GCMProfileService::GCMEnabledState GCMProfileService::GetGCMEnabledState(
31    Profile* profile) {
32  const base::Value* gcm_enabled_value =
33      profile->GetPrefs()->GetUserPrefValue(prefs::kGCMChannelEnabled);
34  if (!gcm_enabled_value)
35    return ENABLED_FOR_APPS;
36
37  bool gcm_enabled = false;
38  if (!gcm_enabled_value->GetAsBoolean(&gcm_enabled))
39    return ENABLED_FOR_APPS;
40
41  return gcm_enabled ? ALWAYS_ENABLED : ALWAYS_DISABLED;
42}
43
44// static
45std::string GCMProfileService::GetGCMEnabledStateString(GCMEnabledState state) {
46  switch (state) {
47    case GCMProfileService::ALWAYS_ENABLED:
48      return "ALWAYS_ENABLED";
49    case GCMProfileService::ENABLED_FOR_APPS:
50      return "ENABLED_FOR_APPS";
51    case GCMProfileService::ALWAYS_DISABLED:
52      return "ALWAYS_DISABLED";
53    default:
54      NOTREACHED();
55      return std::string();
56  }
57}
58
59// static
60void GCMProfileService::RegisterProfilePrefs(
61    user_prefs::PrefRegistrySyncable* registry) {
62  // GCM support is only enabled by default for Canary/Dev/Custom builds.
63  chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
64  bool on_by_default = false;
65  if (channel == chrome::VersionInfo::CHANNEL_UNKNOWN ||
66      channel == chrome::VersionInfo::CHANNEL_CANARY ||
67      channel == chrome::VersionInfo::CHANNEL_DEV) {
68    on_by_default = true;
69  }
70  registry->RegisterBooleanPref(
71      prefs::kGCMChannelEnabled,
72      on_by_default,
73      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
74}
75
76GCMProfileService::GCMProfileService(Profile* profile)
77    : GCMService(scoped_ptr<IdentityProvider>(new ProfileIdentityProvider(
78          SigninManagerFactory::GetForProfile(profile),
79          ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
80#if defined(OS_ANDROID)
81          NULL))),
82#else
83          LoginUIServiceFactory::GetForProfile(profile)))),
84#endif
85      profile_(profile) {
86  DCHECK(!profile->IsOffTheRecord());
87}
88
89GCMProfileService::~GCMProfileService() {
90}
91
92void GCMProfileService::Shutdown() {
93  ShutdownService();
94}
95
96std::string GCMProfileService::SignedInUserName() const {
97  if (IsStarted())
98    return identity_provider_->GetActiveUsername();
99  return std::string();
100}
101
102bool GCMProfileService::ShouldStartAutomatically() const {
103  return GetGCMEnabledState(profile_) == ALWAYS_ENABLED;
104}
105
106base::FilePath GCMProfileService::GetStorePath() const {
107  return profile_->GetPath().Append(chrome::kGCMStoreDirname);
108}
109
110scoped_refptr<net::URLRequestContextGetter>
111GCMProfileService::GetURLRequestContextGetter() const {
112  return profile_->GetRequestContext();
113}
114
115}  // namespace gcm
116