| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/gcm/gcm_api.h" | 5 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/sha1.h" | |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 18 #include "chrome/common/extensions/api/gcm.h" | 17 #include "chrome/common/extensions/api/gcm.h" |
| 19 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
| 20 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
| 21 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 const size_t kMaximumMessageSize = 4096; // in bytes. | 24 const size_t kMaximumMessageSize = 4096; // in bytes. |
| 26 const char kCollapseKey[] = "collapse_key"; | 25 const char kCollapseKey[] = "collapse_key"; |
| 27 const char kGoogDotRestrictedPrefix[] = "goog."; | 26 const char kGoogDotRestrictedPrefix[] = "goog."; |
| 28 const char kGoogleRestrictedPrefix[] = "google"; | 27 const char kGoogleRestrictedPrefix[] = "google"; |
| 29 | 28 |
| 30 // Error messages. | 29 // Error messages. |
| 31 const char kInvalidParameter[] = | 30 const char kInvalidParameter[] = |
| 32 "Function was called with invalid parameters."; | 31 "Function was called with invalid parameters."; |
| 33 const char kNotSignedIn[] = "Profile was not signed in."; | 32 const char kNotSignedIn[] = "Profile was not signed in."; |
| 34 const char kCertificateMissing[] = "Manifest key was missing."; | |
| 35 const char kAsyncOperationPending[] = | 33 const char kAsyncOperationPending[] = |
| 36 "Asynchronous operation is pending."; | 34 "Asynchronous operation is pending."; |
| 37 const char kNetworkError[] = "Network error occurred."; | 35 const char kNetworkError[] = "Network error occurred."; |
| 38 const char kServerError[] = "Server error occurred."; | 36 const char kServerError[] = "Server error occurred."; |
| 39 const char kTtlExceeded[] = "Time-to-live exceeded."; | 37 const char kTtlExceeded[] = "Time-to-live exceeded."; |
| 40 const char kUnknownError[] = "Unknown error occurred."; | 38 const char kUnknownError[] = "Unknown error occurred."; |
| 41 | 39 |
| 42 std::string SHA1HashHexString(const std::string& str) { | |
| 43 std::string hash = base::SHA1HashString(str); | |
| 44 return base::HexEncode(hash.data(), hash.size()); | |
| 45 } | |
| 46 | |
| 47 const char* GcmResultToError(gcm::GCMClient::Result result) { | 40 const char* GcmResultToError(gcm::GCMClient::Result result) { |
| 48 switch (result) { | 41 switch (result) { |
| 49 case gcm::GCMClient::SUCCESS: | 42 case gcm::GCMClient::SUCCESS: |
| 50 return ""; | 43 return ""; |
| 51 case gcm::GCMClient::INVALID_PARAMETER: | 44 case gcm::GCMClient::INVALID_PARAMETER: |
| 52 return kInvalidParameter; | 45 return kInvalidParameter; |
| 53 case gcm::GCMClient::NOT_SIGNED_IN: | 46 case gcm::GCMClient::NOT_SIGNED_IN: |
| 54 return kNotSignedIn; | 47 return kNotSignedIn; |
| 55 case gcm::GCMClient::CERTIFICATE_MISSING: | |
| 56 return kCertificateMissing; | |
| 57 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 48 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 58 return kAsyncOperationPending; | 49 return kAsyncOperationPending; |
| 59 case gcm::GCMClient::NETWORK_ERROR: | 50 case gcm::GCMClient::NETWORK_ERROR: |
| 60 return kNetworkError; | 51 return kNetworkError; |
| 61 case gcm::GCMClient::SERVER_ERROR: | 52 case gcm::GCMClient::SERVER_ERROR: |
| 62 return kServerError; | 53 return kServerError; |
| 63 case gcm::GCMClient::TTL_EXCEEDED: | 54 case gcm::GCMClient::TTL_EXCEEDED: |
| 64 return kTtlExceeded; | 55 return kTtlExceeded; |
| 65 case gcm::GCMClient::UNKNOWN_ERROR: | 56 case gcm::GCMClient::UNKNOWN_ERROR: |
| 66 return kUnknownError; | 57 return kUnknownError; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 105 |
| 115 GcmRegisterFunction::GcmRegisterFunction() {} | 106 GcmRegisterFunction::GcmRegisterFunction() {} |
| 116 | 107 |
| 117 GcmRegisterFunction::~GcmRegisterFunction() {} | 108 GcmRegisterFunction::~GcmRegisterFunction() {} |
| 118 | 109 |
| 119 bool GcmRegisterFunction::DoWork() { | 110 bool GcmRegisterFunction::DoWork() { |
| 120 scoped_ptr<api::gcm::Register::Params> params( | 111 scoped_ptr<api::gcm::Register::Params> params( |
| 121 api::gcm::Register::Params::Create(*args_)); | 112 api::gcm::Register::Params::Create(*args_)); |
| 122 EXTENSION_FUNCTION_VALIDATE(params.get()); | 113 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 123 | 114 |
| 124 if (GetExtension()->public_key().empty()) { | |
| 125 CompleteFunctionWithResult(std::string(), | |
| 126 gcm::GCMClient::CERTIFICATE_MISSING); | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 GCMProfileService()->Register( | 115 GCMProfileService()->Register( |
| 131 GetExtension()->id(), | 116 GetExtension()->id(), |
| 132 params->sender_ids, | 117 params->sender_ids, |
| 133 SHA1HashHexString(GetExtension()->public_key()), | |
| 134 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); | 118 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); |
| 135 | 119 |
| 136 return true; | 120 return true; |
| 137 } | 121 } |
| 138 | 122 |
| 139 void GcmRegisterFunction::CompleteFunctionWithResult( | 123 void GcmRegisterFunction::CompleteFunctionWithResult( |
| 140 const std::string& registration_id, | 124 const std::string& registration_id, |
| 141 gcm::GCMClient::Result result) { | 125 gcm::GCMClient::Result result) { |
| 142 SetResult(base::Value::CreateStringValue(registration_id)); | 126 SetResult(base::Value::CreateStringValue(registration_id)); |
| 143 SetError(GcmResultToError(result)); | 127 SetError(GcmResultToError(result)); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 215 |
| 232 scoped_ptr<Event> event(new Event( | 216 scoped_ptr<Event> event(new Event( |
| 233 api::gcm::OnSendError::kEventName, | 217 api::gcm::OnSendError::kEventName, |
| 234 api::gcm::OnSendError::Create(error).Pass(), | 218 api::gcm::OnSendError::Create(error).Pass(), |
| 235 profile_)); | 219 profile_)); |
| 236 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 220 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 237 app_id, event.Pass()); | 221 app_id, event.Pass()); |
| 238 } | 222 } |
| 239 | 223 |
| 240 } // namespace extensions | 224 } // namespace extensions |
| OLD | NEW |