From c317f07b5668d6e081a54ad9f6636555f35e0994 Mon Sep 17 00:00:00 2001
From: yang-g <yangg@google.com>
Date: Thu, 20 Aug 2015 12:18:08 -0700
Subject: [PATCH] Make Channel ctor private

---
 BUILD                                         |  4 ++
 Makefile                                      |  2 +
 build.json                                    |  2 +
 include/grpc++/channel.h                      |  6 ++-
 src/cpp/client/channel.cc                     |  2 -
 src/cpp/client/create_channel.cc              | 12 +++--
 src/cpp/client/create_channel_internal.cc     | 46 +++++++++++++++++
 src/cpp/client/create_channel_internal.h      | 51 +++++++++++++++++++
 src/cpp/client/insecure_credentials.cc        |  6 ++-
 src/cpp/client/secure_credentials.cc          |  6 +--
 tools/doxygen/Doxyfile.c++.internal           |  2 +
 tools/run_tests/sources_and_headers.json      |  6 +++
 vsprojects/grpc++/grpc++.vcxproj              |  3 ++
 vsprojects/grpc++/grpc++.vcxproj.filters      |  6 +++
 .../grpc++_unsecure/grpc++_unsecure.vcxproj   |  3 ++
 .../grpc++_unsecure.vcxproj.filters           |  6 +++
 16 files changed, 149 insertions(+), 14 deletions(-)
 create mode 100644 src/cpp/client/create_channel_internal.cc
 create mode 100644 src/cpp/client/create_channel_internal.h

