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.
5 #include "chrome/browser/services/gcm/gcm_client_factory.h"
7 #include "base/files/file_path.h"
8 #include "base/lazy_instance.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "google_apis/gcm/gcm_client_impl.h"
17 static base::LazyInstance
<GCMClientImpl
>::Leaky g_gcm_client
=
18 LAZY_INSTANCE_INITIALIZER
;
19 static bool g_gcm_client_initialized
= false;
20 static GCMClientFactory::TestingFactoryFunction g_gcm_client_factory
= NULL
;
21 static GCMClient
* g_gcm_client_override
= NULL
;
27 GCMClient
* GCMClientFactory::GetClient() {
28 if (g_gcm_client_override
)
29 return g_gcm_client_override
;
30 if (g_gcm_client_factory
) {
31 g_gcm_client_override
= g_gcm_client_factory();
32 return g_gcm_client_override
;
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
37 GCMClientImpl
* client
= g_gcm_client
.Pointer();
38 if (!g_gcm_client_initialized
) {
39 // TODO(jianli): get gcm store path.
40 base::FilePath gcm_store_path
;
41 scoped_refptr
<base::SequencedWorkerPool
> worker_pool(
42 content::BrowserThread::GetBlockingPool());
43 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner(
44 worker_pool
->GetSequencedTaskRunnerWithShutdownBehavior(
45 worker_pool
->GetSequenceToken(),
46 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
47 client
->Initialize(gcm_store_path
, blocking_task_runner
);
48 g_gcm_client_initialized
= true;
54 void GCMClientFactory::SetTestingFactory(TestingFactoryFunction factory
) {
55 if (g_gcm_client_override
) {
56 delete g_gcm_client_override
;
57 g_gcm_client_override
= NULL
;
59 g_gcm_client_factory
= factory
;
62 GCMClientFactory::GCMClientFactory() {
65 GCMClientFactory::~GCMClientFactory() {