Componentize GCM Part 1: create GCM component and move some files over

BUG=356716
TEST=existing tests

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=270226

[email protected], [email protected], [email protected], [email protected], [email protected], [email protected]

Review URL: https://codereview.chromium.org/261853012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270470 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/components/gcm_driver/DEPS b/components/gcm_driver/DEPS
new file mode 100644
index 0000000..b69f312
--- /dev/null
+++ b/components/gcm_driver/DEPS
@@ -0,0 +1,4 @@
+include_rules = [
+  "+components/os_crypt",
+  "+google_apis/gcm",
+]
diff --git a/components/gcm_driver/OWNERS b/components/gcm_driver/OWNERS
new file mode 100644
index 0000000..0f8cc7a
--- /dev/null
+++ b/components/gcm_driver/OWNERS
@@ -0,0 +1,4 @@
[email protected]
[email protected]
[email protected]
[email protected]
diff --git a/components/gcm_driver/default_gcm_app_handler.cc b/components/gcm_driver/default_gcm_app_handler.cc
new file mode 100644
index 0000000..80aee2f
--- /dev/null
+++ b/components/gcm_driver/default_gcm_app_handler.cc
@@ -0,0 +1,39 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/default_gcm_app_handler.h"
+
+#include "base/logging.h"
+
+namespace gcm {
+
+DefaultGCMAppHandler::DefaultGCMAppHandler() {
+}
+
+DefaultGCMAppHandler::~DefaultGCMAppHandler() {
+}
+
+void DefaultGCMAppHandler::ShutdownHandler() {
+  // Nothing to do.
+}
+
+void DefaultGCMAppHandler::OnMessage(
+    const std::string& app_id,
+    const GCMClient::IncomingMessage& message) {
+  LOG(ERROR) << "No app handler is found to route message for " << app_id;
+}
+
+void DefaultGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
+  LOG(ERROR) << "No app handler is found to route deleted message for "
+             << app_id;
+}
+
+void DefaultGCMAppHandler::OnSendError(
+    const std::string& app_id,
+    const GCMClient::SendErrorDetails& send_error_details) {
+  LOG(ERROR) << "No app handler is found to route send error message for "
+             << app_id;
+}
+
+}  // namespace gcm
diff --git a/components/gcm_driver/default_gcm_app_handler.h b/components/gcm_driver/default_gcm_app_handler.h
new file mode 100644
index 0000000..0f6c4ce
--- /dev/null
+++ b/components/gcm_driver/default_gcm_app_handler.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.

+// Use of this source code is governed by a BSD-style license that can be

+// found in the LICENSE file.

+

+#ifndef COMPONENTS_GCM_DRIVER_DEFAULT_GCM_APP_HANDLER_H_

+#define COMPONENTS_GCM_DRIVER_DEFAULT_GCM_APP_HANDLER_H_

+

+#include "base/compiler_specific.h"

+#include "components/gcm_driver/gcm_app_handler.h"

+

+namespace gcm {

+

+// The default app handler that is triggered when there is no registered app

+// handler for an application id.

+class DefaultGCMAppHandler : public GCMAppHandler {

+ public:

+  DefaultGCMAppHandler();

+  virtual ~DefaultGCMAppHandler();

+

+  // Overridden from GCMAppHandler:

+  virtual void ShutdownHandler() OVERRIDE;

+  virtual void OnMessage(const std::string& app_id,

+                         const GCMClient::IncomingMessage& message) OVERRIDE;

+  virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;

+  virtual void OnSendError(

+      const std::string& app_id,

+      const GCMClient::SendErrorDetails& send_error_details) OVERRIDE;

+

+ private:

+  DISALLOW_COPY_AND_ASSIGN(DefaultGCMAppHandler);

+};

+

+}  // namespace gcm

+

+#endif  // COMPONENTS_GCM_DRIVER_DEFAULT_GCM_APP_HANDLER_H_

diff --git a/components/gcm_driver/gcm_app_handler.h b/components/gcm_driver/gcm_app_handler.h
new file mode 100644
index 0000000..f1487a7
--- /dev/null
+++ b/components/gcm_driver/gcm_app_handler.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.

+// Use of this source code is governed by a BSD-style license that can be

+// found in the LICENSE file.

+

+#ifndef COMPONENTS_GCM_DRIVER_GCM_APP_HANDLER_H_

+#define COMPONENTS_GCM_DRIVER_GCM_APP_HANDLER_H_

+

+#include <string>

+

+#include "base/basictypes.h"

+#include "google_apis/gcm/gcm_client.h"

+