diff --git a/BUILD b/BUILD
index 514b662101..d712053a3d 100644
--- a/BUILD
+++ b/BUILD
@@ -675,6 +675,7 @@ cc_library(
     "src/cpp/client/secure_credentials.h",
     "src/cpp/common/secure_auth_context.h",
     "src/cpp/server/secure_server_credentials.h",
+    "src/cpp/client/create_channel_internal.h",
     "src/cpp/common/create_auth_context.h",
     "src/cpp/client/secure_channel_arguments.cc",
     "src/cpp/client/secure_credentials.cc",
@@ -686,6 +687,7 @@ cc_library(
     "src/cpp/client/channel_arguments.cc",
     "src/cpp/client/client_context.cc",
     "src/cpp/client/create_channel.cc",
+    "src/cpp/client/create_channel_internal.cc",
     "src/cpp/client/credentials.cc",
     "src/cpp/client/generic_stub.cc",
     "src/cpp/client/insecure_credentials.cc",
@@ -764,12 +766,14 @@ cc_library(
 cc_library(
   name = "grpc++_unsecure",
   srcs = [
+    "src/cpp/client/create_channel_internal.h",
     "src/cpp/common/create_auth_context.h",
     "src/cpp/common/insecure_create_auth_context.cc",
     "src/cpp/client/channel.cc",
     "src/cpp/client/channel_arguments.cc",
     "src/cpp/client/client_context.cc",
     "src/cpp/client/create_channel.cc",
+    "src/cpp/client/create_channel_internal.cc",
     "src/cpp/client/credentials.cc",
     "src/cpp/client/generic_stub.cc",
     "src/cpp/client/insecure_credentials.cc",
diff --git a/Makefile b/Makefile
index ce6406112e..c74c720b7b 100644
--- a/Makefile
+++ b/Makefile
@@ -4603,6 +4603,7 @@ LIBGRPC++_SRC = \
     src/cpp/client/channel_arguments.cc \
     src/cpp/client/client_context.cc \
     src/cpp/client/create_channel.cc \
+    src/cpp/client/create_channel_internal.cc \
     src/cpp/client/credentials.cc \
     src/cpp/client/generic_stub.cc \
     src/cpp/client/insecure_credentials.cc \
@@ -4844,6 +4845,7 @@ LIBGRPC++_UNSECURE_SRC = \
     src/cpp/client/channel_arguments.cc \
     src/cpp/client/client_context.cc \
     src/cpp/client/create_channel.cc \
+    src/cpp/client/create_channel_internal.cc \
     src/cpp/client/credentials.cc \
     src/cpp/client/generic_stub.cc \
     src/cpp/client/insecure_credentials.cc \
diff --git a/build.json b/build.json
index d49b953d8a..64558a6028 100644
--- a/build.json
+++ b/build.json
@@ -72,6 +72,7 @@
         "include/grpc++/time.h"
       ],
       "headers": [
+        "src/cpp/client/create_channel_internal.h",
         "src/cpp/common/create_auth_context.h"
       ],
       "src": [
@@ -79,6 +80,7 @@
         "src/cpp/client/channel_arguments.cc",
         "src/cpp/client/client_context.cc",
         "src/cpp/client/create_channel.cc",
+        "src/cpp/client/create_channel_internal.cc",
         "src/cpp/client/credentials.cc",
         "src/cpp/client/generic_stub.cc",
         "src/cpp/client/insecure_credentials.cc",
diff --git a/include/grpc++/channel.h b/include/grpc++/channel.h
index ceb921eff8..7d6216e9c4 100644
--- a/include/grpc++/channel.h
+++ b/include/grpc++/channel.h
@@ -69,8 +69,6 @@ class Channel GRPC_FINAL : public GrpcLibrary,
                            public CallHook,
                            public std::enable_shared_from_this<Channel> {
  public:
-  explicit Channel(grpc_channel* c_channel);
-  Channel(const grpc::string& host, grpc_channel* c_channel);
   ~Channel();
 
   // Get the current channel state. If the channel is in IDLE and try_to_connect
@@ -115,6 +113,10 @@ class Channel GRPC_FINAL : public GrpcLibrary,
                                   const InputMessage& request,
                                   OutputMessage* result);
   friend class ::grpc::RpcMethod;
+  friend std::shared_ptr<Channel> CreateChannelInternal(
+      const grpc::string& host, grpc_channel* c_channel);
+
+  Channel(const grpc::string& host, grpc_channel* c_channel);
 
   Call CreateCall(const RpcMethod& method, ClientContext* context,
                   CompletionQueue* cq);
diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc
index d9ffdc30cd..90fc776d4a 100644
--- a/src/cpp/client/channel.cc
+++ b/src/cpp/client/channel.cc
@@ -52,8 +52,6 @@
 
 namespace grpc {
 
-Channel::Channel(grpc_channel* channel) : c_channel_(channel) {}
-
 Channel::Channel(const grpc::string& host, grpc_channel* channel)
     : host_(host), c_channel_(channel) {}
 
diff --git a/src/cpp/client/create_channel.cc b/src/cpp/client/create_channel.cc
index 704470693e..70ea7e0e27 100644
--- a/src/cpp/client/create_channel.cc
+++ b/src/cpp/client/create_channel.cc
@@ -38,6 +38,8 @@
 #include <grpc++/channel_arguments.h>
 #include <grpc++/create_channel.h>
 
+#include "src/cpp/client/create_channel_internal.h"
+
 namespace grpc {
 class ChannelArguments;
 
@@ -49,10 +51,10 @@ std::shared_ptr<Channel> CreateChannel(
   user_agent_prefix << "grpc-c++/" << grpc_version_string();
   cp_args.SetString(GRPC_ARG_PRIMARY_USER_AGENT_STRING,
                     user_agent_prefix.str());
-  return creds ? creds->CreateChannel(target, cp_args)
-               : std::shared_ptr<Channel>(
-                     new Channel(grpc_lame_client_channel_create(
-                         NULL, GRPC_STATUS_INVALID_ARGUMENT,
-                         "Invalid credentials.")));
+  return creds
+             ? creds->CreateChannel(target, cp_args)
+             : CreateChannelInternal("", grpc_lame_client_channel_create(
+                                             NULL, GRPC_STATUS_INVALID_ARGUMENT,
+                                             "Invalid credentials."));
 }
 }  // namespace grpc
diff --git a/src/cpp/client/create_channel_internal.cc b/src/cpp/client/create_channel_internal.cc
new file mode 100644
index 0000000000..9c5ab038cf
--- /dev/null
+++ b/src/cpp/client/create_channel_internal.cc
@@ -0,0 +1,46 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <memory>
+
+#include <grpc++/channel.h>
+
+struct grpc_channel;
+
+namespace grpc {
+
+std::shared_ptr<Channel> CreateChannelInternal(const grpc::string& host,
+                                               grpc_channel* c_channel) {
+  return std::shared_ptr<Channel>(new Channel(host, c_channel));
+}
+}  // namespace grpc
diff --git a/src/cpp/client/create_channel_internal.h b/src/cpp/client/create_channel_internal.h
new file mode 100644
index 0000000000..1692471990
--- /dev/null
+++ b/src/cpp/client/create_channel_internal.h
@@ -0,0 +1,51 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_INTERNAL_CPP_CLIENT_CREATE_CHANNEL_INTERNAL_H
+#define GRPC_INTERNAL_CPP_CLIENT_CREATE_CHANNEL_INTERNAL_H
+
+#include <memory>
+
+#include <grpc++/config.h>
+
+struct grpc_channel;
+
+namespace grpc {
+class Channel;
+
+std::shared_ptr<Channel> CreateChannelInternal(const grpc::string& host,
+                                               grpc_channel* c_channel);
+
+}  // namespace grpc
+
+#endif  // GRPC_INTERNAL_CPP_CLIENT_CREATE_CHANNEL_INTERNAL_H
diff --git a/src/cpp/client/insecure_credentials.cc b/src/cpp/client/insecure_credentials.cc
index 70ce17dc6d..97931406af 100644
--- a/src/cpp/client/insecure_credentials.cc
+++ b/src/cpp/client/insecure_credentials.cc
@@ -38,6 +38,7 @@
 #include <grpc++/channel_arguments.h>
 #include <grpc++/config.h>
 #include <grpc++/credentials.h>
+#include "src/cpp/client/create_channel_internal.h"
 
 namespace grpc {
 
@@ -48,8 +49,9 @@ class InsecureCredentialsImpl GRPC_FINAL : public Credentials {
       const string& target, const grpc::ChannelArguments& args) GRPC_OVERRIDE {
     grpc_channel_args channel_args;
     args.SetChannelArgs(&channel_args);
-    return std::shared_ptr<Channel>(new Channel(
-        grpc_insecure_channel_create(target.c_str(), &channel_args, nullptr)));
+    return CreateChannelInternal(
+        "",
+        grpc_insecure_channel_create(target.c_str(), &channel_args, nullptr));
   }
 
   // InsecureCredentials should not be applied to a call.
diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc
index b32f783fa3..1e912c6beb 100644
--- a/src/cpp/client/secure_credentials.cc
+++ b/src/cpp/client/secure_credentials.cc
@@ -32,10 +32,10 @@
  */
 
 #include <grpc/support/log.h>
-
 #include <grpc++/channel.h>
 #include <grpc++/channel_arguments.h>
 #include <grpc++/impl/grpc_library.h>
+#include "src/cpp/client/create_channel_internal.h"
 #include "src/cpp/client/secure_credentials.h"
 
 namespace grpc {
@@ -44,9 +44,9 @@ std::shared_ptr<grpc::Channel> SecureCredentials::CreateChannel(
     const string& target, const grpc::ChannelArguments& args) {
   grpc_channel_args channel_args;
   args.SetChannelArgs(&channel_args);
-  return std::shared_ptr<Channel>(new Channel(
+  return CreateChannelInternal(
       args.GetSslTargetNameOverride(),
-      grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args)));
+      grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args));
 }
 
 bool SecureCredentials::ApplyToCall(grpc_call* call) {
diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal
index b8f04cbcd3..23ff05fb86 100644
--- a/tools/doxygen/Doxyfile.c++.internal
+++ b/tools/doxygen/Doxyfile.c++.internal
@@ -803,6 +803,7 @@ include/grpc++/time.h \
 src/cpp/client/secure_credentials.h \
 src/cpp/common/secure_auth_context.h \
 src/cpp/server/secure_server_credentials.h \
+src/cpp/client/create_channel_internal.h \
 src/cpp/common/create_auth_context.h \
 src/cpp/client/secure_channel_arguments.cc \
 src/cpp/client/secure_credentials.cc \
@@ -814,6 +815,7 @@ src/cpp/client/channel.cc \
 src/cpp/client/channel_arguments.cc \
 src/cpp/client/client_context.cc \
 src/cpp/client/create_channel.cc \
+src/cpp/client/create_channel_internal.cc \
 src/cpp/client/credentials.cc \
 src/cpp/client/generic_stub.cc \
 src/cpp/client/insecure_credentials.cc \
diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json
index 5c515d1ba4..863c9f802e 100644
--- a/tools/run_tests/sources_and_headers.json
+++ b/tools/run_tests/sources_and_headers.json
@@ -13141,6 +13141,7 @@
       "include/grpc++/stub_options.h", 
       "include/grpc++/thread_pool_interface.h", 
       "include/grpc++/time.h", 
+      "src/cpp/client/create_channel_internal.h", 
       "src/cpp/client/secure_credentials.h", 
       "src/cpp/common/create_auth_context.h", 
       "src/cpp/common/secure_auth_context.h", 
@@ -13193,6 +13194,8 @@
       "src/cpp/client/channel_arguments.cc", 
       "src/cpp/client/client_context.cc", 
       "src/cpp/client/create_channel.cc", 
+      "src/cpp/client/create_channel_internal.cc", 
+      "src/cpp/client/create_channel_internal.h", 
       "src/cpp/client/credentials.cc", 
       "src/cpp/client/generic_stub.cc", 
       "src/cpp/client/insecure_credentials.cc", 
@@ -13310,6 +13313,7 @@
       "include/grpc++/stub_options.h", 
       "include/grpc++/thread_pool_interface.h", 
       "include/grpc++/time.h", 
+      "src/cpp/client/create_channel_internal.h", 
       "src/cpp/common/create_auth_context.h"
     ], 
     "language": "c++", 
@@ -13359,6 +13363,8 @@
       "src/cpp/client/channel_arguments.cc", 
       "src/cpp/client/client_context.cc", 
       "src/cpp/client/create_channel.cc", 
+      "src/cpp/client/create_channel_internal.cc", 
+      "src/cpp/client/create_channel_internal.h", 
       "src/cpp/client/credentials.cc", 
       "src/cpp/client/generic_stub.cc", 
       "src/cpp/client/insecure_credentials.cc", 
diff --git a/vsprojects/grpc++/grpc++.vcxproj b/vsprojects/grpc++/grpc++.vcxproj
index dbbb7c12cd..e2e17d4177 100644
--- a/vsprojects/grpc++/grpc++.vcxproj
+++ b/vsprojects/grpc++/grpc++.vcxproj
@@ -258,6 +258,7 @@
     <ClInclude Include="..\..\src\cpp\client\secure_credentials.h" />
     <ClInclude Include="..\..\src\cpp\common\secure_auth_context.h" />
     <ClInclude Include="..\..\src\cpp\server\secure_server_credentials.h" />
+    <ClInclude Include="..\..\src\cpp\client\create_channel_internal.h" />
     <ClInclude Include="..\..\src\cpp\common\create_auth_context.h" />
   </ItemGroup>
   <ItemGroup>
@@ -281,6 +282,8 @@
     </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\create_channel.cc">
     </ClCompile>
+    <ClCompile Include="..\..\src\cpp\client\create_channel_internal.cc">
+    </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\credentials.cc">
     </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\generic_stub.cc">
diff --git a/vsprojects/grpc++/grpc++.vcxproj.filters b/vsprojects/grpc++/grpc++.vcxproj.filters
index 7c982e910a..6f308d1d02 100644
--- a/vsprojects/grpc++/grpc++.vcxproj.filters
+++ b/vsprojects/grpc++/grpc++.vcxproj.filters
@@ -31,6 +31,9 @@
     <ClCompile Include="..\..\src\cpp\client\create_channel.cc">
       <Filter>src\cpp\client</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\cpp\client\create_channel_internal.cc">
+      <Filter>src\cpp\client</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\credentials.cc">
       <Filter>src\cpp\client</Filter>
     </ClCompile>
@@ -224,6 +227,9 @@
     <ClInclude Include="..\..\src\cpp\server\secure_server_credentials.h">
       <Filter>src\cpp\server</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\cpp\client\create_channel_internal.h">
+      <Filter>src\cpp\client</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\cpp\common\create_auth_context.h">
       <Filter>src\cpp\common</Filter>
     </ClInclude>
diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj
index 98d15cd98d..4be468cb62 100644
--- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj
+++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj
@@ -255,6 +255,7 @@
     <ClInclude Include="..\..\include\grpc++\time.h" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\..\src\cpp\client\create_channel_internal.h" />
     <ClInclude Include="..\..\src\cpp\common\create_auth_context.h" />
   </ItemGroup>
   <ItemGroup>
@@ -268,6 +269,8 @@
     </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\create_channel.cc">
     </ClCompile>
+    <ClCompile Include="..\..\src\cpp\client\create_channel_internal.cc">
+    </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\credentials.cc">
     </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\generic_stub.cc">
diff --git a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters
index 99734c8953..bd51d1fa0b 100644
--- a/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters
+++ b/vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters
@@ -16,6 +16,9 @@
     <ClCompile Include="..\..\src\cpp\client\create_channel.cc">
       <Filter>src\cpp\client</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\cpp\client\create_channel_internal.cc">
+      <Filter>src\cpp\client</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\src\cpp\client\credentials.cc">
       <Filter>src\cpp\client</Filter>
     </ClCompile>
@@ -200,6 +203,9 @@
     </ClInclude>
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\..\src\cpp\client\create_channel_internal.h">
+      <Filter>src\cpp\client</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\src\cpp\common\create_auth_context.h">
       <Filter>src\cpp\common</Filter>
     </ClInclude>
-- 
GitLab