- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_device_enumeration_host_helper.h
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 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_DEVICE_ENUMERATION_HOST_HELPER_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_DEVICE_ENUMERATION_HOST_HELPER_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/common/content_export.h"
14 #include "ppapi/c/dev/ppb_device_ref_dev.h"
15
16 namespace ppapi {
17 struct DeviceRefData;
18
19 namespace host {
20 struct HostMessageContext;
21 struct ReplyMessageContext;
22 class ResourceHost;
23 }
24 }
25
26 namespace IPC {
27 class Message;
28 }
29
30 namespace content {
31
32 // Resource hosts that support device enumeration can use this class to filter
33 // and process PpapiHostMsg_DeviceEnumeration_* messages.
34 // TODO(yzshen): Refactor ppapi::host::ResourceMessageFilter to support message
35 // handling on the same thread, and then derive this class from the filter
36 // class.
37 class CONTENT_EXPORT PepperDeviceEnumerationHostHelper {
38  public:
39   class Delegate {
40    public:
41     virtual ~Delegate() {}
42
43     typedef base::Callback<
44         void (int /* request_id */,
45               bool /* succeeded */,
46               const std::vector<ppapi::DeviceRefData>& /* devices */)>
47         EnumerateDevicesCallback;
48
49     // Enumerates devices of the specified type. The request ID passed into the
50     // callback will be the same as the return value.
51     virtual int EnumerateDevices(PP_DeviceType_Dev type,
52                                  const EnumerateDevicesCallback& callback) = 0;
53     // Stop enumerating devices of the specified |request_id|. The |request_id|
54     // is the return value of EnumerateDevicesCallback.
55     virtual void StopEnumerateDevices(int request_id) = 0;
56   };
57
58   // |resource_host| and |delegate| must outlive this object.
59   PepperDeviceEnumerationHostHelper(ppapi::host::ResourceHost* resource_host,
60                                     Delegate* delegate,
61                                     PP_DeviceType_Dev device_type);
62   ~PepperDeviceEnumerationHostHelper();
63
64   // Returns true if the message has been handled.
65   bool HandleResourceMessage(const IPC::Message& msg,
66                              ppapi::host::HostMessageContext* context,
67                              int32_t* result);
68
69  private:
70   class ScopedRequest;
71
72   // Has a different signature than HandleResourceMessage() in order to utilize
73   // message dispatching macros.
74   int32_t InternalHandleResourceMessage(
75       const IPC::Message& msg,
76       ppapi::host::HostMessageContext* context,
77       bool* handled);
78
79   int32_t OnEnumerateDevices(ppapi::host::HostMessageContext* context);
80   int32_t OnMonitorDeviceChange(ppapi::host::HostMessageContext* context,
81                                 uint32_t callback_id);
82   int32_t OnStopMonitoringDeviceChange(
83       ppapi::host::HostMessageContext* context);
84
85   void OnEnumerateDevicesComplete(
86       int request_id,
87       bool succeeded,
88       const std::vector<ppapi::DeviceRefData>& devices);
89   void OnNotifyDeviceChange(
90       uint32_t callback_id,
91       int request_id,
92       bool succeeded,
93       const std::vector<ppapi::DeviceRefData>& devices);
94
95   // Non-owning pointers.
96   ppapi::host::ResourceHost* resource_host_;
97   Delegate* delegate_;
98
99   PP_DeviceType_Dev device_type_;
100
101   scoped_ptr<ScopedRequest> enumerate_;
102   scoped_ptr<ScopedRequest> monitor_;
103
104   scoped_ptr<ppapi::host::ReplyMessageContext> enumerate_devices_context_;
105
106   DISALLOW_COPY_AND_ASSIGN(PepperDeviceEnumerationHostHelper);
107 };
108
109 }  // namespace content
110
111 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_DEVICE_ENUMERATION_HOST_HELPER_H_