libbrillo: Add functionality to get the app id of a kiosk app.

This CL adds the functionality to libpolicy to return the ID of the
auto-launched kiosk app, if one exists in the device policy.

BUG=chromium:707892
TEST=automated

Change-Id: Ia8fd1836c288be8a12a7b3fbd2e064e71cd118fe
Reviewed-on: https://chromium-review.googlesource.com/486241
Commit-Ready: Rahul Chaturvedi <rkc@chromium.org>
Tested-by: Rahul Chaturvedi <rkc@chromium.org>
Reviewed-by: Rahul Chaturvedi <rkc@chromium.org>
(cherry picked from commit d6d6f31b3988e21d0a5b50977ef7befedf339b55)
diff --git a/PRESUBMIT.cfg b/PRESUBMIT.cfg
new file mode 100644
index 0000000..60456a2
--- /dev/null
+++ b/PRESUBMIT.cfg
@@ -0,0 +1,6 @@
+[Hook Overrides]
+cros_license_check: true
+
+[Hook Overrides Options]
+# policy_all and policy_none files are data files
+cros_license_check: --exclude_regex=\bpolicy_(?:all|none)$
diff --git a/policy/device_policy.h b/policy/device_policy.h
index 840e7e5..fbebeaa 100644
--- a/policy/device_policy.h
+++ b/policy/device_policy.h
@@ -150,6 +150,11 @@
   virtual bool GetUsbDetachableWhitelist(
       std::vector<UsbDeviceId>* usb_whitelist) const = 0;
 
+  // Writes the value of the kiosk app id into |app_id_out|.
+  // Only succeeds if the device is in auto-launched kiosk mode.
+  virtual bool GetAutoLaunchedKioskAppId(
+      std::string* app_id_out) const = 0;
+
  private:
   // Verifies that the policy files are owned by root and exist.
   virtual bool VerifyPolicyFiles() = 0;
diff --git a/policy/device_policy_impl.cc b/policy/device_policy_impl.cc
index 8810de8..9c98b52 100644
--- a/policy/device_policy_impl.cc
+++ b/policy/device_policy_impl.cc
@@ -434,6 +434,41 @@
   return true;
 }
 
+bool DevicePolicyImpl::GetAutoLaunchedKioskAppId(
+    std::string* app_id_out) const {
+  if (!device_policy_.has_device_local_accounts())
+    return false;
+
+  const enterprise_management::DeviceLocalAccountsProto& local_accounts =
+      device_policy_.device_local_accounts();
+
+  // For auto-launched kiosk apps, the delay needs to be 0.
+  if (local_accounts.has_auto_login_delay() &&
+      local_accounts.auto_login_delay() != 0)
+    return false;
+
+  for (const enterprise_management::DeviceLocalAccountInfoProto& account :
+       local_accounts.account()) {
+    // If this isn't an auto-login account, move to the next one.
+    if (account.account_id() != local_accounts.auto_login_id())
+      continue;
+
+    // If the auto-launched account is not a kiosk app, bail out, we aren't
+    // running in auto-launched kiosk mode.
+    if (account.type() !=
+            enterprise_management::DeviceLocalAccountInfoProto::
+                ACCOUNT_TYPE_KIOSK_APP) {
+      return false;
+    }
+
+    *app_id_out = account.kiosk_app().app_id();
+    return true;
+  }
+
+  // No auto-launched account found.
+  return false;
+}
+
 bool DevicePolicyImpl::VerifyPolicyFiles() {
   // Both the policy and its signature have to exist.
   if (!base::PathExists(policy_path_) || !base::PathExists(keyfile_path_)) {
diff --git a/policy/device_policy_impl.h b/policy/device_policy_impl.h
index c4ea545..1d13d74 100644
--- a/policy/device_policy_impl.h
+++ b/policy/device_policy_impl.h
@@ -30,44 +30,47 @@
 class DevicePolicyImpl : public DevicePolicy {
  public:
   DevicePolicyImpl();
-  virtual ~DevicePolicyImpl();
+  ~DevicePolicyImpl() override;
 
-  virtual bool LoadPolicy();
-  virtual bool GetPolicyRefreshRate(int* rate) const;
-  virtual bool GetUserWhitelist(std::vector<std::string>* user_whitelist) const;
-  virtual bool GetGuestModeEnabled(bool* guest_mode_enabled) const;
-  virtual bool GetCameraEnabled(bool* camera_enabled) const;
-  virtual bool GetShowUserNames(bool* show_user_names) const;
-  virtual bool GetDataRoamingEnabled(bool* data_roaming_enabled) const;
-  virtual bool GetAllowNewUsers(bool* allow_new_users) const;
-  virtual bool GetMetricsEnabled(bool* metrics_enabled) const;
-  virtual bool GetReportVersionInfo(bool* report_version_info) const;
-  virtual bool GetReportActivityTimes(bool* report_activity_times) const;
-  virtual bool GetReportBootMode(bool* report_boot_mode) const;
-  virtual bool GetEphemeralUsersEnabled(bool* ephemeral_users_enabled) const;
-  virtual bool GetReleaseChannel(std::string* release_channel) const;
-  virtual bool GetReleaseChannelDelegated(
-      bool* release_channel_delegated) const;
-  virtual bool GetUpdateDisabled(bool* update_disabled) const;
-  virtual bool GetTargetVersionPrefix(
-      std::string* target_version_prefix) const;
-  virtual bool GetScatterFactorInSeconds(
-      int64_t* scatter_factor_in_seconds) const;
-  virtual bool GetAllowedConnectionTypesForUpdate(
-      std::set<std::string>* connection_types) const;
-  virtual bool GetOpenNetworkConfiguration(
-      std::string* open_network_configuration) const;
-  virtual bool GetOwner(std::string* owner) const;
-  virtual bool GetHttpDownloadsEnabled(bool* http_downloads_enabled) const;
-  virtual bool GetAuP2PEnabled(bool* au_p2p_enabled) const;
-  virtual bool GetAllowKioskAppControlChromeVersion(
-      bool* allow_kiosk_app_control_chrome_version) const;
-  virtual bool GetUsbDetachableWhitelist(
-      std::vector<UsbDeviceId>* usb_whitelist) const;
+  bool LoadPolicy() override;
+  bool GetPolicyRefreshRate(int* rate) const override;
+  bool GetUserWhitelist(
+      std::vector<std::string>* user_whitelist) const override;
+  bool GetGuestModeEnabled(bool* guest_mode_enabled) const override;
+  bool GetCameraEnabled(bool* camera_enabled) const override;
+  bool GetShowUserNames(bool* show_user_names) const override;
+  bool GetDataRoamingEnabled(bool* data_roaming_enabled) const override;
+  bool GetAllowNewUsers(bool* allow_new_users) const override;
+  bool GetMetricsEnabled(bool* metrics_enabled) const override;
+  bool GetReportVersionInfo(bool* report_version_info) const override;
+  bool GetReportActivityTimes(bool* report_activity_times) const override;
+  bool GetReportBootMode(bool* report_boot_mode) const override;
+  bool GetEphemeralUsersEnabled(bool* ephemeral_users_enabled) const override;
+  bool GetReleaseChannel(std::string* release_channel) const override;
+  bool GetReleaseChannelDelegated(
+      bool* release_channel_delegated) const override;
+  bool GetUpdateDisabled(bool* update_disabled) const override;
+  bool GetTargetVersionPrefix(
+      std::string* target_version_prefix) const override;
+  bool GetScatterFactorInSeconds(
+      int64_t* scatter_factor_in_seconds) const override;
+  bool GetAllowedConnectionTypesForUpdate(
+      std::set<std::string>* connection_types) const override;
+  bool GetOpenNetworkConfiguration(
+      std::string* open_network_configuration) const override;
+  bool GetOwner(std::string* owner) const override;
+  bool GetHttpDownloadsEnabled(bool* http_downloads_enabled) const override;
+  bool GetAuP2PEnabled(bool* au_p2p_enabled) const override;
+  bool GetAllowKioskAppControlChromeVersion(
+      bool* allow_kiosk_app_control_chrome_version) const override;
+  bool GetUsbDetachableWhitelist(
+      std::vector<UsbDeviceId>* usb_whitelist) const override;
+  bool GetAutoLaunchedKioskAppId(
+      std::string* app_id_out) const override;
 
  protected:
   // Verifies that the policy files are owned by root and exist.
-  virtual bool VerifyPolicyFiles();
+  bool VerifyPolicyFiles() override;
 
   base::FilePath policy_path_;
   base::FilePath keyfile_path_;
@@ -75,7 +78,7 @@
 
  private:
   // Verifies that the policy signature is correct.
-  virtual bool VerifyPolicySignature();
+  bool VerifyPolicySignature() override;
 
   enterprise_management::PolicyFetchResponse policy_;
   enterprise_management::PolicyData policy_data_;
diff --git a/policy/mock_device_policy.h b/policy/mock_device_policy.h
index cabba91..23e8147 100644
--- a/policy/mock_device_policy.h
+++ b/policy/mock_device_policy.h
@@ -96,6 +96,7 @@
                      bool(bool*));  // NOLINT(readability/function)
   MOCK_CONST_METHOD1(GetUsbDetachableWhitelist,
                      bool(std::vector<DevicePolicy::UsbDeviceId>*));
+  MOCK_CONST_METHOD1(GetAutoLaunchedKioskAppId, bool(std::string*));
 
   MOCK_METHOD0(VerifyPolicyFiles, bool(void));
   MOCK_METHOD0(VerifyPolicySignature, bool(void));
diff --git a/policy/tests/libpolicy_unittest.cc b/policy/tests/libpolicy_unittest.cc
index 638d79f..f38c423 100644
--- a/policy/tests/libpolicy_unittest.cc
+++ b/policy/tests/libpolicy_unittest.cc
@@ -172,6 +172,9 @@
   ASSERT_EQ(0x0403, list_device[1].vendor_id);
   ASSERT_EQ(0x6001, list_device[1].product_id);
 
+  ASSERT_TRUE(policy.GetAutoLaunchedKioskAppId(&string_value));
+  ASSERT_EQ("my_kiosk_app", string_value);
+
   // Reloading the protobuf should succeed.
   ASSERT_TRUE(provider.Reload());
 }
diff --git a/policy/tests/whitelist/owner.key b/policy/tests/whitelist/owner.key
index 352c42c..94ef466 100644
--- a/policy/tests/whitelist/owner.key
+++ b/policy/tests/whitelist/owner.key
Binary files differ
diff --git a/policy/tests/whitelist/policy_all b/policy/tests/whitelist/policy_all
index b2b8f10..ff197cd 100644
--- a/policy/tests/whitelist/policy_all
+++ b/policy/tests/whitelist/policy_all
Binary files differ
diff --git a/policy/tests/whitelist/policy_none b/policy/tests/whitelist/policy_none
index 5d1cf1f..7f250f6 100644
--- a/policy/tests/whitelist/policy_none
+++ b/policy/tests/whitelist/policy_none
Binary files differ