netwerk/protocol/device/nsDeviceChannel.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/device/nsDeviceChannel.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "plstr.h"
    1.10 +#include "nsDeviceChannel.h"
    1.11 +#include "nsDeviceCaptureProvider.h"
    1.12 +
    1.13 +#ifdef MOZ_WIDGET_ANDROID
    1.14 +#include "mozilla/Preferences.h"
    1.15 +#include "AndroidCaptureProvider.h"
    1.16 +#endif
    1.17 +
    1.18 +using namespace mozilla;
    1.19 +
    1.20 +// Copied from image/decoders/icon/nsIconURI.cpp
    1.21 +// takes a string like ?size=32&contentType=text/html and returns a new string
    1.22 +// containing just the attribute values. i.e you could pass in this string with
    1.23 +// an attribute name of "size=", this will return 32
    1.24 +// Assumption: attribute pairs are separated by &
    1.25 +void extractAttributeValue(const char* searchString, const char* attributeName, nsCString& result)
    1.26 +{
    1.27 +  result.Truncate();
    1.28 +
    1.29 +  if (!searchString || !attributeName)
    1.30 +    return;
    1.31 +
    1.32 +  uint32_t attributeNameSize = strlen(attributeName);
    1.33 +  const char *startOfAttribute = PL_strcasestr(searchString, attributeName);
    1.34 +  if (!startOfAttribute ||
    1.35 +      !( *(startOfAttribute-1) == '?' || *(startOfAttribute-1) == '&') )
    1.36 +    return;
    1.37 +
    1.38 +  startOfAttribute += attributeNameSize; // Skip the attributeName
    1.39 +  if (!*startOfAttribute)
    1.40 +    return;
    1.41 +
    1.42 +  const char *endOfAttribute = strchr(startOfAttribute, '&');
    1.43 +  if (endOfAttribute) {
    1.44 +    result.Assign(Substring(startOfAttribute, endOfAttribute));
    1.45 +  } else {
    1.46 +    result.Assign(startOfAttribute);
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +NS_IMPL_ISUPPORTS_INHERITED(nsDeviceChannel,
    1.51 +                            nsBaseChannel,
    1.52 +                            nsIChannel)
    1.53 +
    1.54 +// nsDeviceChannel methods
    1.55 +nsDeviceChannel::nsDeviceChannel()
    1.56 +{
    1.57 +  SetContentType(NS_LITERAL_CSTRING("image/png"));
    1.58 +}
    1.59 +
    1.60 +nsDeviceChannel::~nsDeviceChannel() 
    1.61 +{
    1.62 +}
    1.63 +
    1.64 +nsresult
    1.65 +nsDeviceChannel::Init(nsIURI* aUri)
    1.66 +{
    1.67 +  nsBaseChannel::Init();
    1.68 +  nsBaseChannel::SetURI(aUri);
    1.69 +  return NS_OK;
    1.70 +}
    1.71 +
    1.72 +nsresult
    1.73 +nsDeviceChannel::OpenContentStream(bool aAsync,
    1.74 +                                   nsIInputStream** aStream,
    1.75 +                                   nsIChannel** aChannel)
    1.76 +{
    1.77 +  if (!aAsync)
    1.78 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.79 +
    1.80 +  nsCOMPtr<nsIURI> uri = nsBaseChannel::URI();
    1.81 +  *aStream = nullptr;
    1.82 +  *aChannel = nullptr;
    1.83 +  NS_NAMED_LITERAL_CSTRING(width, "width=");
    1.84 +  NS_NAMED_LITERAL_CSTRING(height, "height=");
    1.85 +
    1.86 +  nsAutoCString spec;
    1.87 +  uri->GetSpec(spec);
    1.88 +
    1.89 +  nsAutoCString type;
    1.90 +
    1.91 +  nsRefPtr<nsDeviceCaptureProvider> capture;
    1.92 +  nsCaptureParams captureParams;
    1.93 +  captureParams.camera = 0;
    1.94 +  if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=image/png"),
    1.95 +                             true,
    1.96 +                             0,
    1.97 +                             -1)) {
    1.98 +    type.AssignLiteral("image/png");
    1.99 +    SetContentType(type);
   1.100 +    captureParams.captureAudio = false;
   1.101 +    captureParams.captureVideo = true;
   1.102 +    captureParams.timeLimit = 0;
   1.103 +    captureParams.frameLimit = 1;
   1.104 +    nsAutoCString buffer;
   1.105 +    extractAttributeValue(spec.get(), "width=", buffer);
   1.106 +    nsresult err;
   1.107 +    captureParams.width = buffer.ToInteger(&err);
   1.108 +    if (!captureParams.width)
   1.109 +      captureParams.width = 640;
   1.110 +    extractAttributeValue(spec.get(), "height=", buffer);
   1.111 +    captureParams.height = buffer.ToInteger(&err);
   1.112 +    if (!captureParams.height)
   1.113 +      captureParams.height = 480;
   1.114 +    extractAttributeValue(spec.get(), "camera=", buffer);
   1.115 +    captureParams.camera = buffer.ToInteger(&err);
   1.116 +    captureParams.bpp = 32;
   1.117 +#ifdef MOZ_WIDGET_ANDROID
   1.118 +    capture = GetAndroidCaptureProvider();
   1.119 +#endif
   1.120 +  } else if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=video/x-raw-yuv"),
   1.121 +                                    true,
   1.122 +                                    0,
   1.123 +                                    -1)) {
   1.124 +    type.AssignLiteral("video/x-raw-yuv");
   1.125 +    SetContentType(type);
   1.126 +    captureParams.captureAudio = false;
   1.127 +    captureParams.captureVideo = true;
   1.128 +    nsAutoCString buffer;
   1.129 +    extractAttributeValue(spec.get(), "width=", buffer);
   1.130 +    nsresult err;
   1.131 +    captureParams.width = buffer.ToInteger(&err);
   1.132 +    if (!captureParams.width)
   1.133 +      captureParams.width = 640;
   1.134 +    extractAttributeValue(spec.get(), "height=", buffer);
   1.135 +    captureParams.height = buffer.ToInteger(&err);
   1.136 +    if (!captureParams.height)
   1.137 +      captureParams.height = 480;
   1.138 +    extractAttributeValue(spec.get(), "camera=", buffer);
   1.139 +    captureParams.camera = buffer.ToInteger(&err);
   1.140 +    captureParams.bpp = 32;
   1.141 +    captureParams.timeLimit = 0;
   1.142 +    captureParams.frameLimit = 60000;
   1.143 +#ifdef MOZ_WIDGET_ANDROID
   1.144 +    // only enable if "device.camera.enabled" is true.
   1.145 +    if (Preferences::GetBool("device.camera.enabled", false) == true)
   1.146 +      capture = GetAndroidCaptureProvider();
   1.147 +#endif
   1.148 +  } else {
   1.149 +    return NS_ERROR_NOT_IMPLEMENTED;
   1.150 +  }
   1.151 +
   1.152 +  if (!capture)
   1.153 +    return NS_ERROR_FAILURE;
   1.154 +
   1.155 +  return capture->Init(type, &captureParams, aStream);
   1.156 +}

mercurial