OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2009, 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 ApplicationCacheBridgeImpl_h |
| 32 #define ApplicationCacheBridgeImpl_h |
| 33 |
| 34 #if ENABLE(APPLICATION_CACHE) |
| 35 |
| 36 #include "ApplicationCacheBridge.h" |
| 37 #include <wtf/MessageQueue.h> |
| 38 #include <wtf/RefPtr.h> |
| 39 #include <wtf/Threading.h> |
| 40 |
| 41 namespace WebCore { |
| 42 |
| 43 class ApplicationCacheBackend; |
| 44 class ApplicationCacheTask; |
| 45 |
| 46 // A implementation for use in single-process browsers. This impl starts |
| 47 // a background thread in the current process and queues tasks to be perform
ed |
| 48 // by the backend on that thread. Async results are delivered back to the |
| 49 // frontend thread. |
| 50 // |
| 51 // FIXME: sooner, put in place interfaces for use by a fatter appcache |
| 52 // savvy ResourceHandle so appcached responses are retrieved in the act |
| 53 // of resource handle loading. |
| 54 // |
| 55 // FIXME: later, integrate with workers. accomodate method calls and client |
| 56 // callbacks on worker threads too. Either allow this interface to be used o
n |
| 57 // worker threads too, or use a worker specific 'frontendproxy' to to the |
| 58 // thread hopping on behalf of workers (probably the latter). |
| 59 // At present this impl can only be used on the main thread. |
| 60 class ApplicationCacheBridgeImpl : public ApplicationCacheBridge { |
| 61 public: |
| 62 // A host browser should call startup and shutdown on the main thread |
| 63 // at browser startup and shutdown time respectively. |
| 64 static void startup(); |
| 65 static void shutdown(); |
| 66 |
| 67 // ApplicationCacheBridge interface |
| 68 |
| 69 virtual void initializeContextAsync(const GlobalApplicationCacheContextI
D &contextID, |
| 70 ApplicationCacheContextType contextT
ype, |
| 71 const GlobalApplicationCacheContextI
D& parentContextID); |
| 72 virtual void uninitializeContextAsync(const GlobalApplicationCacheContex
tID &contextID); |
| 73 |
| 74 virtual void selectInitialCacheAsync(const GlobalApplicationCacheContext
ID &contextID, |
| 75 int sequenceNumber, |
| 76 const KURL& documentURL, |
| 77 ApplicationCacheID cacheDocumentWas
LoadedFrom); |
| 78 virtual void selectCacheWithoutManifestAsync(const GlobalApplicationCach
eContextID &contextID, |
| 79 int sequenceNumber, |
| 80 const KURL& documentURL, |
| 81 ApplicationCacheID cacheDoc
umentWasLoadedFrom); |
| 82 virtual void selectCacheWithManifestAsync(const GlobalApplicationCacheCo
ntextID &contextID, |
| 83 int sequenceNumber, |
| 84 const KURL& documentURL, |
| 85 ApplicationCacheID cacheDocume
ntWasLoadedFrom, |
| 86 const KURL& manifestURLofCache
DocumentWasLoadedFrom, |
| 87 const KURL& manifestURL); |
| 88 |
| 89 virtual ApplicationCacheStatus status(const GlobalApplicationCacheContex
tID &contextID); |
| 90 virtual bool startUpdate(const GlobalApplicationCacheContextID &contextI
D); |
| 91 virtual bool swapCache(const GlobalApplicationCacheContextID &contextID)
; |
| 92 |
| 93 private: |
| 94 ApplicationCacheBridgeImpl(ApplicationCacheBridgeClient* client); |
| 95 virtual ~ApplicationCacheBridgeImpl(); |
| 96 |
| 97 class Task; // See the concrete task classes below. |
| 98 void postTaskToBackendThread(PassRefPtr<Task> task); |
| 99 void postTaskToFrontendThread(PassRefPtr<Task> task); |
| 100 |
| 101 static void performFrontendThreadTask(void* self); |
| 102 static void* appcacheThreadStart(void*); |
| 103 void* appcacheThread(); |
| 104 |
| 105 ThreadIdentifier m_threadID; |
| 106 MessageQueue<RefPtr<Task> > m_queue; |
| 107 MessageQueue<RefPtr<Task> > m_frontendQueue; |
| 108 |
| 109 // FIXME: sooner, instantiate the backend on the background thread |
| 110 // ApplicationCacheBackend m_appcacheBackend |
| 111 |
| 112 // Task base class |
| 113 class Task : public ThreadSafeShared<Task> { |
| 114 public: |
| 115 const GlobalApplicationCacheContextID m_contextID; // many, but not
all, tasks are associated with a specific context |
| 116 |
| 117 virtual void performTask(ApplicationCacheBridgeImpl* bridge) = 0; |
| 118 protected: |
| 119 friend ThreadSafeShared<Task>; |
| 120 Task() {} |
| 121 Task(const GlobalApplicationCacheContextID& contextID) : m_contextID
(contextID) {} |
| 122 virtual ~Task() {} |
| 123 }; |
| 124 |
| 125 // SyncTask base class |
| 126 class SyncTask : public Task { |
| 127 public: |
| 128 void waitForCompletion(); |
| 129 void signalCompletion(); |
| 130 protected: |
| 131 SyncTask() : Task() {} |
| 132 SyncTask(const GlobalApplicationCacheContextID& contextID) : Task(co
ntextID) {} |
| 133 private: |
| 134 Mutex m_lock; |
| 135 ThreadCondition m_completionCondition; |
| 136 }; |
| 137 |
| 138 // Sent from the frontend to the backend |
| 139 class InitaliazeContextTask : public Task { |
| 140 public: |
| 141 const ApplicationCacheContextType m_contextType; |
| 142 const GlobalApplicationCacheContextID& m_parentContextID; |
| 143 |
| 144 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 145 |
| 146 static PassRefPtr<InitaliazeContextTask> create(const GlobalApplicat
ionCacheContextID &contextID, |
| 147 ApplicationCacheCont
extType contextType, |
| 148 const GlobalApplicat
ionCacheContextID& parentContextID) |
| 149 { |
| 150 return new InitaliazeContextTask(contextID, contextType, parentC
ontextID); |
| 151 } |
| 152 private: |
| 153 InitaliazeContextTask(const GlobalApplicationCacheContextID& context
ID, |
| 154 ApplicationCacheContextType contextType, |
| 155 const GlobalApplicationCacheContextID& parentC
ontextID) |
| 156 : Task(contextID) |
| 157 , m_contextType(contextType) |
| 158 , m_parentContextID(parentContextID) |
| 159 { |
| 160 } |
| 161 }; |
| 162 |
| 163 // Sent from the frontend to the backend |
| 164 class UninitializeContextTask : public Task { |
| 165 public: |
| 166 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 167 |
| 168 static PassRefPtr<UninitializeContextTask> create(const GlobalApplic
ationCacheContextID &contextID) |
| 169 { |
| 170 return new UninitializeContextTask(contextID); |
| 171 } |
| 172 private: |
| 173 UninitializeContextTask(const GlobalApplicationCacheContextID& conte
xtID) : Task(contextID) {} |
| 174 }; |
| 175 |
| 176 // Sent from the frontend to the backend |
| 177 class SelectCacheTask : public Task { |
| 178 public: |
| 179 const int m_cacheSequenceNumber; |
| 180 |
| 181 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 182 |
| 183 // FIXME: create/ctor variants that cover all use cases |
| 184 static PassRefPtr<SelectCacheTask> create(const GlobalApplicationCac
heContextID &contextID, int sequenceNumber) |
| 185 { |
| 186 return new SelectCacheTask(contextID, sequenceNumber); |
| 187 } |
| 188 private: |
| 189 SelectCacheTask(const GlobalApplicationCacheContextID& contextID, in
t sequenceNumber) |
| 190 : Task(contextID) |
| 191 , m_cacheSequenceNumber(sequenceNumber) |
| 192 { |
| 193 } |
| 194 }; |
| 195 |
| 196 // Sent from the frontend to the backend |
| 197 class GetStatusTask : public SyncTask { |
| 198 public: |
| 199 ApplicationCacheStatus m_status; |
| 200 |
| 201 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 202 |
| 203 static PassRefPtr<GetStatusTask> create(const GlobalApplicationCache
ContextID &contextID) |
| 204 { |
| 205 return new GetStatusTask(contextID); |
| 206 } |
| 207 private: |
| 208 GetStatusTask(const GlobalApplicationCacheContextID& contextID) |
| 209 : SyncTask(contextID) |
| 210 , m_status(APPCACHE_UNCACHED) |
| 211 { |
| 212 } |
| 213 |
| 214 }; |
| 215 |
| 216 // Sent from the frontend to the backend |
| 217 class StartUpdateTask : public SyncTask { |
| 218 public: |
| 219 bool m_result; |
| 220 |
| 221 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 222 |
| 223 static PassRefPtr<StartUpdateTask> create(const GlobalApplicationCac
heContextID &contextID) |
| 224 { |
| 225 return new StartUpdateTask(contextID); |
| 226 } |
| 227 private: |
| 228 StartUpdateTask(const GlobalApplicationCacheContextID& contextID) |
| 229 : SyncTask(contextID) |
| 230 , m_result(false) |
| 231 { |
| 232 } |
| 233 }; |
| 234 |
| 235 // Sent from the frontend to the backend |
| 236 class SwapCacheTask : public SyncTask { |
| 237 public: |
| 238 bool m_result; |
| 239 |
| 240 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 241 |
| 242 static PassRefPtr<SwapCacheTask> create(const GlobalApplicationCache
ContextID &contextID) |
| 243 { |
| 244 return new SwapCacheTask(contextID); |
| 245 } |
| 246 private: |
| 247 SwapCacheTask(const GlobalApplicationCacheContextID& contextID) |
| 248 : SyncTask(contextID) |
| 249 , m_result(false) |
| 250 { |
| 251 } |
| 252 }; |
| 253 |
| 254 // Sent from the backend to the frontend in response to a SelectCacheTas
k |
| 255 class NotifySelectCompleteTask : public Task { |
| 256 public: |
| 257 const int m_cacheSequenceNumber; |
| 258 const ApplicationCacheStatus m_status; |
| 259 |
| 260 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 261 |
| 262 static PassRefPtr<NotifySelectCompleteTask> create(const GlobalAppli
cationCacheContextID &contextID, |
| 263 int sequenceNumbe
r, |
| 264 ApplicationCacheS
tatus status) |
| 265 { |
| 266 return new NotifySelectCompleteTask(contextID, sequenceNumber, s
tatus); |
| 267 } |
| 268 private: |
| 269 NotifySelectCompleteTask(const GlobalApplicationCacheContextID &cont
extID, |
| 270 int sequenceNumber, |
| 271 ApplicationCacheStatus status) |
| 272 : Task(contextID) |
| 273 , m_cacheSequenceNumber(sequenceNumber) |
| 274 , m_status(status) |
| 275 { |
| 276 } |
| 277 |
| 278 }; |
| 279 |
| 280 // Sent from the backend to the frontend to broadcast status changes |
| 281 class NotifyStatusChangedTask : public Task { |
| 282 public: |
| 283 const Vector<GlobalApplicationCacheContextID> m_contextIDs; |
| 284 const int m_cacheSequenceNumber; |
| 285 const ApplicationCacheStatus m_status; |
| 286 |
| 287 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 288 |
| 289 static PassRefPtr<NotifyStatusChangedTask> create(const Vector<Globa
lApplicationCacheContextID> &contextIDs, |
| 290 int sequenceNumber
, |
| 291 ApplicationCacheSt
atus status) |
| 292 { |
| 293 return new NotifyStatusChangedTask(contextIDs, sequenceNumber, s
tatus); |
| 294 } |
| 295 private: |
| 296 NotifyStatusChangedTask(const Vector<GlobalApplicationCacheContextID
> &contextIDs, |
| 297 int sequenceNumber, |
| 298 ApplicationCacheStatus status) |
| 299 : m_contextIDs(contextIDs) |
| 300 , m_cacheSequenceNumber(sequenceNumber) |
| 301 , m_status(status) |
| 302 { |
| 303 } |
| 304 }; |
| 305 |
| 306 // Sent from the backend to the frontend to broadcast events |
| 307 class NotifyEventListenerTask : public Task { |
| 308 public: |
| 309 const Vector<GlobalApplicationCacheContextID> m_contextIDs; |
| 310 const int m_cacheSequenceNumber; |
| 311 const ApplicationCacheEventType m_eventType; |
| 312 |
| 313 virtual void performTask(ApplicationCacheBridgeImpl* bridge); |
| 314 |
| 315 static PassRefPtr<NotifyEventListenerTask> create(const Vector<Globa
lApplicationCacheContextID> &contextIDs, |
| 316 int sequenceNumber
, |
| 317 ApplicationCacheEv
entType eventType) |
| 318 { |
| 319 return new NotifyEventListenerTask(contextIDs, sequenceNumber, e
ventType); |
| 320 } |
| 321 private: |
| 322 NotifyEventListenerTask(const Vector<GlobalApplicationCacheContextID
> &contextIDs, |
| 323 int sequenceNumber, |
| 324 ApplicationCacheEventType eventType) |
| 325 : m_contextIDs(contextIDs) |
| 326 , m_cacheSequenceNumber(sequenceNumber) |
| 327 , m_eventType(eventType) |
| 328 { |
| 329 } |
| 330 }; |
| 331 }; |
| 332 |
| 333 } // namespace WebCore |
| 334 |
| 335 #endif // ENABLE(APPLICATION_CACHE) |
| 336 #endif // ApplicationCacheBridgeImpl_h |
OLD | NEW |