Home | History | Annotate | Download | only in icon
      1 /*
      2  * Copyright (C) 2011 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 COMPUTER, 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 COMPUTER, 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 #ifndef IconDatabaseBase_h
     27 #define IconDatabaseBase_h
     28 
     29 #include "SharedBuffer.h"
     30 
     31 #include <wtf/Forward.h>
     32 #include <wtf/Noncopyable.h>
     33 #include <wtf/PassRefPtr.h>
     34 
     35 namespace WebCore {
     36 
     37 class DocumentLoader;
     38 class IconDatabaseClient;
     39 class Image;
     40 class IntSize;
     41 
     42 enum IconLoadDecision {
     43     IconLoadYes,
     44     IconLoadNo,
     45     IconLoadUnknown
     46 };
     47 
     48 class CallbackBase : public RefCounted<CallbackBase> {
     49 public:
     50     virtual ~CallbackBase()
     51     {
     52     }
     53 
     54     uint64_t callbackID() const { return m_callbackID; }
     55 
     56 protected:
     57     CallbackBase(void* context)
     58         : m_context(context)
     59         , m_callbackID(generateCallbackID())
     60     {
     61     }
     62 
     63     void* context() const { return m_context; }
     64 
     65 private:
     66     static uint64_t generateCallbackID()
     67     {
     68         static uint64_t uniqueCallbackID = 1;
     69         return uniqueCallbackID++;
     70     }
     71 
     72     void* m_context;
     73     uint64_t m_callbackID;
     74 };
     75 
     76 template<typename EnumType>
     77 class EnumCallback : public CallbackBase {
     78 public:
     79     typedef void (*CallbackFunction)(EnumType, void*);
     80 
     81     static PassRefPtr<EnumCallback> create(void* context, CallbackFunction callback)
     82     {
     83         return adoptRef(new EnumCallback(context, callback));
     84     }
     85 
     86     virtual ~EnumCallback()
     87     {
     88         ASSERT(!m_callback);
     89     }
     90 
     91     void performCallback(EnumType result)
     92     {
     93         if (!m_callback)
     94             return;
     95         m_callback(result, context());
     96         m_callback = 0;
     97     }
     98 
     99     void invalidate()
    100     {
    101         m_callback = 0;
    102     }
    103 
    104 private:
    105     EnumCallback(void* context, CallbackFunction callback)
    106         : CallbackBase(context)
    107         , m_callback(callback)
    108     {
    109         ASSERT(m_callback);
    110     }
    111 
    112     CallbackFunction m_callback;
    113 };
    114 
    115 template<typename ObjectType>
    116 class ObjectCallback : public CallbackBase {
    117 public:
    118     typedef void (*CallbackFunction)(ObjectType, void*);
    119 
    120     static PassRefPtr<ObjectCallback> create(void* context, CallbackFunction callback)
    121     {
    122         return adoptRef(new ObjectCallback(context, callback));
    123     }
    124 
    125     virtual ~ObjectCallback()
    126     {
    127         ASSERT(!m_callback);
    128     }
    129 
    130     void performCallback(ObjectType result)
    131     {
    132         if (!m_callback)
    133             return;
    134         m_callback(result, context());
    135         m_callback = 0;
    136     }
    137 
    138     void invalidate()
    139     {
    140         m_callback = 0;
    141     }
    142 
    143 private:
    144     ObjectCallback(void* context, CallbackFunction callback)
    145         : CallbackBase(context)
    146         , m_callback(callback)
    147     {
    148         ASSERT(m_callback);
    149     }
    150 
    151     CallbackFunction m_callback;
    152 };
    153 
    154 typedef EnumCallback<IconLoadDecision> IconLoadDecisionCallback;
    155 typedef ObjectCallback<SharedBuffer*> IconDataCallback;
    156 
    157 class IconDatabaseBase {
    158     WTF_MAKE_NONCOPYABLE(IconDatabaseBase);
    159 
    160 protected:
    161     IconDatabaseBase() { }
    162 
    163 public:
    164     virtual ~IconDatabaseBase() { }
    165 
    166     // Used internally by WebCore
    167     virtual bool isEnabled() const { return false; }
    168 
    169     virtual void retainIconForPageURL(const String&) { }
    170     virtual void releaseIconForPageURL(const String&) { }
    171 
    172     virtual void setIconURLForPageURL(const String&, const String&) { }
    173     virtual void setIconDataForIconURL(PassRefPtr<SharedBuffer>, const String&) { }
    174 
    175     // Synchronous calls used internally by WebCore.
    176     // Usage should be replaced by asynchronous calls.
    177     virtual String synchronousIconURLForPageURL(const String&);
    178     virtual bool synchronousIconDataKnownForIconURL(const String&) { return false; }
    179     virtual IconLoadDecision synchronousLoadDecisionForIconURL(const String&, DocumentLoader*) { return IconLoadNo; }
    180     virtual Image* synchronousIconForPageURL(const String&, const IntSize&) { return 0; }
    181 
    182     // Asynchronous calls we should use to replace the above when supported.
    183     virtual bool supportsAsynchronousMode() { return false; }
    184     virtual void loadDecisionForIconURL(const String&, PassRefPtr<IconLoadDecisionCallback>) { }
    185     virtual void iconDataForIconURL(const String&, PassRefPtr<IconDataCallback>) { }
    186 
    187 
    188     // Used within one or more WebKit ports.
    189     // We should try to remove these dependencies from the IconDatabaseBase class.
    190     virtual void setEnabled(bool) { }
    191 
    192     virtual Image* defaultIcon(const IntSize&) { return 0; }
    193 
    194     virtual size_t pageURLMappingCount() { return 0; }
    195     virtual size_t retainedPageURLCount() { return 0; }
    196     virtual size_t iconRecordCount() { return 0; }
    197     virtual size_t iconRecordCountWithData() { return 0; }
    198 
    199     virtual void importIconURLForPageURL(const String&, const String&) { }
    200     virtual void importIconDataForIconURL(PassRefPtr<SharedBuffer>, const String&) { }
    201     virtual bool shouldStopThreadActivity() const { return true; }
    202 
    203     virtual bool open(const String& directory, const String& filename);
    204     virtual void close() { }
    205     virtual void removeAllIcons() { }
    206 
    207     virtual void setPrivateBrowsingEnabled(bool) { }
    208     virtual void setClient(IconDatabaseClient*) { }
    209 
    210     virtual bool isOpen() const { return false; }
    211     virtual String databasePath() const;
    212 
    213 };
    214 
    215 // Functions to get/set the global icon database.
    216 IconDatabaseBase& iconDatabase();
    217 void setGlobalIconDatabase(IconDatabaseBase*);
    218 
    219 } // namespace WebCore
    220 
    221 #endif // IconDatabaseBase_h
    222