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