gcm_profile_service.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "chrome/browser/profiles/profile.h"
11#include "chrome/browser/services/gcm/gcm_utils.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/pref_names.h"
17#include "components/gcm_driver/gcm_client_factory.h"
18#include "components/gcm_driver/gcm_driver.h"
19#include "components/pref_registry/pref_registry_syncable.h"
20#include "components/signin/core/browser/signin_manager.h"
21#include "content/public/browser/browser_thread.h"
22#include "google_apis/gaia/identity_provider.h"
23#include "net/url_request/url_request_context_getter.h"
24
25#if !defined(OS_ANDROID)
26#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
27#endif
28
29namespace gcm {
30
31// static
32bool GCMProfileService::IsGCMEnabled(Profile* profile) {
33  return profile->GetPrefs()->GetBoolean(prefs::kGCMChannelEnabled);
34}
35
36// static
37void GCMProfileService::RegisterProfilePrefs(
38    user_prefs::PrefRegistrySyncable* registry) {
39  registry->RegisterBooleanPref(
40      prefs::kGCMChannelEnabled,
41      true,
42      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
43}
44
45GCMProfileService::GCMProfileService(
46    Profile* profile,
47    scoped_ptr<GCMClientFactory> gcm_client_factory)
48    : profile_(profile) {
49  DCHECK(!profile->IsOffTheRecord());
50
51  scoped_refptr<base::SequencedWorkerPool> worker_pool(
52      content::BrowserThread::GetBlockingPool());
53  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner(
54      worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
55          worker_pool->GetSequenceToken(),
56          base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
57
58#if defined(OS_ANDROID)
59  LoginUIService* login_ui_service = NULL;
60#else
61  LoginUIService* login_ui_service =
62      LoginUIServiceFactory::GetForProfile(profile_);
63#endif
64  driver_.reset(new GCMDriver(
65      gcm_client_factory.Pass(),
66      scoped_ptr<IdentityProvider>(new ProfileIdentityProvider(
67          SigninManagerFactory::GetForProfile(profile_),
68          ProfileOAuth2TokenServiceFactory::GetForProfile(profile_),
69          login_ui_service)),
70      GetChromeBuildInfo(),
71      profile_->GetPath().Append(chrome::kGCMStoreDirname),
72      profile_->GetRequestContext(),
73      content::BrowserThread::GetMessageLoopProxyForThread(
74          content::BrowserThread::UI),
75      content::BrowserThread::GetMessageLoopProxyForThread(
76          content::BrowserThread::IO),
77      blocking_task_runner));
78}
79
80GCMProfileService::GCMProfileService() : profile_(NULL) {
81}
82
83GCMProfileService::~GCMProfileService() {
84}
85
86void GCMProfileService::AddAppHandler(const std::string& app_id,
87                                      GCMAppHandler* handler) {
88  if (driver_)
89    driver_->AddAppHandler(app_id, handler);
90}
91
92void GCMProfileService::RemoveAppHandler(const std::string& app_id) {
93  if (driver_)
94    driver_->RemoveAppHandler(app_id);
95}
96
97void GCMProfileService::Register(const std::string& app_id,
98                                 const std::vector<std::string>& sender_ids,
99                                 const GCMDriver::RegisterCallback& callback) {
100  if (driver_)
101    driver_->Register(app_id, sender_ids, callback);
102}
103
104void GCMProfileService::Shutdown() {
105  if (driver_) {
106    driver_->Shutdown();
107    driver_.reset();
108  }
109}
110
111void GCMProfileService::SetDriverForTesting(GCMDriver* driver) {
112  driver_.reset(driver);
113}
114
115}  // namespace gcm
116