Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2012 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 "ContextFeaturesClientImpl.h"
     33 
     34 #include "WebDocument.h"
     35 #include "WebFrameImpl.h"
     36 #include "WebPermissionClient.h"
     37 #include "core/dom/Document.h"
     38 #include "platform/weborigin/SecurityOrigin.h"
     39 
     40 using namespace WebCore;
     41 
     42 namespace blink {
     43 
     44 class ContextFeaturesCache : public DocumentSupplement {
     45 public:
     46     class Entry {
     47     public:
     48         enum Value {
     49             IsEnabled,
     50             IsDisabled,
     51             NeedsRefresh
     52         };
     53 
     54         Entry()
     55             : m_value(NeedsRefresh)
     56             , m_defaultValue(false)
     57         { }
     58 
     59         bool isEnabled() const
     60         {
     61             ASSERT(m_value != NeedsRefresh);
     62             return m_value == IsEnabled;
     63         }
     64 
     65         void set(bool value, bool defaultValue)
     66         {
     67             m_value = value ? IsEnabled : IsDisabled;
     68             m_defaultValue = defaultValue;
     69         }
     70 
     71         bool needsRefresh(bool defaultValue) const
     72         {
     73             return m_value == NeedsRefresh || m_defaultValue != defaultValue;
     74         }
     75 
     76     private:
     77         Value m_value;
     78         bool m_defaultValue; // Needs to be traked as a part of the signature since it can be changed dynamically.
     79     };
     80 
     81     static const char* supplementName();
     82     static ContextFeaturesCache* from(Document*);
     83 
     84     Entry& entryFor(ContextFeatures::FeatureType type)
     85     {
     86         size_t index = static_cast<size_t>(type);
     87         ASSERT_WITH_SECURITY_IMPLICATION(index < ContextFeatures::FeatureTypeSize);
     88         return m_entries[index];
     89     }
     90 
     91     void validateAgainst(Document*);
     92 
     93 private:
     94     String m_domain;
     95     Entry m_entries[ContextFeatures::FeatureTypeSize];
     96 };
     97 
     98 const char* ContextFeaturesCache::supplementName()
     99 {
    100     return "ContextFeaturesCache";
    101 }
    102 
    103 ContextFeaturesCache* ContextFeaturesCache::from(Document* document)
    104 {
    105     ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(DocumentSupplement::from(document, supplementName()));
    106     if (!cache) {
    107         cache = new ContextFeaturesCache();
    108         DocumentSupplement::provideTo(document, supplementName(), adoptPtr(cache));
    109     }
    110 
    111     return cache;
    112 }
    113 
    114 void ContextFeaturesCache::validateAgainst(Document* document)
    115 {
    116     String currentDomain = document->securityOrigin()->domain();
    117     if (currentDomain == m_domain)
    118         return;
    119     m_domain = currentDomain;
    120     for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
    121         m_entries[i] = Entry();
    122 }
    123 
    124 bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
    125 {
    126     ContextFeaturesCache::Entry& cache = ContextFeaturesCache::from(document)->entryFor(type);
    127     if (cache.needsRefresh(defaultValue))
    128         cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue);
    129     return cache.isEnabled();
    130 }
    131 
    132 void ContextFeaturesClientImpl::urlDidChange(Document* document)
    133 {
    134     ContextFeaturesCache::from(document)->validateAgainst(document);
    135 }
    136 
    137 bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
    138 {
    139     if (!m_client)
    140         return defaultValue;
    141 
    142 #if defined(WEBPERMISSIONCLIENT_USES_FRAME_FOR_ALL_METHODS)
    143     WebFrameImpl* frame = WebFrameImpl::fromFrame(document->frame());
    144     if (!frame)
    145         return defaultValue;
    146 
    147     if (frame->permissionClient()) {
    148         switch (type) {
    149         case ContextFeatures::StyleScoped:
    150             return frame->permissionClient()->allowWebComponents(frame, defaultValue);
    151         case ContextFeatures::MutationEvents:
    152             return frame->permissionClient()->allowMutationEvents(frame, defaultValue);
    153         case ContextFeatures::PushState:
    154             return frame->permissionClient()->allowPushState(frame);
    155         default:
    156             return defaultValue;
    157         }
    158     }
    159 #endif
    160 
    161     switch (type) {
    162 #if defined(WEBPERMISSIONCLIENT_USES_FRAME_FOR_ALL_METHODS)
    163     case ContextFeatures::StyleScoped:
    164         return m_client->allowWebComponents(frame, defaultValue);
    165     case ContextFeatures::MutationEvents:
    166         return m_client->allowMutationEvents(frame, defaultValue);
    167     case ContextFeatures::PushState:
    168         return m_client->allowPushState(frame);
    169 #else
    170     case ContextFeatures::StyleScoped:
    171         return m_client->allowWebComponents(WebDocument(document), defaultValue);
    172     case ContextFeatures::MutationEvents:
    173         return m_client->allowMutationEvents(WebDocument(document), defaultValue);
    174     case ContextFeatures::PushState:
    175         return m_client->allowPushState(WebDocument(document));
    176 #endif
    177     default:
    178         return defaultValue;
    179     }
    180 }
    181 
    182 } // namespace blink
    183