Infobar material design refresh: bg color
[chromium-blink-merge.git] / ppapi / examples / enumerate_devices / enumerate_devices.cc
blob383a0e5280a07d47be2219f70e4b182c74cad965
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 #include <assert.h>
6 #include <string.h>
8 #include <vector>
10 #include "ppapi/c/dev/ppb_video_capture_dev.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/cpp/dev/device_ref_dev.h"
13 #include "ppapi/cpp/dev/video_capture_dev.h"
14 #include "ppapi/cpp/dev/video_capture_client_dev.h"
15 #include "ppapi/cpp/completion_callback.h"
16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/module.h"
18 #include "ppapi/cpp/private/flash.h"
19 #include "ppapi/cpp/var.h"
20 #include "ppapi/utility/completion_callback_factory.h"
22 // When compiling natively on Windows, PostMessage can be #define-d to
23 // something else.
24 #ifdef PostMessage
25 #undef PostMessage
26 #endif
28 namespace {
30 // This object is the global object representing this plugin library as long
31 // as it is loaded.
32 class EnumerateDevicesDemoModule : public pp::Module {
33 public:
34 EnumerateDevicesDemoModule() : pp::Module() {}
35 virtual ~EnumerateDevicesDemoModule() {}
36 virtual pp::Instance* CreateInstance(PP_Instance instance);
39 class EnumerateDevicesDemoInstance : public pp::Instance,
40 public pp::VideoCaptureClient_Dev {
41 public:
42 EnumerateDevicesDemoInstance(PP_Instance instance, pp::Module* module);
43 virtual ~EnumerateDevicesDemoInstance();
45 // pp::Instance implementation (see PPP_Instance).
46 virtual void HandleMessage(const pp::Var& message_data);
48 // pp::VideoCaptureClient_Dev implementation.
49 virtual void OnDeviceInfo(PP_Resource resource,
50 const PP_VideoCaptureDeviceInfo_Dev& info,
51 const std::vector<pp::Buffer_Dev>& buffers) {}
52 virtual void OnStatus(PP_Resource resource, uint32_t status) {}
53 virtual void OnError(PP_Resource resource, uint32_t error) {}
54 virtual void OnBufferReady(PP_Resource resource, uint32_t buffer) {}
56 private:
57 void EnumerateDevicesFinished(int32_t result,
58 std::vector<pp::DeviceRef_Dev>& devices);
60 pp::VideoCapture_Dev video_capture_;
61 pp::CompletionCallbackFactory<EnumerateDevicesDemoInstance> callback_factory_;
63 std::vector<pp::DeviceRef_Dev> devices_;
66 EnumerateDevicesDemoInstance::EnumerateDevicesDemoInstance(PP_Instance instance,
67 pp::Module* module)
68 : pp::Instance(instance),
69 pp::VideoCaptureClient_Dev(this),
70 video_capture_(this),
71 callback_factory_(this) {
74 EnumerateDevicesDemoInstance::~EnumerateDevicesDemoInstance() {
77 void EnumerateDevicesDemoInstance::HandleMessage(const pp::Var& message_data) {
78 if (message_data.is_string()) {
79 std::string event = message_data.AsString();
80 if (event == "EnumerateDevicesAsync") {
81 pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> >
82 callback = callback_factory_.NewCallbackWithOutput(
83 &EnumerateDevicesDemoInstance::EnumerateDevicesFinished);
84 video_capture_.EnumerateDevices(callback);
85 } else if (event == "EnumerateDevicesSync") {
86 std::vector<pp::DeviceRef_Dev> devices;
87 int32_t result = pp::flash::Flash::EnumerateVideoCaptureDevices(
88 this, video_capture_, &devices);
89 EnumerateDevicesFinished(result, devices);
94 void EnumerateDevicesDemoInstance::EnumerateDevicesFinished(
95 int32_t result,
96 std::vector<pp::DeviceRef_Dev>& devices) {
97 static const char* const kDelimiter = "#__#";
99 if (result == PP_OK) {
100 devices_.swap(devices);
101 std::string device_names;
102 for (size_t index = 0; index < devices_.size(); ++index) {
103 pp::Var name = devices_[index].GetName();
104 assert(name.is_string());
106 if (index != 0)
107 device_names += kDelimiter;
108 device_names += name.AsString();
110 PostMessage(pp::Var("EnumerationSuccess" + device_names));
111 } else {
112 PostMessage(pp::Var("EnumerationFailed"));
116 pp::Instance* EnumerateDevicesDemoModule::CreateInstance(PP_Instance instance) {
117 return new EnumerateDevicesDemoInstance(instance, this);
120 } // anonymous namespace
122 namespace pp {
123 // Factory function for your specialization of the Module object.
124 Module* CreateModule() {
125 return new EnumerateDevicesDemoModule();
127 } // namespace pp