Mutable Partial Network Traffic Annotation Tag Mojo interface added.

Mojo interface added for MutablePartialNetworkTrafficAnnotationTag.
This interface is now required for
public_ip_address_geolocation_provider and will also be required for
all Mojo interfaces that pass a partial network traffic annotation tag.

Bug: 656607
Change-Id: I61dd345a0a60166413c2126c89098e9e1f031f43
Reviewed-on: https://chromium-review.googlesource.com/787250
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: Matt Menke <mmenke@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Renjie Liu <renjieliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520484}
diff --git a/docs/network_traffic_annotations.md b/docs/network_traffic_annotations.md
index 3bdfcf2..fb4746a 100644
--- a/docs/network_traffic_annotations.md
+++ b/docs/network_traffic_annotations.md
@@ -419,4 +419,11 @@
 this limitation. It is strongly suggested that mutable annotations would be used
 only if there is no other way around it. Use cases are checked with the
 `traffic_annotation_auditor` to ensure proper values for the mutable
-annotations.
\ No newline at end of file
+annotations.
+
+
+## Mojo Interfaces (Advanced)
+
+For serialization of network traffic annotation and partial network traffic
+annotation tags, you can use the mutable mojo interfaces defined in
+`/services/network/public/interfaces`.
\ No newline at end of file
diff --git a/net/traffic_annotation/network_traffic_annotation.h b/net/traffic_annotation/network_traffic_annotation.h
index 178cdf7..333e839 100644
--- a/net/traffic_annotation/network_traffic_annotation.h
+++ b/net/traffic_annotation/network_traffic_annotation.h
@@ -207,8 +207,8 @@
 // }
 
 // Do not use this unless net-serialization is required.
