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. 4 5 #include "chrome/browser/services/gcm/gcm_desktop_utils.h" 6 7 #include "base/command_line.h" 8 #include "base/logging.h" 9 #include "base/sequenced_task_runner.h" 10 #include "base/threading/sequenced_worker_pool.h" 11 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h" 12 #include "chrome/browser/sync/profile_sync_service.h" 13 #include "chrome/common/chrome_version_info.h" 14 #include "components/gcm_driver/gcm_client.h" 15 #include "components/gcm_driver/gcm_client_factory.h" 16 #include "components/gcm_driver/gcm_driver.h" 17 #include "components/gcm_driver/gcm_driver_desktop.h" 18 #include "content/public/browser/browser_thread.h" 19 20 namespace gcm { 21 22 namespace { 23 24 const char kChannelStatusRelativePath[] = "/experimentstatus"; 25 26 GCMClient::ChromePlatform GetPlatform() { 27 #if defined(OS_WIN) 28 return GCMClient::PLATFORM_WIN; 29 #elif defined(OS_MACOSX) 30 return GCMClient::PLATFORM_MAC; 31 #elif defined(OS_IOS) 32 return GCMClient::PLATFORM_IOS; 33 #elif defined(OS_ANDROID) 34 return GCMClient::PLATFORM_ANDROID; 35 #elif defined(OS_CHROMEOS) 36 return GCMClient::PLATFORM_CROS; 37 #elif defined(OS_LINUX) 38 return GCMClient::PLATFORM_LINUX; 39 #else 40 // For all other platforms, return as LINUX. 41 return GCMClient::PLATFORM_LINUX; 42 #endif 43 } 44 45 GCMClient::ChromeChannel GetChannel() { 46 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); 47 switch (channel) { 48 case chrome::VersionInfo::CHANNEL_UNKNOWN: 49 return GCMClient::CHANNEL_UNKNOWN; 50 case chrome::VersionInfo::CHANNEL_CANARY: 51 return GCMClient::CHANNEL_CANARY; 52 case chrome::VersionInfo::CHANNEL_DEV: 53 return GCMClient::CHANNEL_DEV; 54 case chrome::VersionInfo::CHANNEL_BETA: 55 return GCMClient::CHANNEL_BETA; 56 case chrome::VersionInfo::CHANNEL_STABLE: 57 return GCMClient::CHANNEL_STABLE; 58 default: 59 NOTREACHED(); 60 return GCMClient::CHANNEL_UNKNOWN; 61 } 62 } 63 64 std::string GetVersion() { 65 chrome::VersionInfo version_info; 66 return version_info.Version(); 67 } 68 69 GCMClient::ChromeBuildInfo GetChromeBuildInfo() { 70 GCMClient::ChromeBuildInfo chrome_build_info; 71 chrome_build_info.platform = GetPlatform(); 72 chrome_build_info.channel = GetChannel(); 73 chrome_build_info.version = GetVersion(); 74 return chrome_build_info; 75 } 76 77 std::string GetChannelStatusRequestUrl() { 78 GURL sync_url( 79 ProfileSyncService::GetSyncServiceURL(*CommandLine::ForCurrentProcess())); 80 return sync_url.spec() + kChannelStatusRelativePath; 81 } 82 83 std::string GetUserAgent() { 84 chrome::VersionInfo version_info; 85 return browser_sync::LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi( 86 version_info); 87 } 88 89 } // namespace 90 91 scoped_ptr<GCMDriver> CreateGCMDriverDesktop( 92 scoped_ptr<GCMClientFactory> gcm_client_factory, 93 PrefService* prefs, 94 const base::FilePath& store_path, 95 const scoped_refptr<net::URLRequestContextGetter>& request_context) { 96 scoped_refptr<base::SequencedWorkerPool> worker_pool( 97 content::BrowserThread::GetBlockingPool()); 98 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( 99 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( 100 worker_pool->GetSequenceToken(), 101 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); 102 return scoped_ptr<GCMDriver>( 103 new GCMDriverDesktop(gcm_client_factory.Pass(), 104 GetChromeBuildInfo(), 105 GetChannelStatusRequestUrl(), 106 GetUserAgent(), 107 prefs, 108 store_path, 109 request_context, 110 content::BrowserThread::GetMessageLoopProxyForThread( 111 content::BrowserThread::UI), 112 content::BrowserThread::GetMessageLoopProxyForThread( 113 content::BrowserThread::IO), 114 blocking_task_runner)); 115 } 116 117 } // namespace gcm 118