Remove dependency to libmediadrm from drm HAL

drm HAL has been using libmediadrm for android::SharedLibrary class and
android::PluginLoader class. This is a Treble violation since
libmediadrm is a framework-only library that is not available to
vendors.

To solve the problem, the two classes are copied into this directory
to form a small static library android.hardware.drm@1.0-helper.a.

Bug: 38302533
Test: mm under /hardware/interfaces/drm/1.0/default and
/hardware/interfaces/drm/1.0/vts/functional

Change-Id: I7b4e5cdb3bc815e971e0c3b7ec99ea86042e13eb
diff --git a/drm/1.0/default/Android.bp b/drm/1.0/default/Android.bp
new file mode 100644
index 0000000..6b1db56
--- /dev/null
+++ b/drm/1.0/default/Android.bp
@@ -0,0 +1,23 @@
+cc_library_static {
+    name: "android.hardware.drm@1.0-helper",
+    vendor: true,
+    defaults: ["hidl_defaults"],
+    srcs: [
+        "SharedLibrary.cpp",
+    ],
+    cflags: [
+        "-Werror",
+        "-Wextra",
+        "-Wall",
+    ],
+    shared_libs: [
+        "liblog",
+    ],
+    header_libs: [
+        "libutils_headers",
+    ],
+    export_header_lib_headers: [
+        "libutils_headers",
+    ],
+    export_include_dirs : ["include"]
+}
diff --git a/drm/1.0/default/Android.mk b/drm/1.0/default/Android.mk
index 7084d1b..7916fba 100644
--- a/drm/1.0/default/Android.mk
+++ b/drm/1.0/default/Android.mk
@@ -36,6 +36,9 @@
   libutils \
   libbinder \
 
+LOCAL_STATIC_LIBRARIES := \
+  android.hardware.drm@1.0-helper \
+
 LOCAL_C_INCLUDES := \
   hardware/interfaces/drm
 
@@ -71,10 +74,12 @@
     libhidlmemory \
     libhidltransport \
     liblog \
-    libmediadrm \
     libstagefright_foundation_vendor \
     libutils \
 
+LOCAL_STATIC_LIBRARIES := \
+    android.hardware.drm@1.0-helper \
+
 LOCAL_C_INCLUDES := \
     frameworks/native/include \
     frameworks/av/include
diff --git a/drm/1.0/default/CryptoFactory.h b/drm/1.0/default/CryptoFactory.h
index 412b557..6b1d1ff 100644
--- a/drm/1.0/default/CryptoFactory.h
+++ b/drm/1.0/default/CryptoFactory.h
@@ -19,7 +19,7 @@
 #include <android/hardware/drm/1.0/ICryptoFactory.h>
 #include <hidl/Status.h>
 #include <media/hardware/CryptoAPI.h>
