// Copyright 2014 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.

// Use the <code>chrome.gcdPrivate</code> API to discover GCD APIs and register
// them.
namespace gcdPrivate {

  enum Status {
    // Success.
    success,

    // populateWifiPassword was true and the password has not been prefetched.
    wifiPasswordError,

    // populateWifiPassword was true and the message cannot be parsed as a setup
    // message.
    setupParseError,

    // Could not connect to the device.
    connectionError,

    // Error in establishing session.
    sessionError,

    // Unknown session.
    unknownSessionError,

    // Bad pairing code.
    badPairingCodeError,

    // Device error with details in response object.
    deviceError,

    // Service resulution failed.
    serviceResolutionError
  };

  enum PairingType {
    pinCode,
    embeddedCode
  };

  // Generic callback for session calls, with status only.
  callback SessionCallback = void(Status status);

  // Called when the device info is available or on error.
  // |status| : The status of operation (success or type of error).
  // |deviceInfo| : Content of /privet/info response.
  // https://developers.google.com/cloud-devices/v1/reference/local-api/info
  callback GetDeviceInfoCallback = void(Status status,
                                        object deviceInfo);

  // Called when device starts to establish a secure session.
  // If |status| is |success| app should call |startPairing|.
  // |sessionId| : The session ID (identifies the session for future calls).
  // |status| : The status of operation (success or type of error).
  // |pairingTypes| is the list of supported pairing types.
  callback CreateSessionCallback = void(long sessionId,
                                        Status status,
                                        PairingType[] pairingTypes);

  // Called when the response to the message sent is available or on error.
  // |status| : The status of operation (success or type of error).
  // |response| : The response object with result or error description. May be
  // empty for some errors.
  callback SendMessageCallback = void(Status status, object response);

  // Called as a response to |prefetchWifiPassword|
  // |success| : Denotes whether the password fetch has succeeded or failed.
  callback SuccessCallback = void(boolean success);

  interface Functions {
    // Returns local device information.
    // |serviceName| : The mDns service name of the device.
    static void getDeviceInfo(DOMString serviceName,
                              GetDeviceInfoCallback callback);

    // Create new pairing.
    // |serviceName| : The mDns service name of the device.
    static void createSession(DOMString serviceName,
                              CreateSessionCallback callback);

    // Start pairing with selected method. Should be called after
    // |establishSession|.
    // |sessionId| : The ID of the session created with |establishSession|.
    // |pairingType| : The value selected from the list provided in
    // callback of |establishSession|.
    static void startPairing(long sessionId,
                             PairingType pairingType,
                             SessionCallback callback);

    // Confirm pairing code. Should be called after |startPairing|.
    // |sessionId| : The ID of the session created with |establishSession|.
    // |code| : The string generated by pairing process and available to the
    // user.
    static void confirmCode(long sessionId,
                            DOMString code,
                            SessionCallback callback);

    // Send an encrypted message to the device.
    // If the message is a setup message with a wifi SSID specified but no
    // password, the password cached by |prefetchWifiPassword| will be used and
    // the call will fail if it's not available. For open networks use an empty
    // string as the password.
    // |sessionId| : The ID of the session created with |establishSession|.
    // |api| : The Privet API name to call.
    // |input| : Input data for |api|.
    static void sendMessage(long sessionId,
                            DOMString api,
                            object input,
                            SendMessageCallback callback);

    // Terminate the session with the device.
    // |sessionId| : The ID of the session created with |establishSession|.
    static void terminateSession(long sessionId);
  };
};