Home | History | Annotate | Download | only in css
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "config.h"
      6 #include "core/css/MediaValuesCached.h"
      7 
      8 #include "core/css/CSSPrimitiveValue.h"
      9 #include "core/dom/Document.h"
     10 #include "core/frame/LocalFrame.h"
     11 #include "core/html/imports/HTMLImportsController.h"
     12 #include "core/rendering/RenderObject.h"
     13 
     14 namespace WebCore {
     15 
     16 PassRefPtr<MediaValues> MediaValuesCached::create()
     17 {
     18     return adoptRef(new MediaValuesCached());
     19 }
     20 
     21 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data)
     22 {
     23     return adoptRef(new MediaValuesCached(data));
     24 }
     25 
     26 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document)
     27 {
     28     Document* executingDocument = document.importsController() ? document.importsController()->master() : &document;
     29     ASSERT(executingDocument);
     30     return MediaValuesCached::create(executingDocument->frame());
     31 }
     32 
     33 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame)
     34 {
     35     // FIXME - Added an assert here so we can better understand when a frame is present without its view().
     36     ASSERT(!frame || frame->view());
     37     // FIXME - Because of crbug.com/371084, document()->renderView() may be null here.
     38     if (!frame || !frame->view() || !frame->document() || !frame->document()->renderView())
     39         return adoptRef(new MediaValuesCached());
     40     return adoptRef(new MediaValuesCached(frame));
     41 }
     42 
     43 MediaValuesCached::MediaValuesCached()
     44 {
     45 }
     46 
     47 MediaValuesCached::MediaValuesCached(LocalFrame* frame)
     48 {
     49     ASSERT(isMainThread());
     50     ASSERT(frame);
     51     // In case that frame is missing (e.g. for images that their document does not have a frame)
     52     // We simply leave the MediaValues object with the default MediaValuesCachedData values.
     53     m_data.viewportWidth = calculateViewportWidth(frame);
     54     m_data.viewportHeight = calculateViewportHeight(frame);
     55     m_data.deviceWidth = calculateDeviceWidth(frame);
     56     m_data.deviceHeight = calculateDeviceHeight(frame);
     57     m_data.devicePixelRatio = calculateDevicePixelRatio(frame);
     58     m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame);
     59     m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame);
     60     m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame);
     61     m_data.defaultFontSize = calculateDefaultFontSize(frame);
     62     m_data.threeDEnabled = calculateThreeDEnabled(frame);
     63     m_data.scanMediaType = calculateScanMediaType(frame);
     64     m_data.screenMediaType = calculateScreenMediaType(frame);
     65     m_data.printMediaType = calculatePrintMediaType(frame);
     66     m_data.strictMode = calculateStrictMode(frame);
     67 }
     68 
     69 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data)
     70     : m_data(data)
     71 {
     72 }
     73 
     74 PassRefPtr<MediaValues> MediaValuesCached::copy() const
     75 {
     76     return adoptRef(new MediaValuesCached(m_data));
     77 }
     78 
     79 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, int& result) const
     80 {
     81     return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
     82 }
     83 
     84 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, double& result) const
     85 {
     86     return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
     87 }
     88 
     89 bool MediaValuesCached::isSafeToSendToAnotherThread() const
     90 {
     91     return hasOneRef();
     92 }
     93 
     94 int MediaValuesCached::viewportWidth() const
     95 {
     96     return m_data.viewportWidth;
     97 }
     98 
     99 int MediaValuesCached::viewportHeight() const
    100 {
    101     return m_data.viewportHeight;
    102 }
    103 
    104 int MediaValuesCached::deviceWidth() const
    105 {
    106     return m_data.deviceWidth;
    107 }
    108 
    109 int MediaValuesCached::deviceHeight() const
    110 {
    111     return m_data.deviceHeight;
    112 }
    113 
    114 float MediaValuesCached::devicePixelRatio() const
    115 {
    116     return m_data.devicePixelRatio;
    117 }
    118 
    119 int MediaValuesCached::colorBitsPerComponent() const
    120 {
    121     return m_data.colorBitsPerComponent;
    122 }
    123 
    124 int MediaValuesCached::monochromeBitsPerComponent() const
    125 {
    126     return m_data.monochromeBitsPerComponent;
    127 }
    128 
    129 MediaValues::PointerDeviceType MediaValuesCached::pointer() const
    130 {
    131     return m_data.pointer;
    132 }
    133 
    134 bool MediaValuesCached::threeDEnabled() const
    135 {
    136     return m_data.threeDEnabled;
    137 }
    138 
    139 bool MediaValuesCached::scanMediaType() const
    140 {
    141     return m_data.scanMediaType;
    142 }
    143 
    144 bool MediaValuesCached::screenMediaType() const
    145 {
    146     return m_data.screenMediaType;
    147 }
    148 
    149 bool MediaValuesCached::printMediaType() const
    150 {
    151     return m_data.printMediaType;
    152 }
    153 
    154 bool MediaValuesCached::strictMode() const
    155 {
    156     return m_data.strictMode;
    157 }
    158 
    159 Document* MediaValuesCached::document() const
    160 {
    161     return 0;
    162 }
    163 
    164 bool MediaValuesCached::hasValues() const
    165 {
    166     return true;
    167 }
    168 
    169 } // namespace
    170