+namespace gcm {

+

+// Defines the interface to provide handling and event routing logic for a given

+// app.

+class GCMAppHandler {

+ public:

+  GCMAppHandler() {}

+  virtual ~GCMAppHandler() {}

+

+  // Called to do all the cleanup when GCM is shutting down.

+  // In the case that multiple apps share the same app handler, it should be

+  // make safe for ShutdownHandler to be called multiple times.

+  virtual void ShutdownHandler() = 0;

+

+  // Called when a GCM message has been received.

+  virtual void OnMessage(const std::string& app_id,

+                         const GCMClient::IncomingMessage& message) = 0;

+

+  // Called when some GCM messages have been deleted from the server.

+  virtual void OnMessagesDeleted(const std::string& app_id) = 0;

+

+  // Called when a GCM message failed to be delivered.

+  virtual void OnSendError(

+      const std::string& app_id,

+      const GCMClient::SendErrorDetails& send_error_details) = 0;

+};

+

+}  // namespace gcm

+

+#endif  // COMPONENTS_GCM_DRIVER_GCM_APP_HANDLER_H_

diff --git a/components/gcm_driver/gcm_client_factory.cc b/components/gcm_driver/gcm_client_factory.cc
new file mode 100644
index 0000000..64d7d5c
--- /dev/null
+++ b/components/gcm_driver/gcm_client_factory.cc
@@ -0,0 +1,22 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/gcm_client_factory.h"
+
+#include "google_apis/gcm/gcm_client_impl.h"
+
+namespace gcm {
+
+scoped_ptr<GCMClient> GCMClientFactory::BuildInstance() {
+  return scoped_ptr<GCMClient>(new GCMClientImpl(
+      make_scoped_ptr<GCMInternalsBuilder>(new GCMInternalsBuilder())));
+}
+
+GCMClientFactory::GCMClientFactory() {
+}
+
+GCMClientFactory::~GCMClientFactory() {
+}
+
+}  // namespace gcm
diff --git a/components/gcm_driver/gcm_client_factory.h b/components/gcm_driver/gcm_client_factory.h
new file mode 100644
index 0000000..78c43f4
--- /dev/null
+++ b/components/gcm_driver/gcm_client_factory.h
@@ -0,0 +1,30 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.

+// Use of this source code is governed by a BSD-style license that can be

+// found in the LICENSE file.

+

+#ifndef COMPONENTS_GCM_DRIVER_GCM_CLIENT_FACTORY_H_

+#define COMPONENTS_GCM_DRIVER_GCM_CLIENT_FACTORY_H_

+

+#include "base/macros.h"

+#include "base/memory/scoped_ptr.h"

+

+namespace gcm {

+

+class GCMClient;

+

+class GCMClientFactory {

+ public:

+  GCMClientFactory();

+  virtual ~GCMClientFactory();

+

+  // Creates a new instance of GCMClient. The testing code could override this

+  // to provide a mocked instance.

+  virtual scoped_ptr<GCMClient> BuildInstance();

+

+ private:

+  DISALLOW_COPY_AND_ASSIGN(GCMClientFactory);

+};

+

+}  // namespace gcm

+

+#endif  // COMPONENTS_GCM_DRIVER_GCM_CLIENT_FACTORY_H_

diff --git a/components/gcm_driver/system_encryptor.cc b/components/gcm_driver/system_encryptor.cc
new file mode 100644
index 0000000..bc731a3
--- /dev/null
+++ b/components/gcm_driver/system_encryptor.cc
@@ -0,0 +1,23 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/system_encryptor.h"
+
+#include "components/os_crypt/os_crypt.h"
+
+namespace gcm {
+
+SystemEncryptor::~SystemEncryptor() {}
+
+bool SystemEncryptor::EncryptString(const std::string& plaintext,
+                                    std::string* ciphertext) {
+  return ::OSCrypt::EncryptString(plaintext, ciphertext);
+}
+
+bool SystemEncryptor::DecryptString(const std::string& ciphertext,
+                                    std::string* plaintext) {
+  return ::OSCrypt::DecryptString(ciphertext, plaintext);
+}
+
+}  // namespace gcm
diff --git a/components/gcm_driver/system_encryptor.h b/components/gcm_driver/system_encryptor.h
new file mode 100644
index 0000000..c7ae645
--- /dev/null
+++ b/components/gcm_driver/system_encryptor.h
@@ -0,0 +1,27 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.

+// Use of this source code is governed by a BSD-style license that can be

+// found in the LICENSE file.

+

+#ifndef COMPONENTS_GCM_DRIVER_SYSTEM_ENCRYPTOR_H_

+#define COMPONENTS_GCM_DRIVER_SYSTEM_ENCRYPTOR_H_

+

+#include "base/compiler_specific.h"

+#include "google_apis/gcm/base/encryptor.h"

+

+namespace gcm {

+

+// Encryptor that uses the Chrome password manager's encryptor.

+class SystemEncryptor : public Encryptor {

+ public:

+  virtual ~SystemEncryptor();

+

+  virtual bool EncryptString(const std::string& plaintext,

+                             std::string* ciphertext) OVERRIDE;

+

+  virtual bool DecryptString(const std::string& ciphertext,

+                             std::string* plaintext) OVERRIDE;

+};

+

+}  // namespace gcm

+

+#endif  // COMPONENTS_GCM_DRIVER_SYSTEM_ENCRYPTOR_H_