00001 /* 00002 * Copyright 2010 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 00018 00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_DRIVER_FACTORY_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_DRIVER_FACTORY_H_ 00021 00022 #include <set> 00023 #include <vector> 00024 00025 #include "base/scoped_ptr.h" 00026 #include "net/instaweb/util/public/basictypes.h" 00027 #include "net/instaweb/util/public/function.h" 00028 #include "net/instaweb/util/public/null_statistics.h" 00029 #include "net/instaweb/util/public/string.h" 00030 #include "net/instaweb/util/public/string_util.h" 00031 00032 namespace net_instaweb { 00033 00034 class AbstractClientState; 00035 class AbstractMutex; 00036 class BlinkCriticalLineDataFinder; 00037 class CacheInterface; 00038 class CriticalImagesFinder; 00039 class FileSystem; 00040 class FilenameEncoder; 00041 class Hasher; 00042 class HTTPCache; 00043 class MessageHandler; 00044 class NamedLockManager; 00045 class PropertyCache; 00046 class QueuedWorkerPool; 00047 class ResourceManager; 00048 class RewriteDriver; 00049 class RewriteOptions; 00050 class RewriteStats; 00051 class Scheduler; 00052 class StaticJavascriptManager; 00053 class Statistics; 00054 class ThreadSystem; 00055 class Timer; 00056 class UrlAsyncFetcher; 00057 class UrlFetcher; 00058 class UrlNamer; 00059 class UsageDataReporter; 00060 class UserAgentMatcher; 00061 00066 class RewriteDriverFactory { 00067 public: 00069 template<class T> class Deleter; 00070 00071 enum WorkerPoolName { 00072 kHtmlWorkers, 00073 kRewriteWorkers, 00074 kLowPriorityRewriteWorkers, 00076 kNumWorkerPools 00077 }; 00078 00080 explicit RewriteDriverFactory(ThreadSystem* thread_system); 00081 00083 RewriteDriverFactory(); 00084 00089 void InitializeDefaultOptions(); 00090 00091 virtual ~RewriteDriverFactory(); 00092 00097 void set_html_parse_message_handler(MessageHandler* message_handler); 00098 void set_message_handler(MessageHandler* message_handler); 00099 void set_file_system(FileSystem* file_system); 00100 void set_hasher(Hasher* hasher); 00101 void set_filename_encoder(FilenameEncoder* filename_encoder); 00102 void set_url_namer(UrlNamer* url_namer); 00103 void set_timer(Timer* timer); 00104 void set_critical_images_finder(CriticalImagesFinder* finder); 00105 void set_blink_critical_line_data_finder(BlinkCriticalLineDataFinder* finder); 00106 void set_enable_property_cache(bool enabled); 00107 void set_usage_data_reporter(UsageDataReporter* reporter); 00108 00119 void set_slurp_directory(const StringPiece& directory); 00120 void set_slurp_read_only(bool read_only); 00121 void set_slurp_print_urls(bool read_only); 00122 00125 void set_force_caching(bool u) { force_caching_ = u; } 00126 00138 void set_base_url_fetcher(UrlFetcher* url_fetcher); 00139 void set_base_url_async_fetcher(UrlAsyncFetcher* url_fetcher); 00140 00141 bool set_filename_prefix(StringPiece p); 00142 00144 bool slurping_enabled() const { return !slurp_directory_.empty(); } 00145 00146 MessageHandler* html_parse_message_handler(); 00147 MessageHandler* message_handler(); 00148 FileSystem* file_system(); 00151 Hasher* hasher(); 00152 FilenameEncoder* filename_encoder() { return filename_encoder_.get(); } 00153 UrlNamer* url_namer(); 00154 UserAgentMatcher* user_agent_matcher(); 00155 StaticJavascriptManager* static_javascript_manager(); 00156 RewriteOptions* default_options() { return default_options_.get(); } 00157 00160 Timer* timer(); 00161 HTTPCache* http_cache(); 00162 PropertyCache* page_property_cache(); 00163 PropertyCache* client_property_cache(); 00164 NamedLockManager* lock_manager(); 00165 QueuedWorkerPool* WorkerPool(WorkerPoolName pool); 00166 Scheduler* scheduler(); 00167 UsageDataReporter* usage_data_reporter(); 00168 00170 PropertyCache* MakePropertyCache(const GoogleString& cache_key_prefix, 00171 CacheInterface* cache) const; 00172 00177 virtual UrlFetcher* ComputeUrlFetcher(); 00178 virtual UrlAsyncFetcher* ComputeUrlAsyncFetcher(); 00179 00181 virtual HTTPCache* ComputeHTTPCache(); 00182 00187 ResourceManager* CreateResourceManager(); 00188 00192 void InitResourceManager(ResourceManager* resource_manager); 00193 00197 virtual void AddPlatformSpecificRewritePasses(RewriteDriver* driver); 00198 00205 virtual void AddPlatformSpecificDecodingPasses(RewriteDriver* driver); 00206 00210 virtual void ApplyPlatformSpecificConfiguration(RewriteDriver* driver); 00211 00212 ThreadSystem* thread_system() { return thread_system_.get(); } 00213 00214 CriticalImagesFinder* critical_images_finder(); 00215 BlinkCriticalLineDataFinder* blink_critical_line_data_finder(); 00216 00219 const StringSet& created_directories() const { 00220 return created_directories_; 00221 } 00222 00223 bool async_rewrites() { return true; } 00224 00229 RewriteStats* rewrite_stats(); 00230 00233 Statistics* statistics() { return statistics_; } 00234 00235 static void Initialize(Statistics* statistics); 00236 static void Terminate(); 00237 00239 void SetStatistics(Statistics* stats); 00240 00244 virtual void ShutDown(); 00245 00247 void AddCreatedDirectory(const GoogleString& dir); 00248 00253 virtual RewriteOptions* NewRewriteOptions(); 00254 00258 virtual RewriteOptions* NewRewriteOptionsForQuery(); 00259 00261 const GoogleString& version_string() const { return version_string_; } 00262 void set_version_string(const StringPiece& version_string) { 00263 version_string.CopyToString(&version_string_); 00264 } 00265 00269 void defer_delete(Function* f) { deferred_deletes_.push_back(f); } 00270 00272 virtual bool IsDebugClient(const GoogleString& ip) const { 00273 return false; 00274 } 00275 00279 virtual AbstractClientState* NewClientState(); 00280 00281 protected: 00282 bool FetchersComputed() const; 00283 void StopCacheWrites(); 00284 StringPiece filename_prefix(); 00285 00289 bool TerminateResourceManager(ResourceManager* rm); 00290 00294 virtual UrlFetcher* DefaultUrlFetcher() = 0; 00295 virtual UrlAsyncFetcher* DefaultAsyncUrlFetcher() = 0; 00296 virtual MessageHandler* DefaultHtmlParseMessageHandler() = 0; 00297 virtual MessageHandler* DefaultMessageHandler() = 0; 00298 virtual FileSystem* DefaultFileSystem() = 0; 00299 virtual Timer* DefaultTimer() = 0; 00300 00301 virtual Hasher* NewHasher() = 0; 00302 00304 virtual CriticalImagesFinder* DefaultCriticalImagesFinder(); 00305 00307 virtual BlinkCriticalLineDataFinder* DefaultBlinkCriticalLineDataFinder(); 00308 00310 virtual CacheInterface* DefaultCacheInterface() = 0; 00311 00314 virtual NamedLockManager* DefaultLockManager(); 00315 00318 virtual UrlNamer* DefaultUrlNamer(); 00319 00320 virtual UserAgentMatcher* DefaultUserAgentMatcher(); 00321 virtual UsageDataReporter* DefaultUsageDataReporter(); 00322 00326 virtual QueuedWorkerPool* CreateWorkerPool(WorkerPoolName name); 00327 00334 virtual int LowPriorityLoadSheddingThreshold() const; 00335 00338 virtual Scheduler* CreateScheduler(); 00339 00341 virtual void FetcherSetupHooks(); 00342 00346 virtual StringPiece LockFilePrefix(); 00347 00349 CacheInterface* cache_backend(); 00350 00352 virtual CacheInterface* property_cache_backend() { return cache_backend(); } 00353 00357 virtual StaticJavascriptManager* DefaultStaticJavascriptManager(); 00358 00361 virtual void SetupCohorts() {} 00362 00363 private: 00364 void SetupSlurpDirectories(); 00365 void Init(); 00366 00367 scoped_ptr<MessageHandler> html_parse_message_handler_; 00368 scoped_ptr<MessageHandler> message_handler_; 00369 scoped_ptr<FileSystem> file_system_; 00370 UrlFetcher* url_fetcher_; 00371 UrlAsyncFetcher* url_async_fetcher_; 00372 scoped_ptr<UrlFetcher> base_url_fetcher_; 00373 scoped_ptr<UrlAsyncFetcher> base_url_async_fetcher_; 00374 scoped_ptr<Hasher> hasher_; 00375 scoped_ptr<FilenameEncoder> filename_encoder_; 00376 scoped_ptr<UrlNamer> url_namer_; 00377 scoped_ptr<UserAgentMatcher> user_agent_matcher_; 00378 scoped_ptr<StaticJavascriptManager> static_javascript_manager_; 00379 scoped_ptr<Timer> timer_; 00380 scoped_ptr<Scheduler> scheduler_; 00381 scoped_ptr<UsageDataReporter> usage_data_reporter_; 00382 00383 GoogleString filename_prefix_; 00384 GoogleString slurp_directory_; 00385 bool force_caching_; 00386 bool slurp_read_only_; 00387 bool slurp_print_urls_; 00388 bool enable_property_cache_; 00389 00391 typedef std::set<ResourceManager*> ResourceManagerSet; 00392 ResourceManagerSet resource_managers_; 00393 scoped_ptr<AbstractMutex> resource_manager_mutex_; 00394 00397 scoped_ptr<RewriteOptions> default_options_; 00398 00400 scoped_ptr<CacheInterface> cache_backend_; 00401 scoped_ptr<HTTPCache> http_cache_; 00402 00404 scoped_ptr<NamedLockManager> lock_manager_; 00405 00406 scoped_ptr<ThreadSystem> thread_system_; 00407 scoped_ptr<CriticalImagesFinder> critical_images_finder_; 00408 scoped_ptr<BlinkCriticalLineDataFinder> blink_critical_line_data_finder_; 00409 scoped_ptr<PropertyCache> page_property_cache_; 00410 scoped_ptr<PropertyCache> client_property_cache_; 00411 00414 NullStatistics null_statistics_; 00415 Statistics* statistics_; 00416 00417 StringSet created_directories_; 00418 00419 std::vector<QueuedWorkerPool*> worker_pools_; 00420 00423 scoped_ptr<RewriteStats> rewrite_stats_; 00424 00427 std::vector<Function*> deferred_deletes_; 00428 00430 GoogleString version_string_; 00431 00432 DISALLOW_COPY_AND_ASSIGN(RewriteDriverFactory); 00433 }; 00434 00437 template<class T> class RewriteDriverFactory::Deleter : public Function { 00438 public: 00439 explicit Deleter(T* obj) : obj_(obj) {} 00440 virtual void Run() { delete obj_; } 00441 private: 00442 T* obj_; 00443 DISALLOW_COPY_AND_ASSIGN(Deleter); 00444 }; 00445 00446 } 00447 00448 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_DRIVER_FACTORY_H_