Chromium Code Reviews| Index: components/gcm_driver/gcm_desktop_utils.cc |
| diff --git a/components/gcm_driver/gcm_desktop_utils.cc b/components/gcm_driver/gcm_desktop_utils.cc |
| index 332ad32a6676ca9b0ff5133821723d25b65167f3..4bf14ac3080ef606d05cf71ac3d55310ad15165b 100644 |
| --- a/components/gcm_driver/gcm_desktop_utils.cc |
| +++ b/components/gcm_driver/gcm_desktop_utils.cc |
| @@ -8,12 +8,19 @@ |
| #include "base/command_line.h" |
| #include "base/sequenced_task_runner.h" |
| +#include "base/strings/string_piece.h" |
| +#include "base/strings/string_util.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| #include "build/build_config.h" |
| +#include "components/gcm_driver/gcm_client.h" |
| #include "components/gcm_driver/gcm_client_factory.h" |
| #include "components/gcm_driver/gcm_driver.h" |
| #include "components/gcm_driver/gcm_driver_desktop.h" |
| +#include "components/gcm_driver/pref_names.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| +#include "components/prefs/pref_service.h" |
| #include "components/sync_driver/sync_util.h" |
| +#include "components/version_info/version_info.h" |
| #include "url/gurl.h" |
| namespace gcm { |
| @@ -63,11 +70,49 @@ std::string GetVersion() { |
| return version_info::GetVersionNumber(); |
| } |
| -GCMClient::ChromeBuildInfo GetChromeBuildInfo(version_info::Channel channel) { |
| +std::string ToLowerAlphaNum(base::StringPiece in) { |
| + std::string out; |
| + out.reserve(in.size()); |
| + for (size_t i = 0; i < in.size(); i++) { |
| + if (base::IsAsciiAlpha(in[i]) || base::IsAsciiDigit(in[i])) |
| + out.push_back(base::ToLowerASCII(in[i])); |
| + } |
| + return out; |
| +} |
| + |
| +// Returns a string like "com.chrome.stable.macosx" that should be used as the |
| +// GCM category when an app_id is sent as a subtype instead of as a category. |
| +// This is generated once, then remains fixed forever (even if e.g. the product |
| +// name or channel change), since it must match existing Instance ID tokens. |
| +std::string CategoryForSubtypes(PrefService* prefs, |
| + const std::string& product_shortname, |
| + version_info::Channel channel_info) { |
| + std::string category_for_subtypes = |
| + prefs->GetString(prefs::kCategoryForSubtypes); |
| + if (!category_for_subtypes.empty()) |
| + return category_for_subtypes; |
| + |
| + std::string product = ToLowerAlphaNum(product_shortname); |
| + std::string ns = product == "chromium" ? "org" : "com"; |
|
Peter Beverloo
2016/07/22 12:17:03
This really feels like the wrong layer to make thi
johnme
2016/07/26 17:11:55
Done.
|
| + std::string channel = |
| + ToLowerAlphaNum(version_info::GetChannelString(channel_info)); |
| + std::string platform = ToLowerAlphaNum(version_info::GetOSType()); |
| + category_for_subtypes = ns + '.' + product + '.' + channel + '.' + platform; |
| + |
| + prefs->SetString(prefs::kCategoryForSubtypes, category_for_subtypes); |
| + return category_for_subtypes; |
| +} |
| + |
| +GCMClient::ChromeBuildInfo GetChromeBuildInfo( |
| + PrefService* prefs, |
| + const std::string& product_shortname, |
| + version_info::Channel channel) { |
| GCMClient::ChromeBuildInfo chrome_build_info; |
| chrome_build_info.platform = GetPlatform(); |
| chrome_build_info.channel = GetChannel(channel); |
| chrome_build_info.version = GetVersion(); |
| + chrome_build_info.category_for_subtypes = |
| + CategoryForSubtypes(prefs, product_shortname, channel); |
| return chrome_build_info; |
| } |
| @@ -86,17 +131,24 @@ std::string GetUserAgent(version_info::Channel channel) { |
| } // namespace |
| +void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| + registry->RegisterStringPref(prefs::kCategoryForSubtypes, |
| + std::string() /* default_value */); |
| +} |
| + |
| std::unique_ptr<GCMDriver> CreateGCMDriverDesktop( |
| std::unique_ptr<GCMClientFactory> gcm_client_factory, |
| PrefService* prefs, |
| const base::FilePath& store_path, |
| const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| + const std::string& product_shortname, |
| version_info::Channel channel, |
| const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, |
| const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) { |
| return std::unique_ptr<GCMDriver>(new GCMDriverDesktop( |
| - std::move(gcm_client_factory), GetChromeBuildInfo(channel), |
| + std::move(gcm_client_factory), |
| + GetChromeBuildInfo(prefs, product_shortname, channel), |
| GetChannelStatusRequestUrl(channel), GetUserAgent(channel), prefs, |
| store_path, request_context, ui_task_runner, io_task_runner, |
| blocking_task_runner)); |