Page Speed Optimization Libraries  1.7.30.4
net/instaweb/http/public/sync_fetcher_adapter_callback.h
Go to the documentation of this file.
00001 // Copyright 2010 Google Inc.
00014 
00017 
00018 #ifndef NET_INSTAWEB_HTTP_PUBLIC_SYNC_FETCHER_ADAPTER_CALLBACK_H_
00019 #define NET_INSTAWEB_HTTP_PUBLIC_SYNC_FETCHER_ADAPTER_CALLBACK_H_
00020 
00021 #include "net/instaweb/http/public/async_fetch.h"
00022 #include "net/instaweb/http/public/request_context.h"
00023 #include "net/instaweb/util/public/basictypes.h"
00024 #include "net/instaweb/util/public/scoped_ptr.h"
00025 #include "net/instaweb/util/public/string_util.h"
00026 #include "net/instaweb/util/public/thread_system.h"
00027 #include "net/instaweb/util/public/writer.h"
00028 #include "pagespeed/kernel/base/thread_annotations.h"
00029 
00030 namespace net_instaweb {
00031 
00032 class MessageHandler;
00033 
00035 class SyncFetcherAdapterCallback : public AsyncFetch {
00036  public:
00037   SyncFetcherAdapterCallback(ThreadSystem* thread_system, Writer* writer,
00038                              const RequestContextPtr& request_context);
00039 
00052 
00058   void Release() LOCKS_EXCLUDED(mutex_);
00059 
00060   bool IsDone() const LOCKS_EXCLUDED(mutex_);
00061 
00063   bool IsDoneLockHeld() const EXCLUSIVE_LOCKS_REQUIRED(mutex_);
00064   bool success() const LOCKS_EXCLUDED(mutex_);
00065   bool released() const LOCKS_EXCLUDED(mutex_);
00066 
00070   bool LockIfNotReleased() EXCLUSIVE_TRYLOCK_FUNCTION(true, mutex_);
00071 
00073   void Unlock() UNLOCK_FUNCTION(mutex_);
00074 
00081   void TimedWait(int64 timeout_ms) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
00082 
00083  protected:
00084   virtual void HandleDone(bool success) LOCKS_EXCLUDED(mutex_);
00085   virtual bool HandleWrite(const StringPiece& content,
00086                            MessageHandler* handler) {
00087     return writer_->Write(content, handler);
00088   }
00089   virtual bool HandleFlush(MessageHandler* handler) {
00090     return writer_->Flush(handler);
00091   }
00092   virtual void HandleHeadersComplete() {
00093   }
00094 
00095  private:
00100   class ProtectedWriter : public Writer {
00101    public:
00102     ProtectedWriter(SyncFetcherAdapterCallback* callback, Writer* orig_writer)
00103         : callback_(callback), orig_writer_(orig_writer) {}
00104 
00105     virtual bool Write(const StringPiece& buf, MessageHandler* handler)
00106         LOCKS_EXCLUDED(callback_->mutex_);
00107     virtual bool Flush(MessageHandler* handler)
00108         LOCKS_EXCLUDED(callback_->mutex_);
00109 
00110    private:
00111     SyncFetcherAdapterCallback* callback_;
00112     Writer* orig_writer_ GUARDED_BY(callback_->mutex_);
00113 
00114     DISALLOW_COPY_AND_ASSIGN(ProtectedWriter);
00115   };
00116   virtual ~SyncFetcherAdapterCallback();
00117 
00118   scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
00119   scoped_ptr<ThreadSystem::Condvar> cond_;
00120 
00121   bool done_ GUARDED_BY(mutex_);
00122   bool success_ GUARDED_BY(mutex_);
00123   bool released_ GUARDED_BY(mutex_);
00124   scoped_ptr<Writer> writer_;
00125 
00126   DISALLOW_COPY_AND_ASSIGN(SyncFetcherAdapterCallback);
00127 };
00128 
00129 }  
00130 
00131 #endif  ///< NET_INSTAWEB_HTTP_PUBLIC_SYNC_FETCHER_ADAPTER_CALLBACK_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines