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" | 11 #include "base/sha1.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
17 #include "chrome/common/extensions/api/gcm.h" | 18 #include "chrome/common/extensions/api/gcm.h" |
18 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
19 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
20 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const size_t kMaximumMessageSize = 4096; // in bytes. | 25 const size_t kMaximumMessageSize = 4096; // in bytes. |
| 26 const char kCollapseKey[] = "collapse_key"; |
25 const char kGoogDotRestrictedPrefix[] = "goog."; | 27 const char kGoogDotRestrictedPrefix[] = "goog."; |
26 const size_t kGoogDotPrefixLength = arraysize(kGoogDotRestrictedPrefix) - 1; | |
27 const char kGoogleRestrictedPrefix[] = "google"; | 28 const char kGoogleRestrictedPrefix[] = "google"; |
28 const size_t kGooglePrefixLength = arraysize(kGoogleRestrictedPrefix) - 1; | |
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 kAsyncOperationPending[] = | 34 const char kAsyncOperationPending[] = |
35 "Asynchronous operation is pending."; | 35 "Asynchronous operation is pending."; |
36 const char kNetworkError[] = "Network error occured."; | 36 const char kNetworkError[] = "Network error occurred."; |
37 const char kServerError[] = "Server error occured."; | 37 const char kServerError[] = "Server error occurred."; |
38 const char kTtlExceeded[] = "Time-to-live exceeded."; | 38 const char kTtlExceeded[] = "Time-to-live exceeded."; |
39 const char kUnknownError[] = "Unknown error occured."; | 39 const char kUnknownError[] = "Unknown error occurred."; |
40 | 40 |
41 std::string SHA1HashHexString(const std::string& str) { | 41 std::string SHA1HashHexString(const std::string& str) { |
42 std::string hash = base::SHA1HashString(str); | 42 std::string hash = base::SHA1HashString(str); |
43 return base::HexEncode(hash.data(), hash.size()); | 43 return base::HexEncode(hash.data(), hash.size()); |
44 } | 44 } |
45 | 45 |
46 const char* GcmResultToError(gcm::GCMClient::Result result) { | 46 const char* GcmResultToError(gcm::GCMClient::Result result) { |
47 switch (result) { | 47 switch (result) { |
48 case gcm::GCMClient::SUCCESS: | 48 case gcm::GCMClient::SUCCESS: |
49 return ""; | 49 return ""; |
(...skipping 14 matching lines...) Expand all Loading... |
64 default: | 64 default: |
65 NOTREACHED() << "Unexpected value of result cannot be converted: " | 65 NOTREACHED() << "Unexpected value of result cannot be converted: " |
66 << result; | 66 << result; |
67 } | 67 } |
68 | 68 |
69 // Never reached, but prevents missing return statement warning. | 69 // Never reached, but prevents missing return statement warning. |
70 return ""; | 70 return ""; |
71 } | 71 } |
72 | 72 |
73 bool IsMessageKeyValid(const std::string& key) { | 73 bool IsMessageKeyValid(const std::string& key) { |
| 74 std::string lower = StringToLowerASCII(key); |
74 return !key.empty() && | 75 return !key.empty() && |
75 key.compare(0, kGooglePrefixLength, kGoogleRestrictedPrefix) != 0 && | 76 key.compare(0, arraysize(kCollapseKey) - 1, kCollapseKey) != 0 && |
76 key.compare(0, kGoogDotPrefixLength, kGoogDotRestrictedPrefix) != 0; | 77 lower.compare(0, |
| 78 arraysize(kGoogleRestrictedPrefix) - 1, |
| 79 kGoogleRestrictedPrefix) != 0 && |
| 80 lower.compare(0, |
| 81 arraysize(kGoogDotRestrictedPrefix), |
| 82 kGoogDotRestrictedPrefix) != 0; |
77 } | 83 } |
78 | 84 |
79 } // namespace | 85 } // namespace |
80 | 86 |
81 namespace extensions { | 87 namespace extensions { |
82 | 88 |
83 bool GcmApiFunction::RunImpl() { | 89 bool GcmApiFunction::RunImpl() { |
84 if (!IsGcmApiEnabled()) | 90 if (!IsGcmApiEnabled()) |
85 return false; | 91 return false; |
86 | 92 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 217 |
212 scoped_ptr<Event> event(new Event( | 218 scoped_ptr<Event> event(new Event( |
213 api::gcm::OnSendError::kEventName, | 219 api::gcm::OnSendError::kEventName, |
214 api::gcm::OnSendError::Create(error).Pass(), | 220 api::gcm::OnSendError::Create(error).Pass(), |
215 profile_)); | 221 profile_)); |
216 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 222 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
217 app_id, event.Pass()); | 223 app_id, event.Pass()); |
218 } | 224 } |
219 | 225 |
220 } // namespace extensions | 226 } // namespace extensions |
OLD | NEW |