Home | History | Annotate | Download | only in cf
      1 /*
      2  * Copyright (C) 2009 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef BinaryPropertyList_h
     27 #define BinaryPropertyList_h
     28 
     29 #include <CoreFoundation/CoreFoundation.h>
     30 
     31 #include <wtf/Forward.h>
     32 #include <wtf/Vector.h>
     33 
     34 namespace WebCore {
     35 
     36 // Writes a limited subset of binary property lists.
     37 // Covers only what's needed for writing browser history as of this writing.
     38 class BinaryPropertyListObjectStream {
     39 public:
     40     // Call writeBooleanTrue to write the boolean true value.
     41     // A single shared object will be used in the serialized list.
     42     virtual void writeBooleanTrue() = 0;
     43 
     44     // Call writeInteger to write an integer value.
     45     // A single shared object will be used for each integer in the serialized list.
     46     virtual void writeInteger(int) = 0;
     47 
     48     // Call writeString to write a string value.
     49     // A single shared object will be used for each string in the serialized list.
     50     virtual void writeString(const String&) = 0;
     51 
     52     // Call writeUniqueString instead of writeString when it's unlikely the
     53     // string will be written twice in the same property list; this saves hash
     54     // table overhead for such strings. A separate object will be used for each
     55     // of these strings in the serialized list.
     56     virtual void writeUniqueString(const String&) = 0;
     57     virtual void writeUniqueString(const char*) = 0;
     58 
     59     // Call writeIntegerArray instead of writeArrayStart/writeArrayEnd for
     60     // arrays entirely composed of integers. A single shared object will be used
     61     // for each identical array in the serialized list. Warning: The integer
     62     // pointer must remain valid until the writeBinaryPropertyList function
     63     // returns, because these lists are put into a hash table without copying
     64     // them -- that's OK if the client already has a Vector<int>.
     65     virtual void writeIntegerArray(const int*, size_t) = 0;
     66 
     67     // After calling writeArrayStart, write array elements.
     68     // Then call writeArrayEnd, passing in the result from writeArrayStart.
     69     // A separate object will be used for each of these arrays in the serialized list.
     70     virtual size_t writeArrayStart() = 0;
     71     virtual void writeArrayEnd(size_t resultFromWriteArrayStart) = 0;
     72 
     73     // After calling writeDictionaryStart, write all keys, then all values.
     74     // Then call writeDictionaryEnd, passing in the result from writeDictionaryStart.
     75     // A separate object will be used for each dictionary in the serialized list.
     76     virtual size_t writeDictionaryStart() = 0;
     77     virtual void writeDictionaryEnd(size_t resultFromWriteDictionaryStart) = 0;
     78 
     79 protected:
     80     virtual ~BinaryPropertyListObjectStream() { }
     81 };
     82 
     83 class BinaryPropertyListWriter {
     84 public:
     85     // Calls writeObjects once to prepare for writing and determine how big a
     86     // buffer is required. Then calls buffer to get the appropriately-sized
     87     // buffer, then calls writeObjects a second time and writes the property list.
     88     void writePropertyList();
     89 
     90 protected:
     91     virtual ~BinaryPropertyListWriter() { }
     92 
     93 private:
     94     // Called by writePropertyList.
     95     // Must call the object stream functions for the objects to be written
     96     // into the property list.
     97     virtual void writeObjects(BinaryPropertyListObjectStream&) = 0;
     98 
     99     // Called by writePropertyList.
    100     // Returns the buffer that the writer should write into.
    101     virtual UInt8* buffer(size_t) = 0;
    102 
    103     friend class BinaryPropertyListPlan;
    104     friend class BinaryPropertyListSerializer;
    105 };
    106 
    107 }
    108 
    109 #endif
    110