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 "ppapi/proxy/dispatcher.h"
6
7#include <string.h>  // For memset.
8
9#include <map>
10
11#include "base/compiler_specific.h"
12#include "base/logging.h"
13#include "base/memory/singleton.h"
14#include "ppapi/proxy/ppapi_messages.h"
15#include "ppapi/proxy/var_serialization_rules.h"
16
17namespace ppapi {
18namespace proxy {
19
20Dispatcher::Dispatcher(PP_GetInterface_Func local_get_interface,
21                       const PpapiPermissions& permissions)
22    : local_get_interface_(local_get_interface),
23      permissions_(permissions) {
24}
25
26Dispatcher::~Dispatcher() {
27}
28
29void Dispatcher::AddFilter(IPC::Listener* listener) {
30  filters_.push_back(listener);
31}
32
33InterfaceProxy* Dispatcher::GetInterfaceProxy(ApiID id) {
34  InterfaceProxy* proxy = proxies_[id].get();
35  if (!proxy) {
36    // Handle the first time for a given API by creating the proxy for it.
37    InterfaceProxy::Factory factory =
38        InterfaceList::GetInstance()->GetFactoryForID(id);
39    if (!factory) {
40      NOTREACHED();
41      return NULL;
42    }
43    proxy = factory(this);
44    DCHECK(proxy);
45    proxies_[id].reset(proxy);
46  }
47  return proxy;
48}
49
50base::MessageLoopProxy* Dispatcher::GetIPCMessageLoop() {
51  return delegate()->GetIPCMessageLoop();
52}
53
54void Dispatcher::AddIOThreadMessageFilter(
55    IPC::ChannelProxy::MessageFilter* filter) {
56  // Our filter is refcounted. The channel will call the destruct method on the
57  // filter when the channel is done with it, so the corresponding Release()
58  // happens there.
59  channel()->AddFilter(filter);
60}
61
62bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
63  if (msg.routing_id() <= 0 || msg.routing_id() >= API_ID_COUNT) {
64    OnInvalidMessageReceived();
65    return true;
66  }
67
68  InterfaceProxy* proxy = GetInterfaceProxy(
69      static_cast<ApiID>(msg.routing_id()));
70  if (!proxy) {
71    NOTREACHED();
72    return true;
73  }
74  return proxy->OnMessageReceived(msg);
75}
76
77void Dispatcher::SetSerializationRules(
78    VarSerializationRules* var_serialization_rules) {
79  serialization_rules_ = var_serialization_rules;
80}
81
82void Dispatcher::OnInvalidMessageReceived() {
83}
84
85}  // namespace proxy
86}  // namespace ppapi
87