Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (c) 2010, 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 #ifndef PlatformBridge_h
     32 #define PlatformBridge_h
     33 
     34 #if ENABLE(WEB_AUDIO)
     35 #include "AudioBus.h"
     36 #endif
     37 
     38 #include "FileSystem.h"
     39 #include "ImageSource.h"
     40 #include "LinkHash.h"
     41 #include "PassRefPtr.h"
     42 #include "PasteboardPrivate.h"
     43 #include "PluginData.h"
     44 
     45 #include <wtf/Forward.h>
     46 #include <wtf/HashSet.h>
     47 #include <wtf/Vector.h>
     48 
     49 typedef struct NPObject NPObject;
     50 typedef struct _NPP NPP_t;
     51 typedef NPP_t* NPP;
     52 
     53 #if OS(DARWIN)
     54 typedef struct CGFont* CGFontRef;
     55 typedef uintptr_t ATSFontContainerRef;
     56 #ifdef __OBJC__
     57 @class NSFont;
     58 #else
     59 class NSFont;
     60 #endif
     61 #endif // OS(DARWIN)
     62 
     63 #if OS(WINDOWS)
     64 typedef struct HFONT__* HFONT;
     65 #endif
     66 
     67 namespace WebCore {
     68 
     69 class ClipboardData;
     70 class Color;
     71 class Cursor;
     72 class Document;
     73 class Frame;
     74 class GeolocationServiceBridge;
     75 class GeolocationServiceChromium;
     76 class GraphicsContext;
     77 class Image;
     78 class IDBFactoryBackendInterface;
     79 class IDBKey;
     80 class IntRect;
     81 class KURL;
     82 class SerializedScriptValue;
     83 class Widget;
     84 
     85 struct Cookie;
     86 struct FontRenderStyle;
     87 
     88 // An interface to the embedding layer, which has the ability to answer
     89 // questions about the system and so on...
     90 
     91 class PlatformBridge {
     92 public:
     93     // Cache --------------------------------------------------------------
     94     static void cacheMetadata(const KURL&, double responseTime, const Vector<char>&);
     95 
     96     // Clipboard ----------------------------------------------------------
     97     static bool clipboardIsFormatAvailable(PasteboardPrivate::ClipboardFormat, PasteboardPrivate::ClipboardBuffer);
     98 
     99     static String clipboardReadPlainText(PasteboardPrivate::ClipboardBuffer);
    100     static void clipboardReadHTML(PasteboardPrivate::ClipboardBuffer, String*, KURL*);
    101     static PassRefPtr<SharedBuffer> clipboardReadImage(PasteboardPrivate::ClipboardBuffer);
    102 
    103     // Only the clipboardRead functions take a buffer argument because
    104     // Chromium currently uses a different technique to write to alternate
    105     // clipboard buffers.
    106     static void clipboardWriteSelection(const String&, const KURL&, const String&, bool);
    107     static void clipboardWritePlainText(const String&);
    108     static void clipboardWriteURL(const KURL&, const String&);
    109     static void clipboardWriteImage(NativeImagePtr, const KURL&, const String&);
    110     static void clipboardWriteData(const String& type, const String& data, const String& metadata);
    111 
    112     // Interface for handling copy and paste, drag and drop, and selection copy.
    113     static HashSet<String> clipboardReadAvailableTypes(PasteboardPrivate::ClipboardBuffer, bool* containsFilenames);
    114     static bool clipboardReadData(PasteboardPrivate::ClipboardBuffer, const String& type, String& data, String& metadata);
    115     static Vector<String> clipboardReadFilenames(PasteboardPrivate::ClipboardBuffer);
    116 
    117     // Cookies ------------------------------------------------------------
    118     static void setCookies(const Document*, const KURL&, const String& value);
    119     static String cookies(const Document*, const KURL&);
    120     static String cookieRequestHeaderFieldValue(const Document*, const KURL&);
    121     static bool rawCookies(const Document*, const KURL&, Vector<Cookie>&);
    122     static void deleteCookie(const Document*, const KURL&, const String& cookieName);
    123     static bool cookiesEnabled(const Document*);
    124 
    125     // DNS ----------------------------------------------------------------
    126     static void prefetchDNS(const String& hostname);
    127 
    128     // File ---------------------------------------------------------------
    129     static void revealFolderInOS(const String&);
    130     static bool fileExists(const String&);
    131     static bool deleteFile(const String&);
    132     static bool deleteEmptyDirectory(const String&);
    133     static bool getFileSize(const String&, long long& result);
    134     static bool getFileModificationTime(const String&, time_t& result);
    135     static String directoryName(const String& path);
    136     static String pathByAppendingComponent(const String& path, const String& component);
    137     static bool makeAllDirectories(const String& path);
    138     static String getAbsolutePath(const String&);
    139     static bool isDirectory(const String&);
    140     static KURL filePathToURL(const String&);
    141     static PlatformFileHandle openFile(const String& path, FileOpenMode);
    142     static void closeFile(PlatformFileHandle&);
    143     static long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
    144     static bool truncateFile(PlatformFileHandle, long long offset);
    145     static int readFromFile(PlatformFileHandle, char* data, int length);
    146     static int writeToFile(PlatformFileHandle, const char* data, int length);
    147 
    148     // Font ---------------------------------------------------------------
    149 #if OS(WINDOWS)
    150     static bool ensureFontLoaded(HFONT);
    151 #endif
    152 #if OS(LINUX) || OS(FREEBSD)
    153     static void getRenderStyleForStrike(const char* family, int sizeAndStyle, FontRenderStyle* result);
    154     static String getFontFamilyForCharacters(const UChar*, size_t numCharacters, const char* preferredLocale);
    155 #endif
    156 #if OS(DARWIN)
    157     static bool loadFont(NSFont* srcFont, ATSFontContainerRef* out);
    158 #endif
    159 
    160     // Forms --------------------------------------------------------------
    161     static void notifyFormStateChanged(const Document*);
    162 
    163     // Databases ----------------------------------------------------------
    164     // Returns a handle to the DB file and ooptionally a handle to its containing directory
    165     static PlatformFileHandle databaseOpenFile(const String& vfsFleName, int desiredFlags);
    166     // Returns a SQLite code (SQLITE_OK = 0, on success)
    167     static int databaseDeleteFile(const String& vfsFileName, bool syncDir = false);
    168     // Returns the attributes of the DB file
    169     static long databaseGetFileAttributes(const String& vfsFileName);
    170     // Returns the size of the DB file
    171     static long long databaseGetFileSize(const String& vfsFileName);
    172 
    173     // IndexedDB ----------------------------------------------------------
    174     static PassRefPtr<IDBFactoryBackendInterface> idbFactory();
    175     // Extracts keyPath from values and returns the corresponding keys.
    176     static void createIDBKeysFromSerializedValuesAndKeyPath(const Vector<RefPtr<SerializedScriptValue> >& values, const String& keyPath, Vector<RefPtr<IDBKey> >& keys);
    177     // Injects key via keyPath into value. Returns true on success.
    178     static PassRefPtr<SerializedScriptValue> injectIDBKeyIntoSerializedValue(PassRefPtr<IDBKey>, PassRefPtr<SerializedScriptValue>, const String& keyPath);
    179 
    180     // JavaScript ---------------------------------------------------------
    181     static void notifyJSOutOfMemory(Frame*);
    182     static bool allowScriptDespiteSettings(const KURL& documentURL);
    183 
    184     // Keygen -------------------------------------------------------------
    185     static String signedPublicKeyAndChallengeString(unsigned keySizeIndex, const String& challenge, const KURL&);
    186 
    187     // Language -----------------------------------------------------------
    188     static String computedDefaultLanguage();
    189 
    190     // LayoutTestMode -----------------------------------------------------
    191     static bool layoutTestMode();
    192 
    193     // Memory -------------------------------------------------------------
    194     // Returns the current space allocated for the pagefile, in MB.
    195     // That is committed size for Windows and virtual memory size for POSIX
    196     static int memoryUsageMB();
    197 
    198     // Same as above, but always returns actual value, without any caches.
    199     static int actualMemoryUsageMB();
    200 
    201     // MimeType -----------------------------------------------------------
    202     static bool isSupportedImageMIMEType(const String& mimeType);
    203     static bool isSupportedJavaScriptMIMEType(const String& mimeType);
    204     static bool isSupportedNonImageMIMEType(const String& mimeType);
    205     static String mimeTypeForExtension(const String& fileExtension);
    206     static String mimeTypeFromFile(const String& filePath);
    207     static String preferredExtensionForMIMEType(const String& mimeType);
    208 
    209     // Plugin -------------------------------------------------------------
    210     static bool plugins(bool refresh, Vector<PluginInfo>*);
    211     static NPObject* pluginScriptableObject(Widget*);
    212     static bool popupsAllowed(NPP);
    213 
    214     // Resources ----------------------------------------------------------
    215     static PassRefPtr<Image> loadPlatformImageResource(const char* name);
    216 
    217 #if ENABLE(WEB_AUDIO)
    218     static PassOwnPtr<AudioBus> loadPlatformAudioResource(const char* name, double sampleRate);
    219     static PassOwnPtr<AudioBus> decodeAudioFileData(const char* data, size_t, double sampleRate);
    220 #endif
    221 
    222     // Sandbox ------------------------------------------------------------
    223     static bool sandboxEnabled();
    224 
    225     // Screen -------------------------------------------------------------
    226     static int screenDepth(Widget*);
    227     static int screenDepthPerComponent(Widget*);
    228     static bool screenIsMonochrome(Widget*);
    229     static IntRect screenRect(Widget*);
    230     static IntRect screenAvailableRect(Widget*);
    231 
    232     // SharedTimers -------------------------------------------------------
    233     static void setSharedTimerFiredFunction(void (*func)());
    234     static void setSharedTimerFireTime(double);
    235     static void stopSharedTimer();
    236 
    237     // StatsCounters ------------------------------------------------------
    238     static void decrementStatsCounter(const char* name);
    239     static void incrementStatsCounter(const char* name);
    240     static void histogramCustomCounts(const char* name, int sample, int min, int max, int bucketCount);
    241     static void histogramEnumeration(const char* name, int sample, int boundaryValue);
    242 
    243     // Sudden Termination
    244     static void suddenTerminationChanged(bool enabled);
    245 
    246     // SystemTime ---------------------------------------------------------
    247     static double currentTime();
    248 
    249     // Theming ------------------------------------------------------------
    250 #if OS(WINDOWS)
    251     static void paintButton(
    252         GraphicsContext*, int part, int state, int classicState, const IntRect&);
    253     static void paintMenuList(
    254         GraphicsContext*, int part, int state, int classicState, const IntRect&);
    255     static void paintScrollbarArrow(
    256         GraphicsContext*, int state, int classicState, const IntRect&);
    257     static void paintScrollbarThumb(
    258         GraphicsContext*, int part, int state, int classicState, const IntRect&);
    259     static void paintScrollbarTrack(
    260         GraphicsContext*, int part, int state, int classicState, const IntRect&, const IntRect& alignRect);
    261     static void paintSpinButton(
    262         GraphicsContext*, int part, int state, int classicState, const IntRect&);
    263     static void paintTextField(
    264         GraphicsContext*, int part, int state, int classicState, const IntRect&, const Color&, bool fillContentArea, bool drawEdges);
    265     static void paintTrackbar(
    266         GraphicsContext*, int part, int state, int classicState, const IntRect&);
    267     static void paintProgressBar(
    268         GraphicsContext*, const IntRect& barRect, const IntRect& valueRect, bool determinate, double animatedSeconds);
    269 #elif OS(LINUX)
    270     // The UI part which is being accessed.
    271     enum ThemePart {
    272         // ScrollbarTheme parts
    273         PartScrollbarDownArrow,
    274         PartScrollbarLeftArrow,
    275         PartScrollbarRightArrow,
    276         PartScrollbarUpArrow,
    277         PartScrollbarHorizontalThumb,
    278         PartScrollbarVerticalThumb,
    279         PartScrollbarHorizontalTrack,
    280         PartScrollbarVerticalTrack,
    281 
    282         // RenderTheme parts
    283         PartCheckbox,
    284         PartRadio,
    285         PartButton,
    286         PartTextField,
    287         PartMenuList,
    288         PartSliderTrack,
    289         PartSliderThumb,
    290         PartInnerSpinButton,
    291         PartProgressBar
    292     };
    293 
    294     // The current state of the associated Part.
    295     enum ThemePaintState {
    296         StateDisabled,
    297         StateHover,
    298         StateNormal,
    299         StatePressed
    300     };
    301 
    302     struct ScrollbarTrackExtraParams {
    303         // The bounds of the entire track, as opposed to the part being painted.
    304         int trackX;
    305         int trackY;
    306         int trackWidth;
    307         int trackHeight;
    308     };
    309 
    310     struct ButtonExtraParams {
    311         bool checked;
    312         bool indeterminate; // Whether the button state is indeterminate.
    313         bool isDefault; // Whether the button is default button.
    314         bool hasBorder;
    315         unsigned backgroundColor;
    316     };
    317 
    318     struct TextFieldExtraParams {
    319         bool isTextArea;
    320         bool isListbox;
    321         unsigned backgroundColor;
    322     };
    323 
    324     struct MenuListExtraParams {
    325         bool hasBorder;
    326         bool hasBorderRadius;
    327         int arrowX;
    328         int arrowY;
    329         unsigned backgroundColor;
    330     };
    331 
    332     struct SliderExtraParams {
    333         bool vertical;
    334         bool inDrag;
    335     };
    336 
    337     struct InnerSpinButtonExtraParams {
    338         bool spinUp;
    339         bool readOnly;
    340     };
    341 
    342     struct ProgressBarExtraParams {
    343         bool determinate;
    344         int valueRectX;
    345         int valueRectY;
    346         int valueRectWidth;
    347         int valueRectHeight;
    348     };
    349 
    350     union ThemePaintExtraParams {
    351         ScrollbarTrackExtraParams scrollbarTrack;
    352         ButtonExtraParams button;
    353         TextFieldExtraParams textField;
    354         MenuListExtraParams menuList;
    355         SliderExtraParams slider;
    356         InnerSpinButtonExtraParams innerSpin;
    357         ProgressBarExtraParams progressBar;
    358     };
    359 
    360     // Gets the size of the given theme part. For variable sized items
    361     // like vertical scrollbar thumbs, the width will be the required width of
    362     // the track while the height will be the minimum height.
    363     static IntSize getThemePartSize(ThemePart);
    364     // Paint the given the given theme part.
    365     static void paintThemePart(GraphicsContext*, ThemePart, ThemePaintState, const IntRect&, const ThemePaintExtraParams*);
    366 #elif OS(DARWIN)
    367     enum ThemePaintState {
    368         StateDisabled,
    369         StateInactive,
    370         StateActive,
    371         StatePressed,
    372     };
    373 
    374     enum ThemePaintSize {
    375         SizeRegular,
    376         SizeSmall,
    377     };
    378 
    379     enum ThemePaintScrollbarOrientation {
    380         ScrollbarOrientationHorizontal,
    381         ScrollbarOrientationVertical,
    382     };
    383 
    384     enum ThemePaintScrollbarParent {
    385         ScrollbarParentScrollView,
    386         ScrollbarParentRenderLayer,
    387     };
    388 
    389     struct ThemePaintScrollbarInfo {
    390         ThemePaintScrollbarOrientation orientation;
    391         ThemePaintScrollbarParent parent;
    392         int maxValue;
    393         int currentValue;
    394         int visibleSize;
    395         int totalSize;
    396     };
    397 
    398     static void paintScrollbarThumb(GraphicsContext*, ThemePaintState, ThemePaintSize, const IntRect&, const ThemePaintScrollbarInfo&);
    399 #endif
    400 
    401     // Trace Event --------------------------------------------------------
    402     static void traceEventBegin(const char* name, void*, const char* extra);
    403     static void traceEventEnd(const char* name, void*, const char* extra);
    404 
    405     // Visited links ------------------------------------------------------
    406     static LinkHash visitedLinkHash(const UChar* url, unsigned length);
    407     static LinkHash visitedLinkHash(const KURL& base, const AtomicString& attributeURL);
    408     static bool isLinkVisited(LinkHash);
    409 };
    410 
    411 } // namespace WebCore
    412 
    413 #endif
    414