content/base/src/DOMStringList.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/DOMStringList.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; 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 +#ifndef mozilla_dom_DOMStringList_h
    1.10 +#define mozilla_dom_DOMStringList_h
    1.11 +
    1.12 +#include "nsISupports.h"
    1.13 +#include "nsTArray.h"
    1.14 +#include "nsWrapperCache.h"
    1.15 +#include "nsString.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +namespace dom {
    1.19 +
    1.20 +class DOMStringList : public nsISupports,
    1.21 +                      public nsWrapperCache
    1.22 +{
    1.23 +public:
    1.24 +  DOMStringList()
    1.25 +  {
    1.26 +    SetIsDOMBinding();
    1.27 +  }
    1.28 +  virtual ~DOMStringList();
    1.29 +
    1.30 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.31 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMStringList)
    1.32 +
    1.33 +  virtual JSObject* WrapObject(JSContext* aCx);
    1.34 +  nsISupports* GetParentObject()
    1.35 +  {
    1.36 +    return nullptr;
    1.37 +  }
    1.38 +
    1.39 +  void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult)
    1.40 +  {
    1.41 +    EnsureFresh();
    1.42 +    if (aIndex < mNames.Length()) {
    1.43 +      aFound = true;
    1.44 +      aResult = mNames[aIndex];
    1.45 +    } else {
    1.46 +      aFound = false;
    1.47 +    }
    1.48 +  }
    1.49 +
    1.50 +  void Item(uint32_t aIndex, nsAString& aResult)
    1.51 +  {
    1.52 +    EnsureFresh();
    1.53 +    if (aIndex < mNames.Length()) {
    1.54 +      aResult = mNames[aIndex];
    1.55 +    } else {
    1.56 +      aResult.SetIsVoid(true);
    1.57 +    }
    1.58 +  }
    1.59 +
    1.60 +  uint32_t Length()
    1.61 +  {
    1.62 +    EnsureFresh();
    1.63 +    return mNames.Length();
    1.64 +  }
    1.65 +
    1.66 +  bool Contains(const nsAString& aString)
    1.67 +  {
    1.68 +    EnsureFresh();
    1.69 +    return mNames.Contains(aString);
    1.70 +  }
    1.71 +
    1.72 +  bool Add(const nsAString& aName)
    1.73 +  {
    1.74 +    // XXXbz mNames should really be a fallible array; otherwise this
    1.75 +    // return value is meaningless.
    1.76 +    return mNames.AppendElement(aName) != nullptr;
    1.77 +  }
    1.78 +
    1.79 +  void Clear()
    1.80 +  {
    1.81 +    mNames.Clear();
    1.82 +  }
    1.83 +
    1.84 +  nsTArray<nsString>& StringArray()
    1.85 +  {
    1.86 +    return mNames;
    1.87 +  }
    1.88 +
    1.89 +  void CopyList(nsTArray<nsString>& aNames)
    1.90 +  {
    1.91 +    aNames = mNames;
    1.92 +  }
    1.93 +
    1.94 +protected:
    1.95 +  // A method that subclasses can override to modify mNames as needed
    1.96 +  // before we index into it or return its length or whatnot.
    1.97 +  virtual void EnsureFresh()
    1.98 +  {
    1.99 +  }
   1.100 +
   1.101 +  // XXXbz we really want this to be a fallible array, but we end up passing it
   1.102 +  // to consumers who declare themselves as taking and nsTArray.  :(
   1.103 +  nsTArray<nsString> mNames;
   1.104 +};
   1.105 +
   1.106 +} // namespace dom
   1.107 +} // namespace mozilla
   1.108 +
   1.109 +#endif /* mozilla_dom_DOMStringList_h */

mercurial