-#include <media/PluginLoader.h>
+#include <PluginLoader.h>
 #include <media/SharedLibrary.h>
 
 namespace android {
@@ -28,6 +28,7 @@
 namespace V1_0 {
 namespace implementation {
 
+using ::android::hardware::drm::V1_0::helper::PluginLoader;
 using ::android::hardware::drm::V1_0::ICryptoFactory;
 using ::android::hardware::drm::V1_0::ICryptoPlugin;
 using ::android::hardware::hidl_array;
@@ -51,7 +52,7 @@
             override;
 
 private:
-    android::PluginLoader<android::CryptoFactory> loader;
+    PluginLoader<android::CryptoFactory> loader;
 
     CryptoFactory(const CryptoFactory &) = delete;
     void operator=(const CryptoFactory &) = delete;
diff --git a/drm/1.0/default/DrmFactory.h b/drm/1.0/default/DrmFactory.h
index a008844..726bf97 100644
--- a/drm/1.0/default/DrmFactory.h
+++ b/drm/1.0/default/DrmFactory.h
@@ -19,7 +19,7 @@
 #include <android/hardware/drm/1.0/IDrmFactory.h>
 #include <hidl/Status.h>
 #include <media/drm/DrmAPI.h>
-#include <media/PluginLoader.h>
+#include <PluginLoader.h>
 #include <media/SharedLibrary.h>
 
 namespace android {
@@ -28,6 +28,7 @@
 namespace V1_0 {
 namespace implementation {
 
+using ::android::hardware::drm::V1_0::helper::PluginLoader;
 using ::android::hardware::drm::V1_0::IDrmFactory;
 using ::android::hardware::drm::V1_0::IDrmPlugin;
 using ::android::hardware::hidl_array;
@@ -53,7 +54,7 @@
             const hidl_string& appPackageName, createPlugin_cb _hidl_cb) override;
 
 private:
-    android::PluginLoader<android::DrmFactory> loader;
+    PluginLoader<android::DrmFactory> loader;
 
     DrmFactory(const DrmFactory &) = delete;
     void operator=(const DrmFactory &) = delete;
diff --git a/drm/1.0/default/SharedLibrary.cpp b/drm/1.0/default/SharedLibrary.cpp
new file mode 100644
index 0000000..0a942cd
--- /dev/null
+++ b/drm/1.0/default/SharedLibrary.cpp
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "SharedLibrary.h"
+
+#include <dlfcn.h>
+
+namespace android {
+namespace hardware {
+namespace drm {
+namespace V1_0 {
+namespace helper {
+
+SharedLibrary::SharedLibrary(const String8& path) {
+    mLibHandle = dlopen(path.string(), RTLD_NOW);
+}
+
+SharedLibrary::~SharedLibrary() {
+    if (mLibHandle != NULL) {
+        dlclose(mLibHandle);
+        mLibHandle = NULL;
+    }
+}
+
+bool SharedLibrary::operator!() const {
+    return mLibHandle == NULL;
+}
+
+void* SharedLibrary::lookup(const char* symbol) const {
+    if (!mLibHandle) {
+        return NULL;
+    }
+
+    // Clear last error before we load the symbol again,
+    // in case the caller didn't retrieve it.
+    (void)dlerror();
+    return dlsym(mLibHandle, symbol);
+}
+
+const char* SharedLibrary::lastError() const {
+    const char* error = dlerror();
+    return error ? error : "No errors or unknown error";
+}
+
+}
+}
+}
+}
+} // namespace android
diff --git a/drm/1.0/default/include/PluginLoader.h b/drm/1.0/default/include/PluginLoader.h
new file mode 100644
index 0000000..f387b3c
--- /dev/null
+++ b/drm/1.0/default/include/PluginLoader.h
@@ -0,0 +1,107 @@
+/**
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PLUGIN_LOADER_H_
+#define PLUGIN_LOADER_H_
+
+#include "SharedLibrary.h"
+#include <utils/Log.h>
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+namespace android {
+namespace hardware {
+namespace drm {
+namespace V1_0 {
+namespace helper {
+
+template <class T>
+class PluginLoader {
+
+  public:
+    PluginLoader(const char *dir, const char *entry) {
+        /**
+         * scan all plugins in the plugin directory and add them to the
+         * factories list.
+         */
+        String8 pluginDir(dir);
+
+        DIR* pDir = opendir(pluginDir.string());
+        if (pDir == NULL) {
+            ALOGE("Failed to find plugin directory %s", pluginDir.string());
+        } else {
+            struct dirent* pEntry;
+            while ((pEntry = readdir(pDir))) {
+                String8 file(pEntry->d_name);
+                if (file.getPathExtension() == ".so") {
+                    String8 path = pluginDir + "/" + pEntry->d_name;
+                    T *plugin = loadOne(path, entry);
+                    if (plugin) {
+                        factories.push(plugin);
+                    }
+                }
+            }
+            closedir(pDir);
+        }
+    }
+
+    ~PluginLoader() {
+        for (size_t i = 0; i < factories.size(); i++) {
+            delete factories[i];
+        }
+    }
+
+    T *getFactory(size_t i) const {
+        return factories[i];
+    }
+
+    size_t factoryCount() const {return factories.size();}
+
+  private:
+    T* loadOne(const char *path, const char *entry) {
+        sp<SharedLibrary> library = new SharedLibrary(String8(path));
+        if (!library.get()) {
+            ALOGE("Failed to open plugin library %s: %s", path,
+                    library->lastError());
+        } else {
+            typedef T *(*CreateFactoryFunc)();
+            CreateFactoryFunc createFactoryFunc =
+                    (CreateFactoryFunc)library->lookup(entry);
+            if (createFactoryFunc) {
+                ALOGV("Found plugin factory entry %s in %s", entry, path);
+                libraries.push(library);
+                T* result = createFactoryFunc();
+                return  result;
+           }
+        }
+        return NULL;
+    }
+
+    Vector<T *> factories;
+    Vector<sp<SharedLibrary> > libraries;
+
+    PluginLoader(const PluginLoader &) = delete;
+    void operator=(const PluginLoader &) = delete;
+};
+
+}
+}
+}
+}
+} // namespace android
+
+#endif // PLUGIN_LOADER_H_
+
diff --git a/drm/1.0/default/include/SharedLibrary.h b/drm/1.0/default/include/SharedLibrary.h
new file mode 100644
index 0000000..8e174d0
--- /dev/null
+++ b/drm/1.0/default/include/SharedLibrary.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SHARED_LIBRARY_H_
+#define SHARED_LIBRARY_H_
+
+#include <utils/RefBase.h>
+#include <utils/String8.h>
+
+namespace android {
+namespace hardware {
+namespace drm {
+namespace V1_0 {
+namespace helper {
+
+class SharedLibrary : public RefBase {
+   public:
+    explicit SharedLibrary(const String8& path);
+    ~SharedLibrary();
+
+    bool operator!() const;
+    void* lookup(const char* symbol) const;
+    const char* lastError() const;
+
+   private:
+    void* mLibHandle;
+
+    SharedLibrary(const SharedLibrary&) = delete;
+    void operator=(const SharedLibrary&) = delete;
+};
+
+}
+}
+}
+}
+}
+
+#endif  // SHARED_LIBRARY_H_