Page Speed Optimization Libraries
1.2.24.1
|
00001 /* 00002 * Copyright 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 00023 #ifndef NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_FETCH_H_ 00024 #define NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_FETCH_H_ 00025 00026 #include <map> 00027 #include <set> 00028 #include <vector> 00029 00030 #include "net/instaweb/automatic/public/html_detector.h" 00031 #include "net/instaweb/http/public/async_fetch.h" 00032 #include "net/instaweb/http/public/meta_data.h" 00033 #include "net/instaweb/util/public/queued_worker_pool.h" 00034 #include "net/instaweb/util/public/basictypes.h" 00035 #include "net/instaweb/util/public/property_cache.h" 00036 #include "net/instaweb/util/public/scoped_ptr.h" 00037 #include "net/instaweb/util/public/string.h" 00038 #include "net/instaweb/util/public/string_util.h" 00039 00040 namespace net_instaweb { 00041 00042 class AbstractClientState; 00043 class AbstractMutex; 00044 class CacheUrlAsyncFetcher; 00045 class Function; 00046 class MessageHandler; 00047 class ProxyFetch; 00048 class ProxyFetchPropertyCallbackCollector; 00049 class QueuedAlarm; 00050 class ServerContext; 00051 class ResponseHeaders; 00052 class RewriteDriver; 00053 class RewriteOptions; 00054 class Timer; 00055 00058 class ProxyFetchFactory { 00059 public: 00060 explicit ProxyFetchFactory(ServerContext* manager); 00061 ~ProxyFetchFactory(); 00062 00065 void StartNewProxyFetch( 00066 const GoogleString& url, 00067 AsyncFetch* async_fetch, 00068 RewriteDriver* driver, 00069 ProxyFetchPropertyCallbackCollector* property_callback, 00070 AsyncFetch* original_content_fetch); 00071 00081 ProxyFetch* CreateNewProxyFetch( 00082 const GoogleString& url, 00083 AsyncFetch* async_fetch, 00084 RewriteDriver* driver, 00085 ProxyFetchPropertyCallbackCollector* property_callback, 00086 AsyncFetch* original_content_fetch); 00087 00088 MessageHandler* message_handler() const { return handler_; } 00089 00090 private: 00091 friend class ProxyFetch; 00092 00097 void RegisterNewFetch(ProxyFetch* proxy_fetch); 00098 void RegisterFinishedFetch(ProxyFetch* proxy_fetch); 00099 00100 ServerContext* manager_; 00101 Timer* timer_; 00102 MessageHandler* handler_; 00103 00104 scoped_ptr<AbstractMutex> outstanding_proxy_fetches_mutex_; 00105 std::set<ProxyFetch*> outstanding_proxy_fetches_; 00106 00107 DISALLOW_COPY_AND_ASSIGN(ProxyFetchFactory); 00108 }; 00109 00118 class ProxyFetchPropertyCallback : public PropertyPage { 00119 public: 00121 enum CacheType { 00122 kPagePropertyCache, 00123 kClientPropertyCache 00124 }; 00125 00126 ProxyFetchPropertyCallback(CacheType cache_type, 00127 const StringPiece& key, 00128 ProxyFetchPropertyCallbackCollector* collector, 00129 AbstractMutex* mutex); 00130 00131 CacheType cache_type() const { return cache_type_; } 00132 00134 virtual bool IsCacheValid(int64 write_timestamp_ms) const; 00135 00136 virtual void Done(bool success); 00137 00138 private: 00139 CacheType cache_type_; 00140 ProxyFetchPropertyCallbackCollector* collector_; 00141 GoogleString url_; 00142 DISALLOW_COPY_AND_ASSIGN(ProxyFetchPropertyCallback); 00143 }; 00144 00146 class ProxyFetchPropertyCallbackCollector { 00147 public: 00148 ProxyFetchPropertyCallbackCollector(ServerContext* manager, 00149 const StringPiece& url, 00150 const RewriteOptions* options); 00151 virtual ~ProxyFetchPropertyCallbackCollector(); 00152 00155 void AddCallback(ProxyFetchPropertyCallback* callback); 00156 00163 void ConnectProxyFetch(ProxyFetch* proxy_fetch); 00164 00171 void Detach(HttpStatus::Code status_code); 00172 00175 PropertyPage* GetPropertyPage( 00176 ProxyFetchPropertyCallback::CacheType cache_type); 00177 00180 PropertyPage* GetPropertyPageWithoutOwnership( 00181 ProxyFetchPropertyCallback::CacheType cache_type); 00182 00192 void AddPostLookupTask(Function* func); 00193 00197 bool IsCacheValid(int64 write_timestamp_ms) const; 00198 00200 void Done(ProxyFetchPropertyCallback* callback, bool success); 00201 00203 void UpdateStatusCodeInPropertyCache(); 00204 00205 private: 00206 std::set<ProxyFetchPropertyCallback*> pending_callbacks_; 00207 std::map<ProxyFetchPropertyCallback::CacheType, PropertyPage*> 00208 property_pages_; 00209 scoped_ptr<AbstractMutex> mutex_; 00210 ServerContext* server_context_; 00211 GoogleString url_; 00212 bool detached_; 00213 bool done_; 00214 bool success_; 00215 ProxyFetch* proxy_fetch_; 00216 00217 scoped_ptr<std::vector<Function*> > post_lookup_task_vector_; 00218 const RewriteOptions* options_; 00219 HttpStatus::Code status_code_; 00220 00221 DISALLOW_COPY_AND_ASSIGN(ProxyFetchPropertyCallbackCollector); 00222 }; 00223 00244 class ProxyFetch : public SharedAsyncFetch { 00245 public: 00248 static const char kCollectorDone[]; 00249 static const char kCollectorPrefix[]; 00250 static const char kCollectorReady[]; 00251 static const char kCollectorDelete[]; 00252 static const char kCollectorDetach[]; 00253 static const char kCollectorDoneDelete[]; 00254 00257 static const char kHeadersSetupRaceAlarmQueued[]; 00258 static const char kHeadersSetupRaceDone[]; 00259 static const char kHeadersSetupRaceFlush[]; 00260 static const char kHeadersSetupRacePrefix[]; 00261 static const char kHeadersSetupRaceWait[]; 00262 00269 static const int kTestSignalTimeoutMs = 200; 00270 00271 protected: 00273 virtual void HandleHeadersComplete(); 00274 virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler); 00275 virtual bool HandleFlush(MessageHandler* handler); 00276 virtual void HandleDone(bool success); 00277 virtual bool IsCachedResultValid(const ResponseHeaders& headers); 00278 00279 private: 00280 friend class ProxyFetchFactory; 00281 friend class ProxyFetchPropertyCallbackCollector; 00282 friend class MockProxyFetch; 00283 00286 virtual void PropertyCacheComplete( 00287 bool success, ProxyFetchPropertyCallbackCollector* collector); 00288 00293 AbstractClientState* GetClientState( 00294 ProxyFetchPropertyCallbackCollector* collector); 00295 00298 ProxyFetch(const GoogleString& url, 00299 bool cross_domain, 00300 ProxyFetchPropertyCallbackCollector* property_cache_callback, 00301 AsyncFetch* async_fetch, 00302 AsyncFetch* original_content_fetch, 00303 RewriteDriver* driver, 00304 ServerContext* manager, 00305 Timer* timer, 00306 ProxyFetchFactory* factory); 00307 virtual ~ProxyFetch(); 00308 00309 const RewriteOptions* Options(); 00310 00312 void SetupForHtml(); 00313 00315 void AddPagespeedHeader(); 00316 00319 bool StartParse(); 00320 00322 void StartFetch(); 00323 00325 void DoFetch(); 00326 00329 void ExecuteQueued(); 00330 00333 void ScheduleQueueExecutionIfNeeded(); 00334 00339 void Finish(bool success); 00340 00342 void CompleteFinishParse(bool success); 00343 00346 void FlushDone(); 00347 00350 00352 void CancelIdleAlarm(); 00353 00355 void QueueIdleAlarm(); 00356 00358 void HandleIdleAlarm(); 00359 00360 GoogleString url_; 00361 ServerContext* server_context_; 00362 Timer* timer_; 00363 00364 scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_; 00365 00368 bool cross_domain_; 00369 00371 bool claims_html_; 00372 00375 bool started_parse_; 00376 00378 bool parse_text_called_; 00379 00381 bool done_called_; 00382 00383 HtmlDetector html_detector_; 00384 00389 ProxyFetchPropertyCallbackCollector* property_cache_callback_; 00390 00394 AsyncFetch* original_content_fetch_; 00395 00398 RewriteDriver* driver_; 00399 00402 bool queue_run_job_created_; 00403 00415 scoped_ptr<AbstractMutex> mutex_; 00416 StringStarVector text_queue_; 00417 bool network_flush_outstanding_; 00418 QueuedWorkerPool::Sequence* sequence_; 00419 00422 bool done_outstanding_; 00423 00426 bool finishing_; 00427 00430 bool done_result_; 00431 00435 bool waiting_for_flush_to_finish_; 00436 00439 QueuedAlarm* idle_alarm_; 00440 00441 ProxyFetchFactory* factory_; 00442 00444 bool prepare_success_; 00445 00446 DISALLOW_COPY_AND_ASSIGN(ProxyFetch); 00447 }; 00448 00449 } 00450 00451 #endif ///< NET_INSTAWEB_AUTOMATIC_PUBLIC_PROXY_FETCH_H_