Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / renderer_host / pepper / pepper_platform_verification_message_filter.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/renderer_host/pepper/pepper_platform_verification_message_filter.h"
6
7 #include "base/bind_helpers.h"
8 #include "content/public/browser/browser_ppapi_host.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/web_contents.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17
18 using chromeos::attestation::PlatformVerificationFlow;
19
20 namespace chrome {
21
22 PepperPlatformVerificationMessageFilter::
23     PepperPlatformVerificationMessageFilter(content::BrowserPpapiHost* host,
24                                             PP_Instance instance)
25     : render_process_id_(0), render_frame_id_(0) {
26   host->GetRenderFrameIDsForInstance(
27       instance, &render_process_id_, &render_frame_id_);
28 }
29
30 PepperPlatformVerificationMessageFilter::
31     ~PepperPlatformVerificationMessageFilter() {}
32
33 scoped_refptr<base::TaskRunner>
34 PepperPlatformVerificationMessageFilter::OverrideTaskRunnerForMessage(
35     const IPC::Message& msg) {
36   return content::BrowserThread::GetMessageLoopProxyForThread(
37       content::BrowserThread::UI);
38 }
39
40 int32_t PepperPlatformVerificationMessageFilter::OnResourceMessageReceived(
41     const IPC::Message& msg,
42     ppapi::host::HostMessageContext* context) {
43   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44
45   IPC_BEGIN_MESSAGE_MAP(PepperPlatformVerificationMessageFilter, msg)
46   PPAPI_DISPATCH_HOST_RESOURCE_CALL(
47       PpapiHostMsg_PlatformVerification_ChallengePlatform, OnChallengePlatform)
48   IPC_END_MESSAGE_MAP()
49
50   return PP_ERROR_FAILED;
51 }
52
53 int32_t PepperPlatformVerificationMessageFilter::OnChallengePlatform(
54     ppapi::host::HostMessageContext* context,
55     const std::string& service_id,
56     const std::vector<uint8_t>& challenge) {
57   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
58
59   // Ensure the RenderFrameHost is still alive.
60   content::RenderFrameHost* rfh =
61       content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
62   if (!rfh) {
63     ppapi::host::ReplyMessageContext reply_context =
64         context->MakeReplyMessageContext();
65     reply_context.params.set_result(PP_ERROR_FAILED);
66     SendReply(
67         reply_context,
68         PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
69             std::vector<uint8_t>(), std::vector<uint8_t>(), std::string()));
70     return PP_OK_COMPLETIONPENDING;
71   }
72
73   if (!pv_)
74     pv_ = new PlatformVerificationFlow();
75
76   pv_->ChallengePlatformKey(
77       content::WebContents::FromRenderFrameHost(rfh),
78       service_id,
79       std::string(challenge.begin(), challenge.end()),
80       base::Bind(
81           &PepperPlatformVerificationMessageFilter::ChallengePlatformCallback,
82           this,
83           context->MakeReplyMessageContext()));
84
85   return PP_OK_COMPLETIONPENDING;
86 }
87
88 void PepperPlatformVerificationMessageFilter::ChallengePlatformCallback(
89     ppapi::host::ReplyMessageContext reply_context,
90     chromeos::attestation::PlatformVerificationFlow::Result challenge_result,
91     const std::string& signed_data,
92     const std::string& signature,
93     const std::string& platform_key_certificate) {
94   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
95
96   if (challenge_result == PlatformVerificationFlow::SUCCESS) {
97     reply_context.params.set_result(PP_OK);
98   } else {
99     reply_context.params.set_result(PP_ERROR_FAILED);
100     DCHECK_EQ(signed_data.size(), 0u);
101     DCHECK_EQ(signature.size(), 0u);
102     DCHECK_EQ(platform_key_certificate.size(), 0u);
103   }
104
105   SendReply(reply_context,
106             PpapiHostMsg_PlatformVerification_ChallengePlatformReply(
107                 std::vector<uint8_t>(signed_data.begin(), signed_data.end()),
108                 std::vector<uint8_t>(signature.begin(), signature.end()),
109                 platform_key_certificate));
110 }
111
112 }  // namespace chrome