# Copyright 2010-2021, 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.

# Visibility:  please choose a more appropriate default for the package,
# and update any rules that should be different.

load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load(
    "//:build_defs.bzl",
    "mozc_cc_library",
    "mozc_cc_test",
    "mozc_select",
)

package(default_visibility = ["//visibility:private"])

mozc_cc_library(
    name = "ipc",
    srcs = [
        "ipc.cc",
        "mach_ipc.cc",
        "unix_ipc.cc",
        "win32_ipc.cc",
    ],
    hdrs = ["ipc.h"],
    visibility = [
        "//client:__pkg__",
        "//gui:__subpackages__",
        "//mac:__pkg__",
        "//renderer:__subpackages__",
        "//session:__pkg__",
        "//win32:__subpackages__",
    ],
    deps = [
        ":ipc_path_manager",
        "//base:const",
        "//base:cpu_stats",
        "//base:file_util",
        "//base:singleton",
        "//base:system_util",
        "//base:thread",
        "//base:util",
        "//base:vlog",
        "//base/strings:zstring_view",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
    ] + mozc_select(
        ios = ["//base/mac:mac_util"],
        macos = ["//base/mac:mac_util"],
        windows = [
            "//base/win32:win_sandbox",
            "//base/win32:wide_char",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

proto_library(
    name = "ipc_proto",
    srcs = ["ipc.proto"],
)

cc_proto_library(
    name = "ipc_cc_proto",
    deps = [":ipc_proto"],
)

mozc_cc_library(
    name = "ipc_path_manager",
    srcs = ["ipc_path_manager.cc"],
    hdrs = ["ipc_path_manager.h"],
    deps = [
        ":ipc_cc_proto",
        ":ipc_hdr",
        "//base:const",
        "//base:file_stream",
        "//base:file_util",
        "//base:obfuscator_support",
        "//base:port",
        "//base:process_mutex",
        "//base:random",
        "//base:singleton",
        "//base:system_util",
        "//base:util",
        "//base:version",
        "//base:vlog",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
    ] + mozc_select(
        ios = ["//base/mac:mac_util"],
        macos = ["//base/mac:mac_util"],
        windows = [
            "//base/win32:wide_char",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

mozc_cc_test(
    name = "ipc_path_manager_test",
    size = "small",
    srcs = ["ipc_path_manager_test.cc"],
    deps = [
        ":ipc",
        ":ipc_cc_proto",
        ":ipc_path_manager",
        "//base:file_util",
        "//base:port",
        "//base:process_mutex",
        "//base:system_util",
        "//base:thread",
        "//base:version",
        "//testing:gunit_main",
        "//testing:mozctest",
        "//testing:test_peer",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "ipc_mock",
    testonly = True,
    srcs = ["ipc_mock.cc"],
    hdrs = ["ipc_mock.h"],
    visibility = [
        "//client:__pkg__",
        "//win32/base:__pkg__",
    ],
    deps = [
        ":ipc",
        "//base:version",
        "//base/strings:zstring_view",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "ipc_test_util",
    srcs = ["ipc_test_util.cc"],
    hdrs = ["ipc_test_util.h"],
    visibility = ["//renderer:__subpackages__"],
    deps = [
        ":ipc",
        "//base/strings:zstring_view",
        "@com_google_absl//absl/log:check",
    ],
)

mozc_cc_test(
    name = "ipc_test",
    size = "small",
    srcs = ["ipc_test.cc"],
    copts = ["$(STACK_FRAME_UNLIMITED)"],  # ipc_test.cc
    deps = [
        ":ipc",
        ":ipc_test_util",
        "//base:thread",
        "//testing:gunit_main",
        "//testing:mozctest",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "named_event",
    srcs = ["named_event.cc"],
    hdrs = ["named_event.h"],
    visibility = [
        "//client:__pkg__",
        "//renderer:__subpackages__",
        "//session:__pkg__",
    ],
    deps = [
        "//base:const",
        "//base:hash",
        "//base:system_util",
        "//base:vlog",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
    ] + mozc_select(
        windows = [
            "//base/win32:win_sandbox",
            "//base/win32:wide_char",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

mozc_cc_test(
    name = "named_event_test",
    size = "small",
    srcs = ["named_event_test.cc"],
    deps = [
        ":named_event",
        "//base:clock",
        "//base:thread",
        "//testing:gunit_main",
        "//testing:mozctest",
        "@com_google_absl//absl/time",
    ],
)

mozc_cc_library(
    name = "process_watch_dog",
    srcs = ["process_watch_dog.cc"],
    hdrs = ["process_watch_dog.h"],
    visibility = ["//renderer:__subpackages__"],
    deps = [
        "//base:port",
        "//base:thread",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
    ] + mozc_select(
        windows = [
            "//base:vlog",
            "//base/win32:hresult",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

# This header-only library was inserted because the target
#   //ipc:ipc_path_manager, which includes ipc/ipc.h
# cannot depend on the library
#   //ipc:ipc: Found an inverse include path.
# See http://go/cxx-layering-faq
mozc_cc_library(
    name = "ipc_hdr",
    textual_hdrs = ["ipc.h"],
    deps = [
        "//base:thread",
        "//base/strings:zstring_view",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
    ] + mozc_select(
        windows = [
            "//base/win32:wide_char",
            "@com_microsoft_wil//:wil",
        ],
    ),
)

mozc_cc_test(
    name = "process_watch_dog_test",
    size = "small",
    srcs = ["process_watch_dog_test.cc"],
    deps = [
        ":process_watch_dog",
        "//base:clock",
        "//testing:gunit_main",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/time",
    ],
)

proto_library(
    name = "window_info_proto",
    srcs = ["window_info.proto"],
)

cc_proto_library(
    name = "window_info_cc_proto",
    visibility = ["//gui:__subpackages__"],
    deps = [":window_info_proto"],
)
