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/browser/renderer_host/pepper/pepper_flash_device_id_host.h"
6
7#include "base/bind.h"
8#include "base/compiler_specific.h"
9#include "base/logging.h"
10#include "content/public/browser/browser_ppapi_host.h"
11#include "ppapi/c/pp_errors.h"
12#include "ppapi/host/dispatch_host_message.h"
13#include "ppapi/host/host_message_context.h"
14#include "ppapi/host/ppapi_host.h"
15#include "ppapi/proxy/ppapi_messages.h"
16
17using content::BrowserPpapiHost;
18
19namespace chrome {
20
21PepperFlashDeviceIDHost::PepperFlashDeviceIDHost(BrowserPpapiHost* host,
22                                                 PP_Instance instance,
23                                                 PP_Resource resource)
24    : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource),
25      weak_factory_(this){
26  int render_process_id, unused;
27  host->GetRenderViewIDsForInstance(instance, &render_process_id, &unused);
28  fetcher_ = new DeviceIDFetcher(render_process_id);
29}
30
31PepperFlashDeviceIDHost::~PepperFlashDeviceIDHost() {
32}
33
34int32_t PepperFlashDeviceIDHost::OnResourceMessageReceived(
35    const IPC::Message& msg,
36    ppapi::host::HostMessageContext* context) {
37  IPC_BEGIN_MESSAGE_MAP(PepperFlashDeviceIDHost, msg)
38    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FlashDeviceID_GetDeviceID,
39                                        OnHostMsgGetDeviceID)
40  IPC_END_MESSAGE_MAP()
41  return PP_ERROR_FAILED;
42}
43
44int32_t PepperFlashDeviceIDHost::OnHostMsgGetDeviceID(
45    const ppapi::host::HostMessageContext* context) {
46  if (!fetcher_->Start(base::Bind(&PepperFlashDeviceIDHost::GotDeviceID,
47                                  weak_factory_.GetWeakPtr(),
48                                  context->MakeReplyMessageContext()))) {
49    return PP_ERROR_INPROGRESS;
50  }
51  return PP_OK_COMPLETIONPENDING;
52}
53
54void PepperFlashDeviceIDHost::GotDeviceID(
55    ppapi::host::ReplyMessageContext reply_context,
56    const std::string& id) {
57  reply_context.params.set_result(
58      id.empty() ? PP_ERROR_FAILED : PP_OK);
59  host()->SendReply(reply_context,
60                    PpapiPluginMsg_FlashDeviceID_GetDeviceIDReply(id));
61}
62
63}  // namespace chrome
64