1 // Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
5 #include "tests/cefclient/browser/drm_test.h"
10 #include "include/cef_parser.h"
11 #include "include/cef_web_plugin.h"
18 // Application-specific error codes.
19 const int kMessageFormatError = 1;
20 const int kCdmLoadError = 2;
22 const char kTestUrl[] = "http://tests/drm";
23 const char kWidevineCdmPathKey[] = "widevine_cdm_path";
25 // Callback executed once CDM registration is complete.
26 class CdmCallback : public CefRegisterCdmCallback {
28 CdmCallback(CefRefPtr<CefMessageRouterBrowserSide::Callback> callback)
29 : callback_(callback) {
32 void OnCdmRegistrationComplete(cef_cdm_registration_error_t result,
33 const CefString& error_message) OVERRIDE {
34 if (result == CEF_CDM_REGISTRATION_ERROR_NONE)
35 callback_->Success("");
37 callback_->Failure(kCdmLoadError, error_message);
42 CefRefPtr<CefMessageRouterBrowserSide::Callback> callback_;
44 IMPLEMENT_REFCOUNTING(CdmCallback);
45 DISALLOW_COPY_AND_ASSIGN(CdmCallback);
48 // Handle messages in the browser process.
49 class Handler : public CefMessageRouterBrowserSide::Handler {
53 // Called due to cefQuery execution in binding.html.
54 virtual bool OnQuery(CefRefPtr<CefBrowser> browser,
55 CefRefPtr<CefFrame> frame,
57 const CefString& request,
59 CefRefPtr<Callback> callback) OVERRIDE {
60 // Only handle messages from the test URL.
61 const std::string& url = frame->GetURL();
62 if (url.find(kTestUrl) != 0)
65 // Parse |request| as a JSON dictionary.
66 CefRefPtr<CefDictionaryValue> request_dict = ParseJSON(request);
68 callback->Failure(kMessageFormatError, "Incorrect message format");
72 // Verify the "widevine_cdm_path" key.
73 if (!VerifyKey(request_dict, kWidevineCdmPathKey, VTYPE_STRING, callback))
76 const std::string& widevine_cdm_path =
77 request_dict->GetString(kWidevineCdmPathKey);
78 if (widevine_cdm_path.empty()) {
79 callback->Failure(kMessageFormatError, "Empty widevine CDM path");
83 // Register the Widvine CDM.
84 CefRegisterWidevineCdm(widevine_cdm_path, new CdmCallback(callback));
89 // Convert a JSON string to a dictionary value.
90 static CefRefPtr<CefDictionaryValue> ParseJSON(const CefString& string) {
91 CefRefPtr<CefValue> value = CefParseJSON(string, JSON_PARSER_RFC);
92 if (value.get() && value->GetType() == VTYPE_DICTIONARY)
93 return value->GetDictionary();
97 // Verify that |key| exists in |dictionary| and has type |value_type|. Fails
98 // |callback| and returns false on failure.
99 static bool VerifyKey(CefRefPtr<CefDictionaryValue> dictionary,
101 cef_value_type_t value_type,
102 CefRefPtr<Callback> callback) {
103 if (!dictionary->HasKey(key) || dictionary->GetType(key) != value_type) {
104 callback->Failure(kMessageFormatError,
105 "Missing or incorrectly formatted message key: " +
115 void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) {
116 handlers.insert(new Handler());
119 } // namespace drm_test
120 } // namespace client