Commit 95a8e751 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Create a new extension API returns device id.

The API has been proposed by crbug.com/876800

Bug: 877238
Change-Id: Ic3ad7466d4bff95d8626e8339e13799545bf3f50
Reviewed-on: https://chromium-review.googlesource.com/1187514
Commit-Queue: Owen Min <zmin@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Reviewed-by: default avatarGeorges Khalil <georgesak@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588161}
parent b1c7cfd4
...@@ -37,9 +37,12 @@ namespace enterprise_reporting { ...@@ -37,9 +37,12 @@ namespace enterprise_reporting {
const char kInvalidInputErrorMessage[] = "The report is not valid."; const char kInvalidInputErrorMessage[] = "The report is not valid.";
const char kUploadFailed[] = "Failed to upload the report."; const char kUploadFailed[] = "Failed to upload the report.";
const char kDeviceNotEnrolled[] = "This device has not been enrolled yet."; const char kDeviceNotEnrolled[] = "This device has not been enrolled yet.";
const char kDeviceIdNotFound[] = "Failed to retrieve the device id.";
} // namespace enterprise_reporting } // namespace enterprise_reporting
// UploadDesktopReport
EnterpriseReportingPrivateUploadChromeDesktopReportFunction:: EnterpriseReportingPrivateUploadChromeDesktopReportFunction::
EnterpriseReportingPrivateUploadChromeDesktopReportFunction() EnterpriseReportingPrivateUploadChromeDesktopReportFunction()
: EnterpriseReportingPrivateUploadChromeDesktopReportFunction( : EnterpriseReportingPrivateUploadChromeDesktopReportFunction(
...@@ -139,4 +142,21 @@ void EnterpriseReportingPrivateUploadChromeDesktopReportFunction:: ...@@ -139,4 +142,21 @@ void EnterpriseReportingPrivateUploadChromeDesktopReportFunction::
} }
} }
// GetDeviceId
EnterpriseReportingPrivateGetDeviceIdFunction::
EnterpriseReportingPrivateGetDeviceIdFunction() {}
ExtensionFunction::ResponseAction
EnterpriseReportingPrivateGetDeviceIdFunction::Run() {
std::string client_id =
policy::BrowserDMTokenStorage::Get()->RetrieveClientId();
if (client_id.empty())
return RespondNow(Error(enterprise_reporting::kDeviceIdNotFound));
return RespondNow(OneArgument(std::make_unique<base::Value>(client_id)));
}
EnterpriseReportingPrivateGetDeviceIdFunction::
~EnterpriseReportingPrivateGetDeviceIdFunction() = default;
} // namespace extensions } // namespace extensions
...@@ -22,6 +22,7 @@ namespace enterprise_reporting { ...@@ -22,6 +22,7 @@ namespace enterprise_reporting {
extern const char kInvalidInputErrorMessage[]; extern const char kInvalidInputErrorMessage[];
extern const char kUploadFailed[]; extern const char kUploadFailed[];
extern const char kDeviceNotEnrolled[]; extern const char kDeviceNotEnrolled[];
extern const char kDeviceIdNotFound[];
} // namespace enterprise_reporting } // namespace enterprise_reporting
...@@ -64,6 +65,22 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportFunction ...@@ -64,6 +65,22 @@ class EnterpriseReportingPrivateUploadChromeDesktopReportFunction
EnterpriseReportingPrivateUploadChromeDesktopReportFunction); EnterpriseReportingPrivateUploadChromeDesktopReportFunction);
}; };
class EnterpriseReportingPrivateGetDeviceIdFunction
: public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("enterprise.reportingPrivate.getDeviceId",
ENTERPRISEREPORTINGPRIVATE_GETDEVICEID);
EnterpriseReportingPrivateGetDeviceIdFunction();
// ExtensionFunction
ExtensionFunction::ResponseAction Run() override;
private:
~EnterpriseReportingPrivateGetDeviceIdFunction() override;
DISALLOW_COPY_AND_ASSIGN(EnterpriseReportingPrivateGetDeviceIdFunction);
};
} // namespace extensions } // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_REPORTING_PRIVATE_ENTERPRISE_REPORTING_PRIVATE_API_H_ #endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_REPORTING_PRIVATE_ENTERPRISE_REPORTING_PRIVATE_API_H_
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/extension_api_unittest.h" #include "chrome/browser/extensions/extension_api_unittest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h" #include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/policy/browser_dm_token_storage.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h" #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
...@@ -55,11 +56,32 @@ class MockCloudPolicyClient : public policy::MockCloudPolicyClient { ...@@ -55,11 +56,32 @@ class MockCloudPolicyClient : public policy::MockCloudPolicyClient {
DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyClient); DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyClient);
}; };
class FakeBrowserDMTokenStorage : public policy::BrowserDMTokenStorage {
public:
FakeBrowserDMTokenStorage() = default;
~FakeBrowserDMTokenStorage() override = default;
void SetClientId(const std::string& client_id) { client_id_ = client_id; }
// policy::BrowserDMTokenStorage:
std::string InitClientId() override { return client_id_; }
std::string InitEnrollmentToken() override { return std::string(); }
std::string InitDMToken() override { return std::string(); }
void SaveDMToken(const std::string& token) override {}
private:
std::string client_id_;
DISALLOW_COPY_AND_ASSIGN(FakeBrowserDMTokenStorage);
};
} // namespace } // namespace
class EnterpriseReportingPrivateTest : public ExtensionApiUnittest { // Test for API enterprise.reportingPrivate.uploadChromeDesktopReport
class EnterpriseReportingPrivateUploadChromeDesktopReportTest
: public ExtensionApiUnittest {
public: public:
EnterpriseReportingPrivateTest() EnterpriseReportingPrivateUploadChromeDesktopReportTest()
: test_shared_loader_factory_( : test_shared_loader_factory_(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_)) {} &test_url_loader_factory_)) {}
...@@ -98,24 +120,27 @@ class EnterpriseReportingPrivateTest : public ExtensionApiUnittest { ...@@ -98,24 +120,27 @@ class EnterpriseReportingPrivateTest : public ExtensionApiUnittest {
scoped_refptr<network::WeakWrapperSharedURLLoaderFactory> scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
test_shared_loader_factory_; test_shared_loader_factory_;
DISALLOW_COPY_AND_ASSIGN(EnterpriseReportingPrivateTest); DISALLOW_COPY_AND_ASSIGN(
EnterpriseReportingPrivateUploadChromeDesktopReportTest);
}; };
TEST_F(EnterpriseReportingPrivateTest, DeviceIsNotEnrolled) { TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest,
DeviceIsNotEnrolled) {
ASSERT_EQ(enterprise_reporting::kDeviceNotEnrolled, ASSERT_EQ(enterprise_reporting::kDeviceNotEnrolled,
RunFunctionAndReturnError( RunFunctionAndReturnError(
CreateChromeDesktopReportingFunction(std::string()), CreateChromeDesktopReportingFunction(std::string()),
GenerateArgs(kFakeMachineNameReport))); GenerateArgs(kFakeMachineNameReport)));
} }
TEST_F(EnterpriseReportingPrivateTest, ReportIsNotValid) { TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest,
ReportIsNotValid) {
ASSERT_EQ(enterprise_reporting::kInvalidInputErrorMessage, ASSERT_EQ(enterprise_reporting::kInvalidInputErrorMessage,
RunFunctionAndReturnError( RunFunctionAndReturnError(
CreateChromeDesktopReportingFunction(kFakeDMToken), CreateChromeDesktopReportingFunction(kFakeDMToken),
GenerateInvalidReport())); GenerateInvalidReport()));
} }
TEST_F(EnterpriseReportingPrivateTest, UploadFailed) { TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest, UploadFailed) {
UIThreadExtensionFunction* function = UIThreadExtensionFunction* function =
CreateChromeDesktopReportingFunction(kFakeDMToken); CreateChromeDesktopReportingFunction(kFakeDMToken);
EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _)) EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _))
...@@ -129,7 +154,8 @@ TEST_F(EnterpriseReportingPrivateTest, UploadFailed) { ...@@ -129,7 +154,8 @@ TEST_F(EnterpriseReportingPrivateTest, UploadFailed) {
::testing::Mock::VerifyAndClearExpectations(client_); ::testing::Mock::VerifyAndClearExpectations(client_);
} }
TEST_F(EnterpriseReportingPrivateTest, UploadSucceeded) { TEST_F(EnterpriseReportingPrivateUploadChromeDesktopReportTest,
UploadSucceeded) {
UIThreadExtensionFunction* function = UIThreadExtensionFunction* function =
CreateChromeDesktopReportingFunction(kFakeDMToken); CreateChromeDesktopReportingFunction(kFakeDMToken);
EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _)) EXPECT_CALL(*client_, SetupRegistration(kFakeDMToken, kFakeClientId, _))
...@@ -142,4 +168,40 @@ TEST_F(EnterpriseReportingPrivateTest, UploadSucceeded) { ...@@ -142,4 +168,40 @@ TEST_F(EnterpriseReportingPrivateTest, UploadSucceeded) {
::testing::Mock::VerifyAndClearExpectations(client_); ::testing::Mock::VerifyAndClearExpectations(client_);
} }
// Test for API enterprise.reportingPrivate.getDeviceId
class EnterpriseReportingPrivateGetDeviceIdTest : public ExtensionApiUnittest {
public:
EnterpriseReportingPrivateGetDeviceIdTest() {
policy::BrowserDMTokenStorage::SetForTesting(&storage_);
}
void SetClientId(const std::string& client_id) {
storage_.SetClientId(client_id);
}
private:
FakeBrowserDMTokenStorage storage_;
DISALLOW_COPY_AND_ASSIGN(EnterpriseReportingPrivateGetDeviceIdTest);
};
TEST_F(EnterpriseReportingPrivateGetDeviceIdTest, GetDeviceId) {
auto function =
base::MakeRefCounted<EnterpriseReportingPrivateGetDeviceIdFunction>();
SetClientId(kFakeClientId);
std::unique_ptr<base::Value> id =
RunFunctionAndReturnValue(function.get(), "[]");
ASSERT_TRUE(id);
ASSERT_TRUE(id->is_string());
EXPECT_EQ(kFakeClientId, id->GetString());
}
TEST_F(EnterpriseReportingPrivateGetDeviceIdTest, DeviceIdNotExist) {
auto function =
base::MakeRefCounted<EnterpriseReportingPrivateGetDeviceIdFunction>();
SetClientId("");
ASSERT_EQ(enterprise_reporting::kDeviceIdNotFound,
RunFunctionAndReturnError(function.get(), "[]"));
}
} // namespace extensions } // namespace extensions
...@@ -9,6 +9,8 @@ namespace enterprise.reportingPrivate { ...@@ -9,6 +9,8 @@ namespace enterprise.reportingPrivate {
// finished. // finished.
callback DoneCallback = void(); callback DoneCallback = void();
// Invoked by <code>getDeviceId</code> to return the ID.
callback GetDeviceIdCallback = void(DOMString id);
interface Functions { interface Functions {
// Uploads the status of Chrome browser to the admin console by sending // Uploads the status of Chrome browser to the admin console by sending
...@@ -16,6 +18,10 @@ namespace enterprise.reportingPrivate { ...@@ -16,6 +18,10 @@ namespace enterprise.reportingPrivate {
static void uploadChromeDesktopReport( static void uploadChromeDesktopReport(
object report, object report,
optional DoneCallback callback); optional DoneCallback callback);
// Gets the identity of device that Chrome browser is running on. The ID is
// retrieved from the local device and used by the Google admin console.
static void getDeviceId(optional GetDeviceIdCallback callback);
}; };
}; };
...@@ -1330,6 +1330,7 @@ enum HistogramValue { ...@@ -1330,6 +1330,7 @@ enum HistogramValue {
AUTOTESTPRIVATE_RUNCROSTINIINSTALLER = 1267, AUTOTESTPRIVATE_RUNCROSTINIINSTALLER = 1267,
AUTOFILLPRIVATE_MIGRATECREDITCARDS = 1268, AUTOFILLPRIVATE_MIGRATECREDITCARDS = 1268,
AUTOTESTPRIVATE_ISAPPSHOWN = 1269, AUTOTESTPRIVATE_ISAPPSHOWN = 1269,
ENTERPRISEREPORTINGPRIVATE_GETDEVICEID = 1270,
// Last entry: Add new entries above, then run: // Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py // python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY ENUM_BOUNDARY
......
...@@ -16581,6 +16581,7 @@ Called by update_net_error_codes.py.--> ...@@ -16581,6 +16581,7 @@ Called by update_net_error_codes.py.-->
<int value="1267" label="AUTOTESTPRIVATE_RUNCROSTINIINSTALLER"/> <int value="1267" label="AUTOTESTPRIVATE_RUNCROSTINIINSTALLER"/>
<int value="1268" label="AUTOFILLPRIVATE_MIGRATECREDITCARDS"/> <int value="1268" label="AUTOFILLPRIVATE_MIGRATECREDITCARDS"/>
<int value="1269" label="AUTOTESTPRIVATE_ISAPPSHOWN"/> <int value="1269" label="AUTOTESTPRIVATE_ISAPPSHOWN"/>
<int value="1270" label="ENTERPRISEREPORTINGPRIVATE_GETDEVICEID"/>
</enum> </enum>
<enum name="ExtensionIconState"> <enum name="ExtensionIconState">
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment