1// Copyright (c) 2011 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#ifndef PPAPI_SHARED_IMPL_HOST_RESOURCE_H_
6#define PPAPI_SHARED_IMPL_HOST_RESOURCE_H_
7
8#include "ppapi/c/pp_instance.h"
9#include "ppapi/c/pp_resource.h"
10#include "ppapi/shared_impl/ppapi_shared_export.h"
11
12namespace ppapi {
13
14// For "old" style resources, PP_Resource values differ on the host and plugin
15// side. Implementations of those should be careful to use HostResource to
16// prevent confusion. "New" style resources use the same PP_Resource value on
17// the host and plugin sides, and should not use HostResource.
18//
19// Old style resources match these file specs:
20//   Proxy: ppapi/proxy/ppb_*_proxy.*
21//   Host: content/ppapi_plugin/*
22// New style resources match these file specs:
23//   Proxy: ppapi/proxy/*_resource.*
24//   Browser: (content|chrome)/browser/renderer_host/pepper/pepper_*_host.*
25//   Renderer: (content|chrome)/renderer/pepper/pepper_*_host.*
26//
27//
28// Represents a PP_Resource sent over the wire. This just wraps a PP_Resource.
29// The point is to prevent mistakes where the wrong resource value is sent.
30// Resource values are remapped in the plugin so that it can talk to multiple
31// hosts. If all values were PP_Resource, it would be easy to forget to do
32// this transformation.
33//
34// To get the corresponding plugin PP_Resource for a HostResource, use
35// PluginResourceTracker::PluginResourceForHostResource().
36//
37// All HostResources respresent IDs valid in the host.
38class PPAPI_SHARED_EXPORT HostResource {
39 public:
40  HostResource();
41
42  bool is_null() const { return !host_resource_; }
43
44  // Some resources are plugin-side only and don't have a corresponding
45  // resource in the host. Yet these resources still need an instance to be
46  // associated with. This function creates a HostResource with the given
47  // instances and a 0 host resource ID for these cases.
48  static HostResource MakeInstanceOnly(PP_Instance instance);
49
50  // Sets and retrieves the internal PP_Resource which is valid for the host
51  // (a.k.a. renderer, as opposed to the plugin) process.
52  //
53  // DO NOT CALL THESE FUNCTIONS IN THE PLUGIN SIDE OF THE PROXY. The values
54  // will be invalid. See the class comment above.
55  void SetHostResource(PP_Instance instance, PP_Resource resource);
56  PP_Resource host_resource() const { return host_resource_; }
57
58  PP_Instance instance() const { return instance_; }
59
60  // This object is used in maps so we need to provide this sorting operator.
61  bool operator<(const HostResource& other) const {
62    if (instance_ != other.instance_)
63      return instance_ < other.instance_;
64    return host_resource_ < other.host_resource_;
65  }
66
67 private:
68  PP_Instance instance_;
69  PP_Resource host_resource_;
70};
71
72}  // namespace ppapi
73
74#endif  // PPAPI_SHARED_IMPL_HOST_RESOURCE_H_
75