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: ~CallbackBase()50 virtual ~CallbackBase() 51 { 52 } 53 callbackID()54 uint64_t callbackID() const { return m_callbackID; } 55 56 protected: CallbackBase(void * context)57 CallbackBase(void* context) 58 : m_context(context) 59 , m_callbackID(generateCallbackID()) 60 { 61 } 62 context()63 void* context() const { return m_context; } 64 65 private: generateCallbackID()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 create(void * context,CallbackFunction callback)81 static PassRefPtr<EnumCallback> create(void* context, CallbackFunction callback) 82 { 83 return adoptRef(new EnumCallback(context, callback)); 84 } 85 ~EnumCallback()86 virtual ~EnumCallback() 87 { 88 ASSERT(!m_callback); 89 } 90 performCallback(EnumType result)91 void performCallback(EnumType result) 92 { 93 if (!m_callback) 94 return; 95 m_callback(result, context()); 96 m_callback = 0; 97 } 98 invalidate()99 void invalidate() 100 { 101 m_callback = 0; 102 } 103 104 private: EnumCallback(void * context,CallbackFunction callback)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 create(void * context,CallbackFunction callback)120 static PassRefPtr<ObjectCallback> create(void* context, CallbackFunction callback) 121 { 122 return adoptRef(new ObjectCallback(context, callback)); 123 } 124 ~ObjectCallback()125 virtual ~ObjectCallback() 126 { 127 ASSERT(!m_callback); 128 } 129 performCallback(ObjectType result)130 void performCallback(ObjectType result) 131 { 132 if (!m_callback) 133 return; 134 m_callback(result, context()); 135 m_callback = 0; 136 } 137 invalidate()138 void invalidate() 139 { 140 m_callback = 0; 141 } 142 143 private: ObjectCallback(void * context,CallbackFunction callback)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: IconDatabaseBase()161 IconDatabaseBase() { } 162 163 public: ~IconDatabaseBase()164 virtual ~IconDatabaseBase() { } 165 166 // Used internally by WebCore isEnabled()167 virtual bool isEnabled() const { return false; } 168 retainIconForPageURL(const String &)169 virtual void retainIconForPageURL(const String&) { } releaseIconForPageURL(const String &)170 virtual void releaseIconForPageURL(const String&) { } 171 setIconURLForPageURL(const String &,const String &)172 virtual void setIconURLForPageURL(const String&, const String&) { } setIconDataForIconURL(PassRefPtr<SharedBuffer>,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&); synchronousIconDataKnownForIconURL(const String &)178 virtual bool synchronousIconDataKnownForIconURL(const String&) { return false; } synchronousLoadDecisionForIconURL(const String &,DocumentLoader *)179 virtual IconLoadDecision synchronousLoadDecisionForIconURL(const String&, DocumentLoader*) { return IconLoadNo; } synchronousIconForPageURL(const String &,const IntSize &)180 virtual Image* synchronousIconForPageURL(const String&, const IntSize&) { return 0; } 181 182 // Asynchronous calls we should use to replace the above when supported. supportsAsynchronousMode()183 virtual bool supportsAsynchronousMode() { return false; } loadDecisionForIconURL(const String &,PassRefPtr<IconLoadDecisionCallback>)184 virtual void loadDecisionForIconURL(const String&, PassRefPtr<IconLoadDecisionCallback>) { } iconDataForIconURL(const String &,PassRefPtr<IconDataCallback>)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. setEnabled(bool)190 virtual void setEnabled(bool) { } 191 defaultIcon(const IntSize &)192 virtual Image* defaultIcon(const IntSize&) { return 0; } 193 pageURLMappingCount()194 virtual size_t pageURLMappingCount() { return 0; } retainedPageURLCount()195 virtual size_t retainedPageURLCount() { return 0; } iconRecordCount()196 virtual size_t iconRecordCount() { return 0; } iconRecordCountWithData()197 virtual size_t iconRecordCountWithData() { return 0; } 198 importIconURLForPageURL(const String &,const String &)199 virtual void importIconURLForPageURL(const String&, const String&) { } importIconDataForIconURL(PassRefPtr<SharedBuffer>,const String &)200 virtual void importIconDataForIconURL(PassRefPtr<SharedBuffer>, const String&) { } shouldStopThreadActivity()201 virtual bool shouldStopThreadActivity() const { return true; } 202 203 virtual bool open(const String& directory, const String& filename); close()204 virtual void close() { } removeAllIcons()205 virtual void removeAllIcons() { } 206 setPrivateBrowsingEnabled(bool)207 virtual void setPrivateBrowsingEnabled(bool) { } setClient(IconDatabaseClient *)208 virtual void setClient(IconDatabaseClient*) { } 209 isOpen()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