Add EncryptMessage to GcmEncryptionProvider

- Introduce encryption result enum and callback
- Added encryption logic in GcmEncryptionProvider based on
  EncryptionRoundTrip unit test

Bug: 966035
Change-Id: I390651722f89eaf380c8a2e8d43a76ad72d447c4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1635610
Reviewed-by: Peter Beverloo <[email protected]>
Reviewed-by: Steven Holte <[email protected]>
Commit-Queue: Alex Chau <[email protected]>
Auto-Submit: Alex Chau <[email protected]>
Cr-Commit-Position: refs/heads/master@{#666230}
diff --git a/components/gcm_driver/gcm_driver.cc b/components/gcm_driver/gcm_driver.cc
index f3cf956..d8ed9b0 100644
--- a/components/gcm_driver/gcm_driver.cc
+++ b/components/gcm_driver/gcm_driver.cc
@@ -13,6 +13,7 @@
 #include "base/logging.h"
 #include "base/metrics/histogram_macros.h"
 #include "components/gcm_driver/crypto/gcm_decryption_result.h"
+#include "components/gcm_driver/crypto/gcm_encryption_result.h"
 #include "components/gcm_driver/gcm_app_handler.h"
 
 namespace gcm {
@@ -325,4 +326,43 @@
   RegisterImpl(app_id, normalized_sender_ids);
 }
 
+void GCMDriver::SendWebPushMessage(const std::string& app_id,
+                                   const std::string& authorized_entity,
+                                   const std::string& p256dh,
+                                   const std::string& auth_secret,
+                                   const std::string& fcm_token,
+                                   crypto::ECPrivateKey* vapid_key,
+                                   int time_to_live,
+                                   const std::string& message) {
+  encryption_provider_.EncryptMessage(
+      app_id, authorized_entity, p256dh, auth_secret, message,
+      base::Bind(&GCMDriver::OnMessageEncrypted, weak_ptr_factory_.GetWeakPtr(),
+                 fcm_token, vapid_key, time_to_live));
+}
+
+void GCMDriver::OnMessageEncrypted(const std::string& fcm_token,
+                                   crypto::ECPrivateKey* vapid_key,
+                                   int time_to_live,
+                                   GCMEncryptionResult result,
+                                   const std::string& message) {
+  UMA_HISTOGRAM_ENUMERATION("GCM.Crypto.EncryptMessageResult", result,
+                            GCMEncryptionResult::ENUM_SIZE);
+
+  switch (result) {
+    case GCMEncryptionResult::ENCRYPTED_DRAFT_08:
+      // TODO: send the message.
+      return;
+    case GCMEncryptionResult::NO_KEYS:
+    case GCMEncryptionResult::INVALID_SHARED_SECRET:
+    case GCMEncryptionResult::ENCRYPTION_FAILED: {
+      LOG(ERROR) << "Webpush message encryption failed";
+      return;
+    }
+    case GCMEncryptionResult::ENUM_SIZE:
+      break;  // deliberate fall-through
+  }
+
+  NOTREACHED();
+}
+
 }  // namespace gcm