Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "plstr.h"
7 : #include "nsDeviceChannel.h"
8 : #include "nsDeviceCaptureProvider.h"
9 :
10 : #ifdef MOZ_WIDGET_ANDROID
11 : #include "mozilla/Preferences.h"
12 : #include "AndroidCaptureProvider.h"
13 : #endif
14 :
15 : using namespace mozilla;
16 :
17 : // Copied from image/decoders/icon/nsIconURI.cpp
18 : // takes a string like ?size=32&contentType=text/html and returns a new string
19 : // containing just the attribute values. i.e you could pass in this string with
20 : // an attribute name of "size=", this will return 32
21 : // Assumption: attribute pairs are separated by &
22 0 : void extractAttributeValue(const char* searchString, const char* attributeName, nsCString& result)
23 : {
24 0 : result.Truncate();
25 :
26 0 : if (!searchString || !attributeName)
27 0 : return;
28 :
29 0 : uint32_t attributeNameSize = strlen(attributeName);
30 0 : const char *startOfAttribute = PL_strcasestr(searchString, attributeName);
31 0 : if (!startOfAttribute ||
32 0 : !( *(startOfAttribute-1) == '?' || *(startOfAttribute-1) == '&') )
33 0 : return;
34 :
35 0 : startOfAttribute += attributeNameSize; // Skip the attributeName
36 0 : if (!*startOfAttribute)
37 0 : return;
38 :
39 0 : const char *endOfAttribute = strchr(startOfAttribute, '&');
40 0 : if (endOfAttribute) {
41 0 : result.Assign(Substring(startOfAttribute, endOfAttribute));
42 : } else {
43 0 : result.Assign(startOfAttribute);
44 : }
45 : }
46 :
47 0 : NS_IMPL_ISUPPORTS_INHERITED(nsDeviceChannel,
48 : nsBaseChannel,
49 : nsIChannel)
50 :
51 : // nsDeviceChannel methods
52 0 : nsDeviceChannel::nsDeviceChannel()
53 : {
54 0 : SetContentType(NS_LITERAL_CSTRING("image/png"));
55 0 : }
56 :
57 0 : nsDeviceChannel::~nsDeviceChannel()
58 : {
59 0 : }
60 :
61 : nsresult
62 0 : nsDeviceChannel::Init(nsIURI* aUri)
63 : {
64 0 : nsBaseChannel::Init();
65 0 : nsBaseChannel::SetURI(aUri);
66 0 : return NS_OK;
67 : }
68 :
69 : nsresult
70 0 : nsDeviceChannel::OpenContentStream(bool aAsync,
71 : nsIInputStream** aStream,
72 : nsIChannel** aChannel)
73 : {
74 0 : if (!aAsync)
75 0 : return NS_ERROR_NOT_IMPLEMENTED;
76 :
77 0 : nsCOMPtr<nsIURI> uri = nsBaseChannel::URI();
78 0 : *aStream = nullptr;
79 0 : *aChannel = nullptr;
80 0 : NS_NAMED_LITERAL_CSTRING(width, "width=");
81 0 : NS_NAMED_LITERAL_CSTRING(height, "height=");
82 :
83 0 : nsAutoCString spec;
84 0 : nsresult rv = uri->GetSpec(spec);
85 0 : NS_ENSURE_SUCCESS(rv, rv);
86 :
87 0 : nsAutoCString type;
88 :
89 0 : RefPtr<nsDeviceCaptureProvider> capture;
90 : nsCaptureParams captureParams;
91 0 : captureParams.camera = 0;
92 0 : if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=image/png"),
93 : true,
94 : 0,
95 : -1)) {
96 0 : type.AssignLiteral("image/png");
97 0 : SetContentType(type);
98 0 : captureParams.captureAudio = false;
99 0 : captureParams.captureVideo = true;
100 0 : captureParams.timeLimit = 0;
101 0 : captureParams.frameLimit = 1;
102 0 : nsAutoCString buffer;
103 0 : extractAttributeValue(spec.get(), "width=", buffer);
104 : nsresult err;
105 0 : captureParams.width = buffer.ToInteger(&err);
106 0 : if (!captureParams.width)
107 0 : captureParams.width = 640;
108 0 : extractAttributeValue(spec.get(), "height=", buffer);
109 0 : captureParams.height = buffer.ToInteger(&err);
110 0 : if (!captureParams.height)
111 0 : captureParams.height = 480;
112 0 : extractAttributeValue(spec.get(), "camera=", buffer);
113 0 : captureParams.camera = buffer.ToInteger(&err);
114 0 : captureParams.bpp = 32;
115 : #ifdef MOZ_WIDGET_ANDROID
116 : capture = GetAndroidCaptureProvider();
117 : #endif
118 0 : } else if (kNotFound != spec.Find(NS_LITERAL_CSTRING("type=video/x-raw-yuv"),
119 : true,
120 : 0,
121 : -1)) {
122 0 : type.AssignLiteral("video/x-raw-yuv");
123 0 : SetContentType(type);
124 0 : captureParams.captureAudio = false;
125 0 : captureParams.captureVideo = true;
126 0 : nsAutoCString buffer;
127 0 : extractAttributeValue(spec.get(), "width=", buffer);
128 : nsresult err;
129 0 : captureParams.width = buffer.ToInteger(&err);
130 0 : if (!captureParams.width)
131 0 : captureParams.width = 640;
132 0 : extractAttributeValue(spec.get(), "height=", buffer);
133 0 : captureParams.height = buffer.ToInteger(&err);
134 0 : if (!captureParams.height)
135 0 : captureParams.height = 480;
136 0 : extractAttributeValue(spec.get(), "camera=", buffer);
137 0 : captureParams.camera = buffer.ToInteger(&err);
138 0 : captureParams.bpp = 32;
139 0 : captureParams.timeLimit = 0;
140 0 : captureParams.frameLimit = 60000;
141 : #ifdef MOZ_WIDGET_ANDROID
142 : // only enable if "device.camera.enabled" is true.
143 : if (Preferences::GetBool("device.camera.enabled", false) == true)
144 : capture = GetAndroidCaptureProvider();
145 : #endif
146 : } else {
147 0 : return NS_ERROR_NOT_IMPLEMENTED;
148 : }
149 :
150 0 : if (!capture)
151 0 : return NS_ERROR_FAILURE;
152 :
153 0 : return capture->Init(type, &captureParams, aStream);
154 : }
|