Пример #1
0
void
nsDOMDataChannel::Send(Blob& aData, ErrorResult& aRv)
{
  MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");

  nsCOMPtr<nsIInputStream> msgStream;
  aData.GetInternalStream(getter_AddRefs(msgStream), aRv);
  if (NS_WARN_IF(aRv.Failed())){
    return;
  }

  uint64_t msgLength = aData.GetSize(aRv);
  if (NS_WARN_IF(aRv.Failed())){
    return;
  }

  if (msgLength > UINT32_MAX) {
    aRv.Throw(NS_ERROR_FILE_TOO_BIG);
    return;
  }

  Send(msgStream, EmptyCString(), msgLength, true, aRv);
}
NS_IMETHODIMP
TestGonkCameraHardwareListener::HandleEvent(nsIDOMEvent* aEvent)
{
  nsString eventType;
  aEvent->GetType(eventType);

  DOM_CAMERA_LOGI("Inject '%s' event",
    NS_ConvertUTF16toUTF8(eventType).get());

  if (eventType.EqualsLiteral("focus")) {
    CameraStateChangeEvent* event = aEvent->InternalDOMEvent()->AsCameraStateChangeEvent();

    if (!NS_WARN_IF(!event)) {
      nsString state;

      event->GetNewState(state);
      if (state.EqualsLiteral("focused")) {
        OnAutoFocusComplete(mTarget, true);
      } else if (state.EqualsLiteral("unfocused")) {
        OnAutoFocusComplete(mTarget, false);
      } else if (state.EqualsLiteral("focusing")) {
        OnAutoFocusMoving(mTarget, true);
      } else if (state.EqualsLiteral("not_focusing")) {
        OnAutoFocusMoving(mTarget, false);
      } else {
        DOM_CAMERA_LOGE("Unhandled focus state '%s'\n",
          NS_ConvertUTF16toUTF8(state).get());
      }
    }
  } else if (eventType.EqualsLiteral("shutter")) {
    DOM_CAMERA_LOGI("Inject shutter event");
    OnShutter(mTarget);
  } else if (eventType.EqualsLiteral("picture")) {
    BlobEvent* event = aEvent->InternalDOMEvent()->AsBlobEvent();

    if (!NS_WARN_IF(!event)) {
      Blob* blob = event->GetData();

      if (blob) {
        static const uint64_t MAX_FILE_SIZE = 2147483647;

        ErrorResult rv;
        uint64_t dataLength = blob->GetSize(rv);

        if (NS_WARN_IF(rv.Failed()) || NS_WARN_IF(dataLength > MAX_FILE_SIZE)) {
          rv.SuppressException();
          return NS_OK;
        }

        nsCOMPtr<nsIInputStream> inputStream;
        blob->GetInternalStream(getter_AddRefs(inputStream), rv);
        if (NS_WARN_IF(rv.Failed())) {
          rv.SuppressException();
          return NS_OK;
        }

        uint8_t* data = new uint8_t[dataLength];
        rv = NS_ReadInputStreamToBuffer(inputStream,
                                        reinterpret_cast<void**>(&data),
                                        static_cast<uint32_t>(dataLength));
        if (NS_WARN_IF(rv.Failed())) {
          rv.SuppressException();
          delete [] data;
          return NS_OK;
        }

        OnTakePictureComplete(mTarget, data, dataLength);
        delete [] data;
      } else {
        OnTakePictureComplete(mTarget, nullptr, 0);
      }
    }
  } else if(eventType.EqualsLiteral("error")) {
    ErrorEvent* event = aEvent->InternalDOMEvent()->AsErrorEvent();

    if (!NS_WARN_IF(!event)) {
      nsString errorType;

      event->GetMessage(errorType);
      if (errorType.EqualsLiteral("picture")) {
        OnTakePictureError(mTarget);
      } else if (errorType.EqualsLiteral("system")) {
        if (!NS_WARN_IF(!mCameraThread)) {
          class DeferredSystemFailure : public nsRunnable
          {
          public:
            DeferredSystemFailure(nsGonkCameraControl* aTarget)
              : mTarget(aTarget)
            { }

            NS_IMETHODIMP
            Run()
            {
              OnSystemError(mTarget, CameraControlListener::kSystemService, 100, 0);
              return NS_OK;
            }

          protected:
            nsRefPtr<nsGonkCameraControl> mTarget;
          };

          mCameraThread->Dispatch(new DeferredSystemFailure(mTarget), NS_DISPATCH_NORMAL);
        }
      } else {
        DOM_CAMERA_LOGE("Unhandled error event type '%s'\n",
          NS_ConvertUTF16toUTF8(errorType).get());
      }
    }
  } else if(eventType.EqualsLiteral("facesdetected")) {
    CameraFacesDetectedEvent* event = aEvent->InternalDOMEvent()->AsCameraFacesDetectedEvent();

    if (!NS_WARN_IF(!event)) {
      Nullable<nsTArray<nsRefPtr<DOMCameraDetectedFace>>> faces;
      event->GetFaces(faces);

      camera_frame_metadata_t metadata;
      memset(&metadata, 0, sizeof(metadata));

      if (faces.IsNull()) {
        OnFacesDetected(mTarget, &metadata);
      } else {
        const nsTArray<nsRefPtr<DOMCameraDetectedFace>>& facesData = faces.Value();
        uint32_t i = facesData.Length();

        metadata.number_of_faces = i;
        metadata.faces = new camera_face_t[i];
        memset(metadata.faces, 0, sizeof(camera_face_t) * i);

        while (i > 0) {
          --i;
          const nsRefPtr<DOMCameraDetectedFace>& face = facesData[i];
          camera_face_t& f = metadata.faces[i];
          const DOMRect& bounds = *face->Bounds();
          f.rect[0] = static_cast<int32_t>(bounds.Left());
          f.rect[1] = static_cast<int32_t>(bounds.Top());
          f.rect[2] = static_cast<int32_t>(bounds.Right());
          f.rect[3] = static_cast<int32_t>(bounds.Bottom());
          CopyFaceFeature(f.left_eye, face->HasLeftEye(), face->GetLeftEye());
          CopyFaceFeature(f.right_eye, face->HasRightEye(), face->GetRightEye());
          CopyFaceFeature(f.mouth, face->HasMouth(), face->GetMouth());
          f.id = face->Id();
          f.score = face->Score();
        }

        OnFacesDetected(mTarget, &metadata);
        delete [] metadata.faces;
      }
    }
  } else {
    DOM_CAMERA_LOGE("Unhandled injected event '%s'",
      NS_ConvertUTF16toUTF8(eventType).get());
  }

  return NS_OK;
}