1// Copyright (c) 2012 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/renderer/pepper/pepper_flash_drm_renderer_host.h"
6
7#include "base/files/file_path.h"
8#include "content/public/renderer/pepper_plugin_instance.h"
9#include "content/public/renderer/renderer_ppapi_host.h"
10#include "ppapi/c/pp_errors.h"
11#include "ppapi/host/dispatch_host_message.h"
12#include "ppapi/host/host_message_context.h"
13#include "ppapi/host/ppapi_host.h"
14#include "ppapi/proxy/ppapi_messages.h"
15
16// TODO(raymes): This is duplicated from pepper_flash_drm_host.cc but once
17// FileRef is refactored to the browser, it won't need to be.
18namespace {
19const char kVoucherFilename[] = "plugin.vch";
20}  // namespace
21
22PepperFlashDRMRendererHost::PepperFlashDRMRendererHost(
23    content::RendererPpapiHost* host,
24    PP_Instance instance,
25    PP_Resource resource)
26    : ResourceHost(host->GetPpapiHost(), instance, resource),
27      renderer_ppapi_host_(host),
28      weak_factory_(this) {}
29
30PepperFlashDRMRendererHost::~PepperFlashDRMRendererHost() {}
31
32int32_t PepperFlashDRMRendererHost::OnResourceMessageReceived(
33    const IPC::Message& msg,
34    ppapi::host::HostMessageContext* context) {
35  PPAPI_BEGIN_MESSAGE_MAP(PepperFlashDRMRendererHost, msg)
36    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FlashDRM_GetVoucherFile,
37                                        OnGetVoucherFile)
38  PPAPI_END_MESSAGE_MAP()
39  return PP_ERROR_FAILED;
40}
41
42int32_t PepperFlashDRMRendererHost::OnGetVoucherFile(
43    ppapi::host::HostMessageContext* context) {
44  content::PepperPluginInstance* plugin_instance =
45      renderer_ppapi_host_->GetPluginInstance(pp_instance());
46  if (!plugin_instance)
47    return PP_ERROR_FAILED;
48
49  base::FilePath plugin_dir = plugin_instance->GetModulePath().DirName();
50  DCHECK(!plugin_dir.empty());
51  base::FilePath voucher_file = plugin_dir.AppendASCII(kVoucherFilename);
52
53  int renderer_pending_host_id =
54      plugin_instance->MakePendingFileRefRendererHost(voucher_file);
55  if (renderer_pending_host_id == 0)
56    return PP_ERROR_FAILED;
57
58  std::vector<IPC::Message> create_msgs;
59  create_msgs.push_back(PpapiHostMsg_FileRef_CreateForRawFS(voucher_file));
60
61  renderer_ppapi_host_->CreateBrowserResourceHosts(
62      pp_instance(),
63      create_msgs,
64      base::Bind(&PepperFlashDRMRendererHost::DidCreateFileRefHosts,
65                 weak_factory_.GetWeakPtr(),
66                 context->MakeReplyMessageContext(),
67                 voucher_file,
68                 renderer_pending_host_id));
69  return PP_OK_COMPLETIONPENDING;
70}
71
72void PepperFlashDRMRendererHost::DidCreateFileRefHosts(
73    const ppapi::host::ReplyMessageContext& reply_context,
74    const base::FilePath& external_path,
75    int renderer_pending_host_id,
76    const std::vector<int>& browser_pending_host_ids) {
77  DCHECK_EQ(1U, browser_pending_host_ids.size());
78  int browser_pending_host_id = browser_pending_host_ids[0];
79
80  ppapi::FileRefCreateInfo create_info =
81      ppapi::MakeExternalFileRefCreateInfo(external_path,
82                                           std::string(),
83                                           browser_pending_host_id,
84                                           renderer_pending_host_id);
85  host()->SendReply(reply_context,
86                    PpapiPluginMsg_FlashDRM_GetVoucherFileReply(create_info));
87}
88