Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "google_apis/gcm/engine/gservices_settings.h" | 5 #include "google_apis/gcm/engine/gservices_settings.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | |
| 9 | 10 |
| 10 namespace { | 11 namespace { |
| 11 // The expected time in seconds between periodic checkins. | 12 // The expected time in seconds between periodic checkins. |
| 12 const char kCheckinIntervalKey[] = "checkin_interval"; | 13 const char kCheckinIntervalKey[] = "checkin_interval"; |
| 13 // The override URL to the checkin server. | 14 // The override URL to the checkin server. |
| 14 const char kCheckinURLKey[] = "checkin_url"; | 15 const char kCheckinURLKey[] = "checkin_url"; |
| 15 // The MCS machine name to connect to. | 16 // The MCS machine name to connect to. |
| 16 const char kMCSHostnameKey[] = "gcm_hostname"; | 17 const char kMCSHostnameKey[] = "gcm_hostname"; |
| 17 // The MCS port to connect to. | 18 // The MCS port to connect to. |
| 18 const char kMCSSecurePortKey[] = "gcm_secure_port"; | 19 const char kMCSSecurePortKey[] = "gcm_secure_port"; |
| 19 // The URL to get MCS registration IDs. | 20 // The URL to get MCS registration IDs. |
| 20 const char kRegistrationURLKey[] = "gcm_registration_url"; | 21 const char kRegistrationURLKey[] = "gcm_registration_url"; |
| 21 | 22 |
| 22 const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60; // seconds = 2 days. | 23 const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60; // seconds = 2 days. |
| 23 const int64 kMinimumCheckinInterval = 12 * 60 * 60; // seconds = 12 hours. | 24 const int64 kMinimumCheckinInterval = 12 * 60 * 60; // seconds = 12 hours. |
| 24 const char kDefaultCheckinURL[] = "https://android.clients.google.com/checkin"; | 25 const char kDefaultCheckinURL[] = "https://android.clients.google.com/checkin"; |
| 25 const char kDefaultMCSHostname[] = "https://mtalk.google.com"; | 26 const char kDefaultMCSHostname[] = "mtalk.google.com"; |
| 26 const int kDefaultMCSSecurePort = 5228; | 27 const int kDefaultMCSMainSecurePort = 5228; |
| 28 const int kDefaultMCSFallbackSecurePort = 443; | |
| 27 const char kDefaultRegistrationURL[] = | 29 const char kDefaultRegistrationURL[] = |
| 28 "https://android.clients.google.com/c2dm/register3"; | 30 "https://android.clients.google.com/c2dm/register3"; |
| 31 const char kMCSEnpointTemplate[] = "https://%s:%d"; | |
| 32 | |
| 33 std::string MakeMCSEndpoint(const std::string& mcs_hostname, int port) { | |
| 34 return base::StringPrintf(kMCSEnpointTemplate, mcs_hostname.c_str(), port); | |
|
Nicolas Zea
2014/05/08 22:00:52
Should we be doing any kind of verification that t
fgorski
2014/05/09 17:54:41
Based on our conversation, we don't want to make a
| |
| 35 } | |
| 29 | 36 |
| 30 } // namespace | 37 } // namespace |
| 31 | 38 |
| 32 namespace gcm { | 39 namespace gcm { |
| 33 | 40 |
| 34 // static | 41 // static |
| 35 const base::TimeDelta GServicesSettings::MinimumCheckinInterval() { | 42 const base::TimeDelta GServicesSettings::MinimumCheckinInterval() { |
| 36 return base::TimeDelta::FromSeconds(kMinimumCheckinInterval); | 43 return base::TimeDelta::FromSeconds(kMinimumCheckinInterval); |
| 37 } | 44 } |
| 38 | 45 |
| 39 // static | 46 // static |
| 40 const GURL GServicesSettings::DefaultCheckinURL() { | 47 const GURL GServicesSettings::DefaultCheckinURL() { |
| 41 return GURL(kDefaultCheckinURL); | 48 return GURL(kDefaultCheckinURL); |
| 42 } | 49 } |
| 43 | 50 |
| 44 GServicesSettings::GServicesSettings() | 51 GServicesSettings::GServicesSettings() |
| 45 : checkin_interval_(base::TimeDelta::FromSeconds(kDefaultCheckinInterval)), | 52 : checkin_interval_(base::TimeDelta::FromSeconds(kDefaultCheckinInterval)), |
| 46 checkin_url_(kDefaultCheckinURL), | 53 checkin_url_(kDefaultCheckinURL), |
| 47 mcs_hostname_(kDefaultMCSHostname), | 54 mcs_main_endpoint_(MakeMCSEndpoint(kDefaultMCSHostname, |
| 48 mcs_secure_port_(kDefaultMCSSecurePort), | 55 kDefaultMCSMainSecurePort)), |
| 56 mcs_fallback_endpoint_(MakeMCSEndpoint(kDefaultMCSHostname, | |
| 57 kDefaultMCSFallbackSecurePort)), | |
| 49 registration_url_(kDefaultRegistrationURL), | 58 registration_url_(kDefaultRegistrationURL), |
| 50 weak_ptr_factory_(this) { | 59 weak_ptr_factory_(this) { |
| 51 } | 60 } |
| 52 | 61 |
| 53 GServicesSettings::~GServicesSettings() {} | 62 GServicesSettings::~GServicesSettings() {} |
| 54 | 63 |
| 55 bool GServicesSettings::UpdateFromCheckinResponse( | 64 bool GServicesSettings::UpdateFromCheckinResponse( |
| 56 const checkin_proto::AndroidCheckinResponse& checkin_response) { | 65 const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| 57 if (!checkin_response.has_digest() || | 66 if (!checkin_response.has_digest() || |
| 58 checkin_response.digest() == digest_) { | 67 checkin_response.digest() == digest_) { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 81 const GCMStore::LoadResult& load_result) { | 90 const GCMStore::LoadResult& load_result) { |
| 82 if (UpdateSettings(load_result.gservices_settings)) | 91 if (UpdateSettings(load_result.gservices_settings)) |
| 83 digest_ = load_result.gservices_digest; | 92 digest_ = load_result.gservices_digest; |
| 84 } | 93 } |
| 85 | 94 |
| 86 std::map<std::string, std::string> GServicesSettings::GetSettingsMap() const { | 95 std::map<std::string, std::string> GServicesSettings::GetSettingsMap() const { |
| 87 std::map<std::string, std::string> settings; | 96 std::map<std::string, std::string> settings; |
| 88 settings[kCheckinIntervalKey] = | 97 settings[kCheckinIntervalKey] = |
| 89 base::Int64ToString(checkin_interval_.InSeconds()); | 98 base::Int64ToString(checkin_interval_.InSeconds()); |
| 90 settings[kCheckinURLKey] = checkin_url_.spec(); | 99 settings[kCheckinURLKey] = checkin_url_.spec(); |
| 91 settings[kMCSHostnameKey] = mcs_hostname_; | 100 settings[kMCSHostnameKey] = mcs_main_endpoint_.host(); |
| 92 settings[kMCSSecurePortKey] = base::IntToString(mcs_secure_port_); | 101 settings[kMCSSecurePortKey] = mcs_main_endpoint_.port(); |
| 93 settings[kRegistrationURLKey] = registration_url_.spec(); | 102 settings[kRegistrationURLKey] = registration_url_.spec(); |
| 94 return settings; | 103 return settings; |
| 95 } | 104 } |
| 96 | 105 |
| 97 bool GServicesSettings::UpdateSettings( | 106 bool GServicesSettings::UpdateSettings( |
| 98 const std::map<std::string, std::string>& settings) { | 107 const std::map<std::string, std::string>& settings) { |
| 99 int64 new_checkin_interval = kMinimumCheckinInterval; | 108 int64 new_checkin_interval = kMinimumCheckinInterval; |
| 100 std::map<std::string, std::string>::const_iterator iter = | 109 std::map<std::string, std::string>::const_iterator iter = |
| 101 settings.find(kCheckinIntervalKey); | 110 settings.find(kCheckinIntervalKey); |
| 102 if (iter == settings.end()) { | 111 if (iter == settings.end()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 } | 146 } |
| 138 if (!base::StringToInt(iter->second, &new_mcs_secure_port)) { | 147 if (!base::StringToInt(iter->second, &new_mcs_secure_port)) { |
| 139 LOG(ERROR) << "Failed to parse MCS secure port: " << iter->second; | 148 LOG(ERROR) << "Failed to parse MCS secure port: " << iter->second; |
| 140 return false; | 149 return false; |
| 141 } | 150 } |
| 142 if (new_mcs_secure_port < 0 || 65535 < new_mcs_secure_port) { | 151 if (new_mcs_secure_port < 0 || 65535 < new_mcs_secure_port) { |
| 143 LOG(ERROR) << "Incorrect port value: " << new_mcs_secure_port; | 152 LOG(ERROR) << "Incorrect port value: " << new_mcs_secure_port; |
| 144 return false; | 153 return false; |
| 145 } | 154 } |
| 146 | 155 |
| 156 GURL new_mcs_main_endpoint = | |
| 157 GURL(MakeMCSEndpoint(new_mcs_hostname, new_mcs_secure_port)); | |
| 158 GURL new_mcs_fallback_endpoint = | |
| 159 GURL(MakeMCSEndpoint(new_mcs_hostname, kDefaultMCSFallbackSecurePort)); | |
| 160 if (!new_mcs_main_endpoint.is_valid() || | |
| 161 !new_mcs_fallback_endpoint.is_valid()) | |
| 162 return false; | |
| 163 | |
| 147 GURL new_checkin_url; | 164 GURL new_checkin_url; |
| 148 iter = settings.find(kCheckinURLKey); | 165 iter = settings.find(kCheckinURLKey); |
| 149 if (iter == settings.end()) { | 166 if (iter == settings.end()) { |
| 150 LOG(ERROR) << "Setting not found: " << kCheckinURLKey; | 167 LOG(ERROR) << "Setting not found: " << kCheckinURLKey; |
| 151 return false; | 168 return false; |
| 152 } | 169 } |
| 153 new_checkin_url = GURL(iter->second); | 170 new_checkin_url = GURL(iter->second); |
| 154 if (!new_checkin_url.is_valid()) { | 171 if (!new_checkin_url.is_valid()) { |
| 155 LOG(ERROR) << "Invalid checkin URL provided: " | 172 LOG(ERROR) << "Invalid checkin URL provided: " |
| 156 << new_checkin_url.possibly_invalid_spec(); | 173 << new_checkin_url.possibly_invalid_spec(); |
| 157 return false; | 174 return false; |
| 158 } | 175 } |
| 159 | 176 |
| 160 GURL new_registration_url; | 177 GURL new_registration_url; |
| 161 iter = settings.find(kRegistrationURLKey); | 178 iter = settings.find(kRegistrationURLKey); |
| 162 if (iter == settings.end()) { | 179 if (iter == settings.end()) { |
| 163 LOG(ERROR) << "Setting not found: " << kRegistrationURLKey; | 180 LOG(ERROR) << "Setting not found: " << kRegistrationURLKey; |
| 164 return false; | 181 return false; |
| 165 } | 182 } |
| 166 new_registration_url = GURL(iter->second); | 183 new_registration_url = GURL(iter->second); |
| 167 if (!new_registration_url.is_valid()) { | 184 if (!new_registration_url.is_valid()) { |
| 168 LOG(ERROR) << "Invalid registration URL provided: " | 185 LOG(ERROR) << "Invalid registration URL provided: " |
| 169 << new_registration_url.possibly_invalid_spec(); | 186 << new_registration_url.possibly_invalid_spec(); |
| 170 return false; | 187 return false; |
| 171 } | 188 } |
| 172 | 189 |
| 173 // We only update the settings once all of them are correct. | 190 // We only update the settings once all of them are correct. |
| 174 checkin_interval_ = base::TimeDelta::FromSeconds(new_checkin_interval); | 191 checkin_interval_ = base::TimeDelta::FromSeconds(new_checkin_interval); |
| 175 mcs_hostname_ = new_mcs_hostname; | 192 mcs_main_endpoint_ = new_mcs_main_endpoint; |
| 176 mcs_secure_port_ = new_mcs_secure_port; | 193 mcs_fallback_endpoint_ = new_mcs_fallback_endpoint; |
| 177 checkin_url_ = new_checkin_url; | 194 checkin_url_ = new_checkin_url; |
| 178 registration_url_ = new_registration_url; | 195 registration_url_ = new_registration_url; |
| 179 return true; | 196 return true; |
| 180 } | 197 } |
| 181 | 198 |
| 182 } // namespace gcm | 199 } // namespace gcm |
| OLD | NEW |