Add USB Client policy 05/155105/3
authorLee Sung Jun <sjun221.lee@samsung.com>
Thu, 12 Oct 2017 05:48:10 +0000 (14:48 +0900)
committerLee Sung Jun <sjun221.lee@samsung.com>
Mon, 16 Oct 2017 06:54:27 +0000 (15:54 +0900)
Change-Id: Ia3ed1e51897872bfb823f8525e70c24a62fcba3b
Signed-off-by: Lee Sung Jun <sjun221.lee@samsung.com>
plugin/usb.cpp

index db053ee0f564fc887548c1a09cea4217db77dc86..d2a4ab35d41af3fcbf2c08c65a0b57bd0b2b65f2 100644 (file)
 #include <dpm/pil/policy-model.h>
 #include <dpm/pil/policy-storage.h>
 #include <dpm/pil/policy-event.h>
+#include <dpm/pil/app-bundle.h>
+#include <dpm/pil/launchpad.h>
+
+#include <klay/auth/user.h>
+#include <klay/dbus/variant.h>
+#include <klay/dbus/connection.h>
+
+#include <tzplatform_config.h>
+
+#define DEVICED_INTERFACE_USB "org.tizen.system.deviced.Usb"
+#define DEVICED_PATH_USB "/Org/Tizen/System/DeviceD/Usb"
+#define SIGNAL_STATE_CHANGED "StateChanged"
 
 class DebuggingMode : public GlobalPolicy<DataSetInt> {
 public:
@@ -51,19 +63,46 @@ public:
        }
 };
 
+class Client : public GlobalPolicy<DataSetInt> {
+public:
+       Client() : GlobalPolicy("usb-client")
+       {
+               PolicyEventNotifier::create("usb-client");
+       }
+
+       bool apply(const DataType& value)
+       {
+               int enable = value;
+               PolicyEventNotifier::emit("usb-client", enable ? "allowed" : "disallowed");
+               return true;
+       }
+};
+
 class Usb : public AbstractPolicyProvider {
 public:
+       Usb();
+
        int setDebuggingState(bool enable);
        bool getDebuggingState();
 
        int setTetheringState(bool enable);
        bool getTetheringState();
 
+       int setClientState(bool enable);
+       bool getClientState();
+
+       void addDbusSignalHandler(void);
+
 private:
        DebuggingMode debugging;
        Tethering tethering;
+       Client client;
 };
 
+Usb::Usb()
+{
+       addDbusSignalHandler();
+}
 
 int Usb::setDebuggingState(bool enable)
 {
@@ -99,6 +138,48 @@ bool Usb::getTetheringState()
        return tethering.get();
 }
 
+int Usb::setClientState(bool enable)
+{
+       try {
+               client.set(enable);
+       } catch (runtime::Exception& e) {
+               ERROR(e.what());
+               return -1;
+       }
+
+       return 0;
+}
+
+bool Usb::getClientState()
+{
+       return client.get();
+}
+
+void Usb::addDbusSignalHandler(void)
+{
+       dbus::Connection &systemDBus = dbus::Connection::getSystem();
+
+       auto onUSBConnectionStateChanged = [this](dbus::Variant parameters) {
+               unsigned int intparam;
+
+               parameters.get("(u)", &intparam);
+
+               if (intparam == 1 && client.get().value == 0) {
+                       try {
+                               Bundle bundle;
+                               bundle.add("id", "usb-client");
+
+                               runtime::User defaultUser(::tzplatform_getenv(TZ_SYS_DEFAULT_USER));
+                               Launchpad launchpad(defaultUser.getUid());
+                               launchpad.launch("org.tizen.dpm-syspopup", bundle);
+                       } catch(runtime::Exception& e) {
+                               ERROR(e.what());
+                       }
+               }
+       };
+
+       systemDBus.subscribeSignal("", DEVICED_PATH_USB, DEVICED_INTERFACE_USB, SIGNAL_STATE_CHANGED, onUSBConnectionStateChanged);
+}
 
 extern "C" {
 
@@ -111,9 +192,11 @@ AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context)
 
        context.expose(policy, PRIVILEGE_DEBUGGING, (int)(Usb::setDebuggingState)(bool));
        context.expose(policy, PRIVILEGE_USB, (int)(Usb::setTetheringState)(bool));
+       context.expose(policy, PRIVILEGE_USB, (int)(Usb::setClientState)(bool));
 
        context.expose(policy, "", (bool)(Usb::getDebuggingState)());
        context.expose(policy, "", (bool)(Usb::getTetheringState)());
+       context.expose(policy, "", (bool)(Usb::getClientState)());
 
        return policy;
 }