2011-08-25 03:42:23 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2012-12-21 05:04:50 +04:00
|
|
|
|
|
|
|
#include "nsTArray.h"
|
2011-08-25 03:42:23 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
|
|
|
#include "nsEscape.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsIURI.h"
|
2012-12-21 05:04:50 +04:00
|
|
|
#include <utility>
|
|
|
|
|
2011-08-25 03:42:23 +04:00
|
|
|
#include "nsMediaFragmentURIParser.h"
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
|
|
|
|
|
|
|
namespace mozilla { namespace net {
|
|
|
|
|
|
|
|
nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsIURI* aURI)
|
|
|
|
: mClipUnit(eClipUnit_Pixel)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
2012-12-21 05:04:50 +04:00
|
|
|
nsAutoCString ref;
|
|
|
|
aURI->GetRef(ref);
|
|
|
|
Parse(ref);
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsCString& aRef)
|
|
|
|
: mClipUnit(eClipUnit_Pixel)
|
|
|
|
{
|
|
|
|
Parse(aRef);
|
|
|
|
}
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPT(nsDependentSubstring aString)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
nsDependentSubstring original(aString);
|
|
|
|
if (aString.Length() > 4 &&
|
|
|
|
aString[0] == 'n' && aString[1] == 'p' &&
|
|
|
|
aString[2] == 't' && aString[3] == ':') {
|
|
|
|
aString.Rebind(aString, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aString.Length() == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
double start = -1.0;
|
|
|
|
double end = -1.0;
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
ParseNPTTime(aString, start);
|
2011-08-25 03:42:23 +04:00
|
|
|
|
|
|
|
if (aString.Length() == 0) {
|
2014-08-14 02:42:16 +04:00
|
|
|
mStart.emplace(start);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aString[0] != ',') {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, 1);
|
|
|
|
|
|
|
|
if (aString.Length() == 0) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
ParseNPTTime(aString, end);
|
2011-08-25 03:42:23 +04:00
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
if (end <= start || aString.Length() != 0) {
|
2011-08-25 03:42:23 +04:00
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2014-08-14 02:42:16 +04:00
|
|
|
mStart.emplace(start);
|
|
|
|
mEnd.emplace(end);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTTime(nsDependentSubstring& aString, double& aTime)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
if (aString.Length() == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
ParseNPTHHMMSS(aString, aTime) ||
|
|
|
|
ParseNPTMMSS(aString, aTime) ||
|
|
|
|
ParseNPTSec(aString, aTime);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
// Return true if the given character is a numeric character
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsDigit(nsDependentSubstring::char_type aChar)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
return (aChar >= '0' && aChar <= '9');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the index of the first character in the string that is not
|
|
|
|
// a numerical digit, starting from 'aStart'.
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t FirstNonDigit(nsDependentSubstring& aString, uint32_t aStart)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
while (aStart < aString.Length() && IsDigit(aString[aStart])) {
|
|
|
|
++aStart;
|
|
|
|
}
|
|
|
|
return aStart;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTSec(nsDependentSubstring& aString, double& aSec)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
nsDependentSubstring original(aString);
|
|
|
|
if (aString.Length() == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index = FirstNonDigit(aString, 0);
|
2011-08-25 03:42:23 +04:00
|
|
|
if (index == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring n(aString, 0, index);
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t s = PromiseFlatString(n).ToInteger(&ec);
|
2011-08-25 03:42:23 +04:00
|
|
|
if (NS_FAILED(ec)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, index);
|
|
|
|
double fraction = 0.0;
|
|
|
|
if (!ParseNPTFraction(aString, fraction)) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aSec = s + fraction;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTMMSS(nsDependentSubstring& aString, double& aTime)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
nsDependentSubstring original(aString);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mm = 0;
|
|
|
|
uint32_t ss = 0;
|
2011-08-25 03:42:23 +04:00
|
|
|
double fraction = 0.0;
|
|
|
|
if (!ParseNPTMM(aString, mm)) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aString.Length() < 2 || aString[0] != ':') {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, 1);
|
|
|
|
if (!ParseNPTSS(aString, ss)) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParseNPTFraction(aString, fraction)) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
aTime = mm * 60 + ss + fraction;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTFraction(nsDependentSubstring& aString, double& aFraction)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
double fraction = 0.0;
|
|
|
|
|
|
|
|
if (aString.Length() > 0 && aString[0] == '.') {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index = FirstNonDigit(aString, 1);
|
2011-08-25 03:42:23 +04:00
|
|
|
|
|
|
|
if (index > 1) {
|
|
|
|
nsDependentSubstring number(aString, 0, index);
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec;
|
2011-08-25 03:42:23 +04:00
|
|
|
fraction = PromiseFlatString(number).ToDouble(&ec);
|
|
|
|
if (NS_FAILED(ec)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aString.Rebind(aString, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
aFraction = fraction;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
nsDependentSubstring original(aString);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t hh = 0;
|
2011-08-25 03:42:23 +04:00
|
|
|
double seconds = 0.0;
|
|
|
|
if (!ParseNPTHH(aString, hh)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aString.Length() < 2 || aString[0] != ':') {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, 1);
|
|
|
|
if (!ParseNPTMMSS(aString, seconds)) {
|
|
|
|
aString.Rebind(original, 0);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aTime = hh * 3600 + seconds;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
if (aString.Length() == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index = FirstNonDigit(aString, 0);
|
2011-08-25 03:42:23 +04:00
|
|
|
if (index == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring n(aString, 0, index);
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t u = PromiseFlatString(n).ToInteger(&ec);
|
2011-08-25 03:42:23 +04:00
|
|
|
if (NS_FAILED(ec)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, index);
|
|
|
|
aHour = u;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
return ParseNPTSS(aString, aMinute);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsMediaFragmentURIParser::ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
|
|
|
if (aString.Length() < 2) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDigit(aString[0]) && IsDigit(aString[1])) {
|
|
|
|
nsDependentSubstring n(aString, 0, 2);
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult ec;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t u = PromiseFlatString(n).ToInteger(&ec);
|
2011-08-25 03:42:23 +04:00
|
|
|
if (NS_FAILED(ec)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, 2);
|
|
|
|
if (u >= 60)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
|
|
|
|
aSecond = u;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
static bool ParseInteger(nsDependentSubstring& aString,
|
|
|
|
int32_t& aResult)
|
|
|
|
{
|
|
|
|
uint32_t index = FirstNonDigit(aString, 0);
|
|
|
|
if (index == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring n(aString, 0, index);
|
|
|
|
nsresult ec;
|
|
|
|
int32_t s = PromiseFlatString(n).ToInteger(&ec);
|
|
|
|
if (NS_FAILED(ec)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aString.Rebind(aString, index);
|
|
|
|
aResult = s;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ParseCommaSeparator(nsDependentSubstring& aString)
|
|
|
|
{
|
|
|
|
if (aString.Length() > 1 && aString[0] == ',') {
|
|
|
|
aString.Rebind(aString, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsMediaFragmentURIParser::ParseXYWH(nsDependentSubstring aString)
|
2011-08-25 03:42:23 +04:00
|
|
|
{
|
2012-12-21 05:04:50 +04:00
|
|
|
int32_t x, y, w, h;
|
|
|
|
ClipUnit clipUnit;
|
|
|
|
|
|
|
|
// Determine units.
|
|
|
|
if (StringBeginsWith(aString, NS_LITERAL_STRING("pixel:"))) {
|
|
|
|
clipUnit = eClipUnit_Pixel;
|
|
|
|
aString.Rebind(aString, 6);
|
|
|
|
} else if (StringBeginsWith(aString, NS_LITERAL_STRING("percent:"))) {
|
|
|
|
clipUnit = eClipUnit_Percent;
|
|
|
|
aString.Rebind(aString, 8);
|
|
|
|
} else {
|
|
|
|
clipUnit = eClipUnit_Pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and validate coordinates.
|
|
|
|
if (ParseInteger(aString, x) && x >= 0 &&
|
|
|
|
ParseCommaSeparator(aString) &&
|
|
|
|
ParseInteger(aString, y) && y >= 0 &&
|
|
|
|
ParseCommaSeparator(aString) &&
|
|
|
|
ParseInteger(aString, w) && w > 0 &&
|
|
|
|
ParseCommaSeparator(aString) &&
|
|
|
|
ParseInteger(aString, h) && h > 0 &&
|
|
|
|
aString.Length() == 0) {
|
|
|
|
|
|
|
|
// Reject invalid percentage coordinates.
|
|
|
|
if (clipUnit == eClipUnit_Percent &&
|
|
|
|
(x + w > 100 || y + h > 100)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-14 02:42:16 +04:00
|
|
|
mClip.emplace(x, y, w, h);
|
2012-12-21 05:04:50 +04:00
|
|
|
mClipUnit = clipUnit;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsMediaFragmentURIParser::Parse(nsACString& aRef)
|
|
|
|
{
|
|
|
|
// Create an array of possibly-invalid media fragments.
|
|
|
|
nsTArray< std::pair<nsCString, nsCString> > fragments;
|
|
|
|
nsCCharSeparatedTokenizer tokenizer(aRef, '&');
|
|
|
|
|
2011-08-25 03:42:23 +04:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsACString& nv = tokenizer.nextToken();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index = nv.FindChar('=');
|
2011-08-25 03:42:23 +04:00
|
|
|
if (index >= 0) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name;
|
|
|
|
nsAutoCString value;
|
2011-08-25 03:42:23 +04:00
|
|
|
NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name);
|
|
|
|
NS_UnescapeURL(Substring(nv, index + 1, nv.Length()),
|
|
|
|
esc_Ref | esc_AlwaysCopy, value);
|
2012-12-21 05:04:50 +04:00
|
|
|
fragments.AppendElement(make_pair(name, value));
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 05:04:50 +04:00
|
|
|
// Parse the media fragment values.
|
2016-10-19 03:05:29 +03:00
|
|
|
bool gotTemporal = false, gotSpatial = false;
|
2012-12-21 05:04:50 +04:00
|
|
|
for (int i = fragments.Length() - 1 ; i >= 0 ; --i) {
|
2016-10-19 03:05:29 +03:00
|
|
|
if (gotTemporal && gotSpatial) {
|
2012-12-21 05:04:50 +04:00
|
|
|
// We've got one of each possible type. No need to look at the rest.
|
|
|
|
break;
|
|
|
|
} else if (!gotTemporal && fragments[i].first.EqualsLiteral("t")) {
|
|
|
|
nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
|
|
|
|
gotTemporal = ParseNPT(nsDependentSubstring(value, 0));
|
|
|
|
} else if (!gotSpatial && fragments[i].first.EqualsLiteral("xywh")) {
|
|
|
|
nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
|
|
|
|
gotSpatial = ParseXYWH(nsDependentSubstring(value, 0));
|
2011-08-25 03:42:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-09 04:20:11 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|