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: webkit/plugins/ppapi/*
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 {
43    return !host_resource_;
44  }
45
46  // Some resources are plugin-side only and don't have a corresponding
47  // resource in the host. Yet these resources still need an instance to be
48  // associated with. This function creates a HostResource with the given
49  // instances and a 0 host resource ID for these cases.
50  static HostResource MakeInstanceOnly(PP_Instance instance);
51
52  // Sets and retrieves the internal PP_Resource which is valid for the host
53  // (a.k.a. renderer, as opposed to the plugin) process.
54  //
55  // DO NOT CALL THESE FUNCTIONS IN THE PLUGIN SIDE OF THE PROXY. The values
56  // will be invalid. See the class comment above.
57  void SetHostResource(PP_Instance instance, PP_Resource resource);
58  PP_Resource host_resource() const {
59    return host_resource_;
60  }
61
62  PP_Instance instance() const { return instance_; }
63
64  // This object is used in maps so we need to provide this sorting operator.
65  bool operator<(const HostResource& other) const {
66    if (instance_ != other.instance_)
67      return instance_ < other.instance_;
68    return host_resource_ < other.host_resource_;
69  }
70
71 private:
72  PP_Instance instance_;
73  PP_Resource host_resource_;
74};
75
76}  // namespace ppapi
77
78#endif  // PPAPI_SHARED_IMPL_HOST_RESOURCE_H_
79