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 |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 const size_t kMaximumMessageSize = 4096; // in bytes. | 25 const size_t kMaximumMessageSize = 4096; // in bytes. |
26 const char kCollapseKey[] = "collapse_key"; | 26 const char kCollapseKey[] = "collapse_key"; |
27 const char kGoogDotRestrictedPrefix[] = "goog."; | 27 const char kGoogDotRestrictedPrefix[] = "goog."; |
28 const char kGoogleRestrictedPrefix[] = "google"; | 28 const char kGoogleRestrictedPrefix[] = "google"; |
29 | 29 |
30 // Error messages. | 30 // Error messages. |
31 const char kInvalidParameter[] = | 31 const char kInvalidParameter[] = |
32 "Function was called with invalid parameters."; | 32 "Function was called with invalid parameters."; |
33 const char kNotSignedIn[] = "Profile was not signed in."; | 33 const char kNotSignedIn[] = "Profile was not signed in."; |
| 34 const char kCertificateMissing[] = "Manifest key was missing."; |
34 const char kAsyncOperationPending[] = | 35 const char kAsyncOperationPending[] = |
35 "Asynchronous operation is pending."; | 36 "Asynchronous operation is pending."; |
36 const char kNetworkError[] = "Network error occurred."; | 37 const char kNetworkError[] = "Network error occurred."; |
37 const char kServerError[] = "Server error occurred."; | 38 const char kServerError[] = "Server error occurred."; |
38 const char kTtlExceeded[] = "Time-to-live exceeded."; | 39 const char kTtlExceeded[] = "Time-to-live exceeded."; |
39 const char kUnknownError[] = "Unknown error occurred."; | 40 const char kUnknownError[] = "Unknown error occurred."; |
40 | 41 |
41 std::string SHA1HashHexString(const std::string& str) { | 42 std::string SHA1HashHexString(const std::string& str) { |
42 std::string hash = base::SHA1HashString(str); | 43 std::string hash = base::SHA1HashString(str); |
43 return base::HexEncode(hash.data(), hash.size()); | 44 return base::HexEncode(hash.data(), hash.size()); |
44 } | 45 } |
45 | 46 |
46 const char* GcmResultToError(gcm::GCMClient::Result result) { | 47 const char* GcmResultToError(gcm::GCMClient::Result result) { |
47 switch (result) { | 48 switch (result) { |
48 case gcm::GCMClient::SUCCESS: | 49 case gcm::GCMClient::SUCCESS: |
49 return ""; | 50 return ""; |
50 case gcm::GCMClient::INVALID_PARAMETER: | 51 case gcm::GCMClient::INVALID_PARAMETER: |
51 return kInvalidParameter; | 52 return kInvalidParameter; |
52 case gcm::GCMClient::NOT_SIGNED_IN: | 53 case gcm::GCMClient::NOT_SIGNED_IN: |
53 return kNotSignedIn; | 54 return kNotSignedIn; |
| 55 case gcm::GCMClient::CERTIFICATE_MISSING: |
| 56 return kCertificateMissing; |
54 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 57 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
55 return kAsyncOperationPending; | 58 return kAsyncOperationPending; |
56 case gcm::GCMClient::NETWORK_ERROR: | 59 case gcm::GCMClient::NETWORK_ERROR: |
57 return kNetworkError; | 60 return kNetworkError; |
58 case gcm::GCMClient::SERVER_ERROR: | 61 case gcm::GCMClient::SERVER_ERROR: |
59 return kServerError; | 62 return kServerError; |
60 case gcm::GCMClient::TTL_EXCEEDED: | 63 case gcm::GCMClient::TTL_EXCEEDED: |
61 return kTtlExceeded; | 64 return kTtlExceeded; |
62 case gcm::GCMClient::UNKNOWN_ERROR: | 65 case gcm::GCMClient::UNKNOWN_ERROR: |
63 return kUnknownError; | 66 return kUnknownError; |
(...skipping 24 matching lines...) Expand all Loading... |
88 | 91 |
89 bool GcmApiFunction::RunImpl() { | 92 bool GcmApiFunction::RunImpl() { |
90 if (!IsGcmApiEnabled()) | 93 if (!IsGcmApiEnabled()) |
91 return false; | 94 return false; |
92 | 95 |
93 return DoWork(); | 96 return DoWork(); |
94 } | 97 } |
95 | 98 |
96 bool GcmApiFunction::IsGcmApiEnabled() const { | 99 bool GcmApiFunction::IsGcmApiEnabled() const { |
97 return gcm::GCMProfileService::IsGCMEnabled( | 100 return gcm::GCMProfileService::IsGCMEnabled( |
98 Profile::FromBrowserContext(context())) && | 101 Profile::FromBrowserContext(context())); |
99 !GetExtension()->public_key().empty(); | |
100 } | 102 } |
101 | 103 |
102 gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const { | 104 gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const { |
103 return gcm::GCMProfileServiceFactory::GetForProfile( | 105 return gcm::GCMProfileServiceFactory::GetForProfile( |
104 Profile::FromBrowserContext(context())); | 106 Profile::FromBrowserContext(context())); |
105 } | 107 } |
106 | 108 |
107 GcmRegisterFunction::GcmRegisterFunction() {} | 109 GcmRegisterFunction::GcmRegisterFunction() {} |
108 | 110 |
109 GcmRegisterFunction::~GcmRegisterFunction() {} | 111 GcmRegisterFunction::~GcmRegisterFunction() {} |
110 | 112 |
111 bool GcmRegisterFunction::DoWork() { | 113 bool GcmRegisterFunction::DoWork() { |
112 scoped_ptr<api::gcm::Register::Params> params( | 114 scoped_ptr<api::gcm::Register::Params> params( |
113 api::gcm::Register::Params::Create(*args_)); | 115 api::gcm::Register::Params::Create(*args_)); |
114 EXTENSION_FUNCTION_VALIDATE(params.get()); | 116 EXTENSION_FUNCTION_VALIDATE(params.get()); |
115 | 117 |
| 118 if (GetExtension()->public_key().empty()) { |
| 119 CompleteFunctionWithResult(std::string(), |
| 120 gcm::GCMClient::CERTIFICATE_MISSING); |
| 121 return false; |
| 122 } |
| 123 |
116 GCMProfileService()->Register( | 124 GCMProfileService()->Register( |
117 GetExtension()->id(), | 125 GetExtension()->id(), |
118 params->sender_ids, | 126 params->sender_ids, |
119 SHA1HashHexString(GetExtension()->public_key()), | 127 SHA1HashHexString(GetExtension()->public_key()), |
120 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); | 128 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); |
121 | 129 |
122 return true; | 130 return true; |
123 } | 131 } |
124 | 132 |
125 void GcmRegisterFunction::CompleteFunctionWithResult( | 133 void GcmRegisterFunction::CompleteFunctionWithResult( |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 225 |
218 scoped_ptr<Event> event(new Event( | 226 scoped_ptr<Event> event(new Event( |
219 api::gcm::OnSendError::kEventName, | 227 api::gcm::OnSendError::kEventName, |
220 api::gcm::OnSendError::Create(error).Pass(), | 228 api::gcm::OnSendError::Create(error).Pass(), |
221 profile_)); | 229 profile_)); |
222 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 230 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
223 app_id, event.Pass()); | 231 app_id, event.Pass()); |
224 } | 232 } |
225 | 233 |
226 } // namespace extensions | 234 } // namespace extensions |
OLD | NEW |