Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebHistoryItem.h"
     33 
     34 #include "WebSerializedScriptValue.h"
     35 #include "bindings/v8/SerializedScriptValue.h"
     36 #include "core/history/HistoryItem.h"
     37 #include "core/html/FormController.h"
     38 #include "core/platform/network/FormData.h"
     39 #include "public/platform/WebHTTPBody.h"
     40 #include "public/platform/WebPoint.h"
     41 #include "public/platform/WebString.h"
     42 #include "public/platform/WebVector.h"
     43 #include "weborigin/KURL.h"
     44 #include "wtf/text/StringHash.h"
     45 
     46 using namespace WebCore;
     47 
     48 namespace WebKit {
     49 namespace {
     50 
     51 void addReferencedFilePaths(HistoryItem* item, HashSet<String>& results)
     52 {
     53     const FormData* formData = item->formData();
     54     if (formData) {
     55         for (size_t i = 0; i < formData->elements().size(); ++i) {
     56             const FormDataElement& element = formData->elements()[i];
     57             if (element.m_type == FormDataElement::encodedFile)
     58                 results.add(element.m_filename);
     59         }
     60     }
     61 
     62     const Vector<String>& filePaths = FormController::getReferencedFilePaths(item->documentState());
     63     for (size_t i = 0; i < filePaths.size(); ++i)
     64         results.add(filePaths[i]);
     65 
     66     const HistoryItemVector& children = item->children();
     67     for (size_t i = 0; i < children.size(); ++i)
     68         addReferencedFilePaths(children[i].get(), results);
     69 }
     70 
     71 } // namespace
     72 
     73 void WebHistoryItem::initialize()
     74 {
     75     m_private = HistoryItem::create();
     76 }
     77 
     78 void WebHistoryItem::reset()
     79 {
     80     m_private.reset();
     81 }
     82 
     83 void WebHistoryItem::assign(const WebHistoryItem& other)
     84 {
     85     m_private = other.m_private;
     86 }
     87 
     88 WebString WebHistoryItem::urlString() const
     89 {
     90     return m_private->urlString();
     91 }
     92 
     93 void WebHistoryItem::setURLString(const WebString& url)
     94 {
     95     ensureMutable();
     96     m_private->setURLString(KURL(ParsedURLString, url).string());
     97 }
     98 
     99 WebString WebHistoryItem::originalURLString() const
    100 {
    101     return m_private->originalURLString();
    102 }
    103 
    104 void WebHistoryItem::setOriginalURLString(const WebString& originalURLString)
    105 {
    106     ensureMutable();
    107     m_private->setOriginalURLString(originalURLString);
    108 }
    109 
    110 WebString WebHistoryItem::referrer() const
    111 {
    112     return m_private->referrer();
    113 }
    114 
    115 void WebHistoryItem::setReferrer(const WebString& referrer)
    116 {
    117     ensureMutable();
    118     m_private->setReferrer(referrer);
    119 }
    120 
    121 WebString WebHistoryItem::target() const
    122 {
    123     return m_private->target();
    124 }
    125 
    126 void WebHistoryItem::setTarget(const WebString& target)
    127 {
    128     ensureMutable();
    129     m_private->setTarget(target);
    130 }
    131 
    132 WebString WebHistoryItem::parent() const
    133 {
    134     return m_private->parent();
    135 }
    136 
    137 void WebHistoryItem::setParent(const WebString& parent)
    138 {
    139     ensureMutable();
    140     m_private->setParent(parent);
    141 }
    142 
    143 WebString WebHistoryItem::title() const
    144 {
    145     return m_private->title();
    146 }
    147 
    148 void WebHistoryItem::setTitle(const WebString& title)
    149 {
    150     ensureMutable();
    151     m_private->setTitle(title);
    152 }
    153 
    154 WebString WebHistoryItem::alternateTitle() const
    155 {
    156     return m_private->alternateTitle();
    157 }
    158 
    159 void WebHistoryItem::setAlternateTitle(const WebString& alternateTitle)
    160 {
    161     ensureMutable();
    162     m_private->setAlternateTitle(alternateTitle);
    163 }
    164 
    165 double WebHistoryItem::lastVisitedTime() const
    166 {
    167     return m_private->lastVisitedTime();
    168 }
    169 
    170 void WebHistoryItem::setLastVisitedTime(double lastVisitedTime)
    171 {
    172     ensureMutable();
    173     // FIXME: setLastVisitedTime increments the visit count, so we have to
    174     // correct for that.  Instead, we should have a back-door to just mutate
    175     // the last visited time directly.
    176     int count = m_private->visitCount();
    177     m_private->setLastVisitedTime(lastVisitedTime);
    178     m_private->setVisitCount(count);
    179 }
    180 
    181 WebPoint WebHistoryItem::scrollOffset() const
    182 {
    183     return m_private->scrollPoint();
    184 }
    185 
    186 void WebHistoryItem::setScrollOffset(const WebPoint& scrollOffset)
    187 {
    188     ensureMutable();
    189     m_private->setScrollPoint(scrollOffset);
    190 }
    191 
    192 float WebHistoryItem::pageScaleFactor() const
    193 {
    194     return m_private->pageScaleFactor();
    195 }
    196 
    197 void WebHistoryItem::setPageScaleFactor(float scale)
    198 {
    199     ensureMutable();
    200     m_private->setPageScaleFactor(scale);
    201 }
    202 
    203 bool WebHistoryItem::isTargetItem() const
    204 {
    205     return m_private->isTargetItem();
    206 }
    207 
    208 void WebHistoryItem::setIsTargetItem(bool isTargetItem)
    209 {
    210     ensureMutable();
    211     m_private->setIsTargetItem(isTargetItem);
    212 }
    213 
    214 int WebHistoryItem::visitCount() const
    215 {
    216     return m_private->visitCount();
    217 }
    218 
    219 void WebHistoryItem::setVisitCount(int count)
    220 {
    221     ensureMutable();
    222     m_private->setVisitCount(count);
    223 }
    224 
    225 WebVector<WebString> WebHistoryItem::documentState() const
    226 {
    227     return m_private->documentState();
    228 }
    229 
    230 void WebHistoryItem::setDocumentState(const WebVector<WebString>& state)
    231 {
    232     ensureMutable();
    233     // FIXME: would be nice to avoid the intermediate copy
    234     Vector<String> ds;
    235     for (size_t i = 0; i < state.size(); ++i)
    236         ds.append(state[i]);
    237     m_private->setDocumentState(ds);
    238 }
    239 
    240 long long WebHistoryItem::itemSequenceNumber() const
    241 {
    242     return m_private->itemSequenceNumber();
    243 }
    244 
    245 void WebHistoryItem::setItemSequenceNumber(long long itemSequenceNumber)
    246 {
    247     ensureMutable();
    248     m_private->setItemSequenceNumber(itemSequenceNumber);
    249 }
    250 
    251 long long WebHistoryItem::documentSequenceNumber() const
    252 {
    253     return m_private->documentSequenceNumber();
    254 }
    255 
    256 void WebHistoryItem::setDocumentSequenceNumber(long long documentSequenceNumber)
    257 {
    258     ensureMutable();
    259     m_private->setDocumentSequenceNumber(documentSequenceNumber);
    260 }
    261 
    262 WebSerializedScriptValue WebHistoryItem::stateObject() const
    263 {
    264     return WebSerializedScriptValue(m_private->stateObject());
    265 }
    266 
    267 void WebHistoryItem::setStateObject(const WebSerializedScriptValue& object)
    268 {
    269     ensureMutable();
    270     m_private->setStateObject(object);
    271 }
    272 
    273 WebString WebHistoryItem::httpContentType() const
    274 {
    275     return m_private->formContentType();
    276 }
    277 
    278 void WebHistoryItem::setHTTPContentType(const WebString& httpContentType)
    279 {
    280     ensureMutable();
    281     m_private->setFormContentType(httpContentType);
    282 }
    283 
    284 WebHTTPBody WebHistoryItem::httpBody() const
    285 {
    286     return WebHTTPBody(m_private->formData());
    287 }
    288 
    289 void WebHistoryItem::setHTTPBody(const WebHTTPBody& httpBody)
    290 {
    291     ensureMutable();
    292     m_private->setFormData(httpBody);
    293 }
    294 
    295 WebVector<WebHistoryItem> WebHistoryItem::children() const
    296 {
    297     return m_private->children();
    298 }
    299 
    300 void WebHistoryItem::setChildren(const WebVector<WebHistoryItem>& items)
    301 {
    302     ensureMutable();
    303     m_private->clearChildren();
    304     for (size_t i = 0; i < items.size(); ++i)
    305         m_private->addChildItem(items[i]);
    306 }
    307 
    308 void WebHistoryItem::appendToChildren(const WebHistoryItem& item)
    309 {
    310     ensureMutable();
    311     m_private->addChildItem(item);
    312 }
    313 
    314 WebVector<WebString> WebHistoryItem::getReferencedFilePaths() const
    315 {
    316     HashSet<String> filePaths;
    317     addReferencedFilePaths(m_private.get(), filePaths);
    318 
    319     Vector<String> results;
    320     copyToVector(filePaths, results);
    321     return results;
    322 }
    323 
    324 WebHistoryItem::WebHistoryItem(const PassRefPtr<HistoryItem>& item)
    325     : m_private(item)
    326 {
    327 }
    328 
    329 WebHistoryItem& WebHistoryItem::operator=(const PassRefPtr<HistoryItem>& item)
    330 {
    331     m_private = item;
    332     return *this;
    333 }
    334 
    335 WebHistoryItem::operator PassRefPtr<HistoryItem>() const
    336 {
    337     return m_private.get();
    338 }
    339 
    340 void WebHistoryItem::ensureMutable()
    341 {
    342     if (!m_private->hasOneRef())
    343         m_private = m_private->copy();
    344 }
    345 
    346 } // namespace WebKit
    347