Home | History | Annotate | Download | only in appcache
      1 /*
      2  * Copyright (C) 2008, 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 #include "config.h"
     27 #include "core/loader/appcache/DOMApplicationCache.h"
     28 
     29 #include "bindings/v8/ExceptionState.h"
     30 #include "core/dom/Document.h"
     31 #include "core/dom/EventListener.h"
     32 #include "core/dom/EventNames.h"
     33 #include "core/dom/ExceptionCode.h"
     34 #include "core/loader/DocumentLoader.h"
     35 #include "core/loader/FrameLoader.h"
     36 #include "core/loader/appcache/ApplicationCacheHost.h"
     37 #include "core/page/Frame.h"
     38 
     39 namespace WebCore {
     40 
     41 DOMApplicationCache::DOMApplicationCache(Frame* frame)
     42     : DOMWindowProperty(frame)
     43 {
     44     ScriptWrappable::init(this);
     45     ApplicationCacheHost* cacheHost = applicationCacheHost();
     46     if (cacheHost)
     47         cacheHost->setDOMApplicationCache(this);
     48 }
     49 
     50 void DOMApplicationCache::willDestroyGlobalObjectInFrame()
     51 {
     52     if (ApplicationCacheHost* cacheHost = applicationCacheHost())
     53         cacheHost->setDOMApplicationCache(0);
     54     DOMWindowProperty::willDestroyGlobalObjectInFrame();
     55 }
     56 
     57 ApplicationCacheHost* DOMApplicationCache::applicationCacheHost() const
     58 {
     59     if (!m_frame || !m_frame->loader()->documentLoader())
     60         return 0;
     61     return m_frame->loader()->documentLoader()->applicationCacheHost();
     62 }
     63 
     64 unsigned short DOMApplicationCache::status() const
     65 {
     66     ApplicationCacheHost* cacheHost = applicationCacheHost();
     67     if (!cacheHost)
     68         return ApplicationCacheHost::UNCACHED;
     69     return cacheHost->status();
     70 }
     71 
     72 void DOMApplicationCache::update(ExceptionState& es)
     73 {
     74     ApplicationCacheHost* cacheHost = applicationCacheHost();
     75     if (!cacheHost || !cacheHost->update())
     76         es.throwDOMException(InvalidStateError);
     77 }
     78 
     79 void DOMApplicationCache::swapCache(ExceptionState& es)
     80 {
     81     ApplicationCacheHost* cacheHost = applicationCacheHost();
     82     if (!cacheHost || !cacheHost->swapCache())
     83         es.throwDOMException(InvalidStateError);
     84 }
     85 
     86 void DOMApplicationCache::abort()
     87 {
     88     ApplicationCacheHost* cacheHost = applicationCacheHost();
     89     if (cacheHost)
     90         cacheHost->abort();
     91 }
     92 
     93 const AtomicString& DOMApplicationCache::interfaceName() const
     94 {
     95     return eventNames().interfaceForDOMApplicationCache;
     96 }
     97 
     98 ScriptExecutionContext* DOMApplicationCache::scriptExecutionContext() const
     99 {
    100     if (m_frame)
    101         return m_frame->document();
    102     return 0;
    103 }
    104 
    105 const AtomicString& DOMApplicationCache::toEventType(ApplicationCacheHost::EventID id)
    106 {
    107     switch (id) {
    108     case ApplicationCacheHost::CHECKING_EVENT:
    109         return eventNames().checkingEvent;
    110     case ApplicationCacheHost::ERROR_EVENT:
    111         return eventNames().errorEvent;
    112     case ApplicationCacheHost::NOUPDATE_EVENT:
    113         return eventNames().noupdateEvent;
    114     case ApplicationCacheHost::DOWNLOADING_EVENT:
    115         return eventNames().downloadingEvent;
    116     case ApplicationCacheHost::PROGRESS_EVENT:
    117         return eventNames().progressEvent;
    118     case ApplicationCacheHost::UPDATEREADY_EVENT:
    119         return eventNames().updatereadyEvent;
    120     case ApplicationCacheHost::CACHED_EVENT:
    121         return eventNames().cachedEvent;
    122     case ApplicationCacheHost::OBSOLETE_EVENT:
    123         return eventNames().obsoleteEvent;
    124     }
    125     ASSERT_NOT_REACHED();
    126     return eventNames().errorEvent;
    127 }
    128 
    129 EventTargetData* DOMApplicationCache::eventTargetData()
    130 {
    131     return &m_eventTargetData;
    132 }
    133 
    134 EventTargetData* DOMApplicationCache::ensureEventTargetData()
    135 {
    136     return &m_eventTargetData;
    137 }
    138 
    139 } // namespace WebCore
    140