/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupports.idl"

%{C++
#include "nsTArrayForwardDeclare.h"
#include "nsStringFwd.h"
%}

[ref] native nsCString(const nsCString);
[ref] native StringArrayRef(const nsTArray<nsCString>);

/**
 * Start-up parameters for subprocesses are passed through nsIObserverService,
 * which, unfortunately, means we need to implement nsISupports in order to
 * go through it.
 */
[builtinclass, uuid(0a175ba7-8fcf-4ce9-9c4b-ccc6272f4425)]
interface nsIProfilerStartParams : nsISupports
{
  readonly attribute uint32_t entries;
  readonly attribute double interval;
  readonly attribute uint32_t features;

  [noscript, notxpcom, nostdcall] StringArrayRef getFilters();
};

[scriptable, uuid(ead3f75c-0e0e-4fbb-901c-1e5392ef5b2a)]
interface nsIProfiler : nsISupports
{
  boolean CanProfile();
  void StartProfiler(in uint32_t aEntries, in double aInterval,
                      [array, size_is(aFeatureCount)] in string aFeatures,
                      in uint32_t aFeatureCount,
                      [array, size_is(aFilterCount), optional] in string aFilters,
                      [optional] in uint32_t aFilterCount);
  void StopProfiler();
  boolean IsPaused();
  void PauseSampling();
  void ResumeSampling();
  void AddMarker(in string aMarker);
  /*
   * Returns the JSON string of the profile. If aSinceTime is passed, only
   * report samples taken at >= aSinceTime.
   */
  string GetProfile([optional] in double aSinceTime);

  /*
   * Returns a JS object of the profile. If aSinceTime is passed, only report
   * samples taken at >= aSinceTime.
   */
  [implicit_jscontext]
  jsval getProfileData([optional] in double aSinceTime);

  [implicit_jscontext]
  nsISupports getProfileDataAsync([optional] in double aSinceTime);

  [implicit_jscontext]
  nsISupports getProfileDataAsArrayBuffer([optional] in double aSinceTime);

  /**
   * Returns a promise that resolves once the file has been written.
   */
  [implicit_jscontext]
  nsISupports dumpProfileToFileAsync(in ACString aFilename,
                                     [optional] in double aSinceTime);

  boolean IsActive();

  /**
   * Returns an array of the features that are supported in this build.
   * Features may vary depending on platform and build flags.
   */
  void GetFeatures(out uint32_t aCount, [retval, array, size_is(aCount)] out string aFeatures);

  /**
   * Returns an array of all features that are supported by the profiler.
   * The array may contain features that are not supported in this build.
   */
  void GetAllFeatures(out uint32_t aCount, [retval, array, size_is(aCount)] out string aFeatures);

  /**
   * The starting parameters that were sent to the profiler for sampling.
   * If the profiler is not currently sampling, this will return null.
   */
  [noscript] readonly attribute nsIProfilerStartParams startParams;

  void GetBufferInfo(out uint32_t aCurrentPosition, out uint32_t aTotalSize,
                     out uint32_t aGeneration);

  /**
   * Returns the elapsed time, in milliseconds, since the profiler's epoch.
   * The epoch is guaranteed to be constant for the duration of the
   * process, but is otherwise arbitrary.
   */
  double getElapsedTime();

  /**
   * Contains an array of shared library objects.
   * Every object has the properties:
   *  - start:      The start address of the memory region occupied by this library.
   *  - end:        The end address of the memory region occupied by this library.
   *  - offset:     Usually zero, except on Android if the region was mapped from
   *                a file (using mmap), then this is the offset in the file where
   *                the mapping begins.
   *  - name:       The name (file basename) of the binary.
   *  - path:       The full absolute path to the binary.
   *  - debugName:  On Windows, the name of the pdb file for the binary. On other
   *                platforms, the same as |name|.
   *  - debugPath:  On Windows, the full absolute path of the pdb file for the
   *                binary. On other platforms, the same as |path|.
   *  - arch:       On Mac, the name of the architecture that identifies the right
   *                binary image of a fat binary. Example values are "i386", "x86_64",
   *                and "x86_64h". (x86_64h is used for binaries that contain
   *                instructions that are specific to the Intel Haswell microarchitecture.)
   *                On non-Mac platforms, arch is "".
   *  - breakpadId: A unique identifier string for this library, as used by breakpad.
   */
  [implicit_jscontext]
  readonly attribute jsval sharedLibraries;

  /**
   * Dump the collected profile to a file.
   */
  void dumpProfileToFile(in string aFilename);

  [noscript, notxpcom, nostdcall]
  void receiveShutdownProfile(in nsCString aProfile);
};