-// TODO(crbug.com/690323): Add tools to check constructor of this structure is
-// used only in .mojom.cc files.
+// Mojo interfaces for this class and the next one are defined in
+// '/services/network/public/interfaces'.
 struct MutableNetworkTrafficAnnotationTag {
   MutableNetworkTrafficAnnotationTag()
       : unique_id_hash_code(TRAFFIC_ANNOTATION_UNINITIALIZED) {}
diff --git a/services/network/public/cpp/BUILD.gn b/services/network/public/cpp/BUILD.gn
index 8efcc4f0..7dc3fa5 100644
--- a/services/network/public/cpp/BUILD.gn
+++ b/services/network/public/cpp/BUILD.gn
@@ -9,6 +9,7 @@
     "cors_error_status.cc",
     "cors_error_status.h",
     "mutable_network_traffic_annotation_tag_struct_traits.h",
+    "mutable_partial_network_traffic_annotation_tag_struct_traits.h",
     "net_adapters.cc",
     "net_adapters.h",
     "url_loader_completion_status.cc",
@@ -39,6 +40,8 @@
   testonly = true
 
   sources = [
+    "mutable_network_traffic_annotation_tag_struct_traits_unittest.cc",
+    "mutable_partial_network_traffic_annotation_tag_struct_traits_unittest.cc",
     "network_struct_traits_unittest.cc",
   ]
   deps = [
diff --git a/services/network/public/cpp/mutable_network_traffic_annotation_tag.typemap b/services/network/public/cpp/mutable_network_traffic_annotation_tag.typemap
index 2966622..3b72b65 100644
--- a/services/network/public/cpp/mutable_network_traffic_annotation_tag.typemap
+++ b/services/network/public/cpp/mutable_network_traffic_annotation_tag.typemap
@@ -3,6 +3,7 @@
 # found in the LICENSE file.
 
 mojom = "//services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom"
+public_headers = [ "//net/traffic_annotation/network_traffic_annotation.h" ]
 traits_headers = [ "//services/network/public/cpp/" +
                    "mutable_network_traffic_annotation_tag_struct_traits.h" ]
 deps = [
diff --git a/services/network/public/cpp/mutable_network_traffic_annotation_tag_struct_traits_unittest.cc b/services/network/public/cpp/mutable_network_traffic_annotation_tag_struct_traits_unittest.cc
new file mode 100644
index 0000000..ac5a629
--- /dev/null
+++ b/services/network/public/cpp/mutable_network_traffic_annotation_tag_struct_traits_unittest.cc
@@ -0,0 +1,30 @@
+// Copyright 2017 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 "services/network/public/cpp/mutable_network_traffic_annotation_tag_struct_traits.h"
+
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
+#include "mojo/public/cpp/bindings/binding_set.h"
+#include "services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace network {
+
+TEST(MutableNetworkTrafficAnnottionTagsTest, BasicTest) {
+  net::MutableNetworkTrafficAnnotationTag original;
+  net::MutableNetworkTrafficAnnotationTag copy;
+
+  original.unique_id_hash_code = 1;
+  EXPECT_TRUE(mojom::MutableNetworkTrafficAnnotationTag::Deserialize(
+      mojom::MutableNetworkTrafficAnnotationTag::Serialize(&original), &copy));
+  EXPECT_EQ(copy.unique_id_hash_code, 1);
+
+  original.unique_id_hash_code = 2;
+  EXPECT_TRUE(mojom::MutableNetworkTrafficAnnotationTag::Deserialize(
+      mojom::MutableNetworkTrafficAnnotationTag::Serialize(&original), &copy));
+  EXPECT_EQ(copy.unique_id_hash_code, 2);
+}
+
+}  // namespace network
diff --git a/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag.typemap b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag.typemap
new file mode 100644
index 0000000..d1a3154
--- /dev/null
+++ b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag.typemap
@@ -0,0 +1,14 @@
+# Copyright 2017 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.
+
+mojom = "//services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom"
+public_headers = [ "//net/traffic_annotation/network_traffic_annotation.h" ]
+traits_headers =
+    [ "//services/network/public/cpp/" +
+      "mutable_partial_network_traffic_annotation_tag_struct_traits.h" ]
+deps = [
+  "//net:traffic_annotation",
+]
+type_mappings = [ "network.mojom.MutablePartialNetworkTrafficAnnotationTag=" +
+                  "net::MutablePartialNetworkTrafficAnnotationTag" ]
diff --git a/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits.h b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits.h
new file mode 100644
index 0000000..09a552c
--- /dev/null
+++ b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits.h
@@ -0,0 +1,45 @@
+// Copyright 2017 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 SERVICES_NETWORK_PUBLIC_CPP_MUTABLE_PARTIAL_NETWORK_TRAFFIC_ANNOTATION_TAG_STRUCT_TRAITS_H_
+#define SERVICES_NETWORK_PUBLIC_CPP_MUTABLE_PARTIAL_NETWORK_TRAFFIC_ANNOTATION_TAG_STRUCT_TRAITS_H_
+
+#include "mojo/common/common_custom_types_struct_traits.h"
+#include "net/traffic_annotation/network_traffic_annotation.h"
+#include "services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom.h"
+
+namespace mojo {
+
+template <>
+struct StructTraits<
+    network::mojom::MutablePartialNetworkTrafficAnnotationTagDataView,
+    net::MutablePartialNetworkTrafficAnnotationTag> {
+  static int32_t unique_id_hash_code(
+      const net::MutablePartialNetworkTrafficAnnotationTag&
+          traffic_annotation) {
+    return traffic_annotation.unique_id_hash_code;
+  }
+  static int32_t completing_id_hash_code(
+      const net::MutablePartialNetworkTrafficAnnotationTag&
+          traffic_annotation) {
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+    return traffic_annotation.completing_id_hash_code;
+#else
+    return -1;
+#endif
+  }
+  static bool Read(
+      network::mojom::MutablePartialNetworkTrafficAnnotationTagDataView data,
+      net::MutablePartialNetworkTrafficAnnotationTag* out) {
+    out->unique_id_hash_code = data.unique_id_hash_code();
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+    out->completing_id_hash_code = data.completing_id_hash_code();
+#endif
+    return true;
+  }
+};
+
+}  // namespace mojo
+
+#endif  // SERVICES_NETWORK_PUBLIC_CPP_MUTABLE_PARTIAL_NETWORK_TRAFFIC_ANNOTATION_TAG_STRUCT_TRAITS_H_
diff --git a/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits_unittest.cc b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits_unittest.cc
new file mode 100644
index 0000000..096ed71
--- /dev/null
+++ b/services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits_unittest.cc
@@ -0,0 +1,33 @@
+// Copyright 2017 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 "services/network/public/cpp/mutable_partial_network_traffic_annotation_tag_struct_traits.h"
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
+#include "mojo/public/cpp/bindings/binding_set.h"
+#include "services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace network {
+
+TEST(MutablePartialNetworkTrafficAnnottionTagsTest, BasicTest) {
+  net::MutablePartialNetworkTrafficAnnotationTag original;
+  net::MutablePartialNetworkTrafficAnnotationTag copy;
+
+  original.unique_id_hash_code = 1;
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+  original.completing_id_hash_code = 2;
+#endif
+  EXPECT_TRUE(mojom::MutablePartialNetworkTrafficAnnotationTag::Deserialize(
+      mojom::MutablePartialNetworkTrafficAnnotationTag::Serialize(&original),
+      &copy));
+  EXPECT_EQ(copy.unique_id_hash_code, 1);
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
+  EXPECT_EQ(copy.completing_id_hash_code, 2);
+#endif
+}
+
+}  // namespace network
diff --git a/services/network/public/cpp/typemaps.gni b/services/network/public/cpp/typemaps.gni
index af96688..bde889a 100644
--- a/services/network/public/cpp/typemaps.gni
+++ b/services/network/public/cpp/typemaps.gni
@@ -4,6 +4,7 @@
 
 typemaps = [
   "//services/network/public/cpp/mutable_network_traffic_annotation_tag.typemap",
+  "//services/network/public/cpp/mutable_partial_network_traffic_annotation_tag.typemap",
   "//services/network/public/cpp/cookie_manager.typemap",
   "//services/network/public/cpp/http_request_headers.typemap",
 ]
diff --git a/services/network/public/interfaces/BUILD.gn b/services/network/public/interfaces/BUILD.gn
index 120027f7..e95d43d 100644
--- a/services/network/public/interfaces/BUILD.gn
+++ b/services/network/public/interfaces/BUILD.gn
@@ -12,6 +12,7 @@
     "fetch_api.mojom",
     "http_request_headers.mojom",
     "mutable_network_traffic_annotation_tag.mojom",
+    "mutable_partial_network_traffic_annotation_tag.mojom",
     "network_change_manager.mojom",
     "restricted_cookie_manager.mojom",
   ]
diff --git a/services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom b/services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom
index ad1143a..34ab3a3 100644
--- a/services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom
+++ b/services/network/public/interfaces/mutable_network_traffic_annotation_tag.mojom
@@ -2,6 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+// This interface defines the required structure to serialize a
+// net::MutableNetworkTrafficAnnotationTag. Please refer to
+// '/docs/network_traffic_annotations.md' for more details.
+
 module network.mojom;
 
 struct MutableNetworkTrafficAnnotationTag {
diff --git a/services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom b/services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom
new file mode 100644
index 0000000..265814c
--- /dev/null
+++ b/services/network/public/interfaces/mutable_partial_network_traffic_annotation_tag.mojom
@@ -0,0 +1,14 @@
+// Copyright 2017 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.
+
+// This interface defines the required structure to serialize a
+// net::MutablePartialNetworkTrafficAnnotationTag. Please refer to
+// '/docs/network_traffic_annotations.md' for more details.
+
+module network.mojom;
+
+struct MutablePartialNetworkTrafficAnnotationTag {
+  uint32 unique_id_hash_code;
+  uint32 completing_id_hash_code;
+};