pepper_talk_host.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_talk_host.h"
6
7#include "base/bind.h"
8#include "content/public/browser/browser_ppapi_host.h"
9#include "content/public/browser/browser_thread.h"
10#include "content/public/browser/render_view_host.h"
11#include "grit/generated_resources.h"
12#include "ppapi/c/pp_errors.h"
13#include "ppapi/host/host_message_context.h"
14#include "ppapi/host/ppapi_host.h"
15#include "ppapi/proxy/ppapi_messages.h"
16#include "ui/base/l10n/l10n_util.h"
17
18#if defined(USE_ASH)
19#include "ash/shell.h"
20#include "ash/shell_window_ids.h"
21#include "chrome/browser/ui/simple_message_box.h"
22#include "ui/aura/window.h"
23#endif
24
25namespace chrome {
26
27namespace {
28
29ppapi::host::ReplyMessageContext GetPermissionOnUIThread(
30    int render_process_id,
31    int render_view_id,
32    ppapi::host::ReplyMessageContext reply) {
33  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
34  reply.params.set_result(0);
35
36  content::RenderViewHost* render_view_host =
37      content::RenderViewHost::FromID(render_process_id, render_view_id);
38  if (!render_view_host)
39    return reply;  // RVH destroyed while task was pending.
40
41#if defined(USE_ASH)
42  // TODO(brettw). We should not be grabbing the active toplevel window, we
43  // should use the toplevel window associated with the render view.
44  const string16 title = l10n_util::GetStringUTF16(
45      IDS_GTALK_SCREEN_SHARE_DIALOG_TITLE);
46  const string16 message = l10n_util::GetStringUTF16(
47      IDS_GTALK_SCREEN_SHARE_DIALOG_MESSAGE);
48
49  aura::Window* parent = ash::Shell::GetContainer(
50      ash::Shell::GetActiveRootWindow(),
51      ash::internal::kShellWindowId_SystemModalContainer);
52  reply.params.set_result(static_cast<int32_t>(
53      chrome::ShowMessageBox(parent, title, message,
54                             chrome::MESSAGE_BOX_TYPE_QUESTION) ==
55      chrome::MESSAGE_BOX_RESULT_YES));
56#else
57  NOTIMPLEMENTED();
58#endif
59  return reply;
60}
61
62}  // namespace
63
64PepperTalkHost::PepperTalkHost(content::BrowserPpapiHost* host,
65                               PP_Instance instance,
66                               PP_Resource resource)
67    : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource),
68      weak_factory_(this),
69      browser_ppapi_host_(host) {
70}
71
72PepperTalkHost::~PepperTalkHost() {
73}
74
75int32_t PepperTalkHost::OnResourceMessageReceived(
76    const IPC::Message& msg,
77    ppapi::host::HostMessageContext* context) {
78  // We only have one message with no parameters.
79  if (msg.type() != PpapiHostMsg_Talk_GetPermission::ID)
80    return 0;
81
82  int render_process_id = 0;
83  int render_view_id = 0;
84  browser_ppapi_host_->GetRenderViewIDsForInstance(
85      pp_instance(), &render_process_id, &render_view_id);
86
87  content::BrowserThread::PostTaskAndReplyWithResult(
88      content::BrowserThread::UI, FROM_HERE,
89      base::Bind(&GetPermissionOnUIThread, render_process_id, render_view_id,
90                 context->MakeReplyMessageContext()),
91      base::Bind(&PepperTalkHost::GotTalkPermission,
92                 weak_factory_.GetWeakPtr()));
93  return PP_OK_COMPLETIONPENDING;
94}
95
96void PepperTalkHost::GotTalkPermission(
97    ppapi::host::ReplyMessageContext reply) {
98  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
99  host()->SendReply(reply, PpapiPluginMsg_Talk_GetPermissionReply());
100}
101
102}  // namespace chrome
103