// Copyright 2016 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef PPAPI_CPP_VPN_PROVIDER_H_ #define PPAPI_CPP_VPN_PROVIDER_H_ #include "ppapi/c/ppb_vpn_provider.h" #include "ppapi/cpp/completion_callback.h" namespace pp { class InstanceHandle; /// @file /// This file defines the VpnProvider interface providing a way to implement a /// VPN client. /// Important: This API is available only on Chrome OS. /// The VpnProvider class enhances the /// chrome.vpnProvider JavaScript API by providing a high /// performance path for packet handling. /// /// Permissions: Apps permission vpnProvider is required for /// VpnProvider.Bind(). /// /// Typical usage: /// - Create a VpnProvider instance. /// - Register the callback for VpnProvider.ReceivePacket(). /// - In the extension follow the usual workflow for configuring a VPN /// connection via the chrome.vpnProvider API until the step for /// notifying the connection state as "connected". /// - Bind to the previously created connection using /// VpnProvider.Bind(). /// - Notify the connection state as "connected" from JavaScript using /// chrome.vpnProvider.notifyConnectionStateChanged. /// - When the steps above are completed without errors, a virtual tunnel is /// created to the network stack of Chrome OS. IP packets can be sent through /// the tunnel using VpnProvider.SendPacket() and any packets /// originating on the Chrome OS device will be received using the callback /// registered for VpnProvider.ReceivePacket(). /// - When the user disconnects from the VPN configuration or there is an error /// the extension will be notfied via /// chrome.vpnProvider.onPlatformMessage. class VpnProvider : public Resource { public: /// Constructs a VpnProvider object. /// /// @param[in] instance The instance with which this resource will be /// associated. explicit VpnProvider(const InstanceHandle& instance); /// Destructs a VpnProvider object. virtual ~VpnProvider(); /// Static function for determining whether the browser supports the /// VpnProvider interface. /// /// @return true if the interface is available, false otherwise. static bool IsAvailable(); /// Binds to an existing configuration created from JavaScript by /// chrome.vpnProvider.createConfig. All packets will be routed /// via SendPacket and ReceivePacket. The user /// should register the callback for ReceivePacket before calling /// Bind(). /// /// @param[in] configuration_id The configuration id from the callback of /// chrome.vpnProvider.createConfig. This Var must /// be of string type. /// /// @param[in] configuration_name The configuration name as defined by the /// user when calling chrome.vpnProvider.createConfig. This /// Var must be of string type. /// /// @param[in] callback A CompletionCallback to be called on /// completion. /// /// @return An int32_t containing an error code from pp_errors.h. /// Returns PP_ERROR_INPROGRESS if a previous call to /// Bind() has not completed. /// Returns PP_ERROR_BADARGUMENT if the Var type of /// either configuration_id or configuration_name is /// not of string type. /// Returns PP_ERROR_NOACCESS if the caller does the have the /// required "vpnProvider" permission. /// Returns PP_ERROR_FAILED if connection_id and /// connection_name could not be matched with the existing /// connection, or if the plugin originates from a different extension than /// the one that created the connection. int32_t Bind(const Var& configuration_id, const Var& configuration_name, const CompletionCallback& callback); /// Sends an IP packet through the tunnel created for the VPN session. This /// will succeed only when the VPN session is owned by the module and /// connection is bound. /// /// @param[in] packet IP packet to be sent to the platform. The /// Var must be of ArrayBuffer type. /// /// @param[in] callback A CompletionCallback to be called on /// completion. /// /// @return An int32_t containing an error code from pp_errors.h. /// Returns PP_ERROR_FAILED if the connection is not bound. /// Returns PP_ERROR_INPROGRESS if a previous call to /// SendPacket() has not completed. /// Returns PP_ERROR_BADARGUMENT if the Var type of /// packet is not of ArrayBuffer type. int32_t SendPacket(const Var& packet, const CompletionCallback& callback); /// Receives an IP packet from the tunnel for the VPN session. This function /// only returns a single packet. That is, this function must be called at /// least N times to receive N packets, no matter the size of each packet. The /// callback should be registered before calling Bind(). /// /// @param[in] callback A CompletionCallbackWithOutput to be /// called upon completion of ReceivePacket. It will be passed an ArrayBuffer /// type Var containing an IP packet to be sent to the platform. /// /// @return An int32_t containing an error code from pp_errors.h. /// Returns PP_ERROR_INPROGRESS if a previous call to /// ReceivePacket() has not completed. int32_t ReceivePacket(const CompletionCallbackWithOutput& callback); private: InstanceHandle associated_instance_; }; } // namespace pp #endif // PPAPI_CPP_VPN_PROVIDER_H_