Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rewrite_driver_factory.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_DRIVER_FACTORY_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_DRIVER_FACTORY_H_
21 
22 #include <set>
23 #include <vector>
24 #include <memory>
25 
37 
38 namespace pagespeed { namespace js { struct JsTokenizerPatterns; } }
39 
40 namespace net_instaweb {
41 
42 class CriticalImagesFinder;
43 class CriticalSelectorFinder;
44 class FileSystem;
45 class ExperimentMatcher;
46 class Hasher;
47 class MessageHandler;
48 class NamedLockManager;
49 class NonceGenerator;
50 class ProcessContext;
51 class ServerContext;
52 class RewriteDriver;
53 class RewriteOptions;
54 class RewriteOptionsManager;
55 class RewriteStats;
56 class SHA1Signature;
57 class Scheduler;
58 class StaticAssetManager;
59 class Timer;
60 class UrlAsyncFetcher;
61 class UrlNamer;
62 class UsageDataReporter;
63 class UserAgentMatcher;
64 class UserAgentNormalizer;
65 
71  public:
73  template<class T> class Deleter;
74 
76  kHtmlWorkers,
77  kRewriteWorkers,
78  kLowPriorityRewriteWorkers,
81  };
82 
84  RewriteDriverFactory(const ProcessContext& process_context,
85  ThreadSystem* thread_system);
86 
92 
95  static void InitializeDefaultOptions(RewriteOptions* options);
96 
97  virtual ~RewriteDriverFactory();
98 
103  void set_html_parse_message_handler(MessageHandler* message_handler);
104  void set_message_handler(MessageHandler* message_handler);
105  void set_file_system(FileSystem* file_system);
106  void set_hasher(Hasher* hasher);
107  void set_nonce_generator(NonceGenerator* nonce_generator);
108  void set_url_namer(UrlNamer* url_namer);
109  void set_signature(SHA1Signature* signature);
110  void set_timer(Timer* timer);
111  void set_usage_data_reporter(UsageDataReporter* reporter);
112 
122  void set_slurp_directory(const StringPiece& directory);
123  void set_slurp_read_only(bool read_only);
124  void set_slurp_print_urls(bool read_only);
125 
128  void set_force_caching(bool u) { force_caching_ = u; }
129 
138  void set_base_url_async_fetcher(UrlAsyncFetcher* url_fetcher);
139  bool set_filename_prefix(StringPiece p);
140 
142  bool slurping_enabled() const { return !slurp_directory_.empty(); }
143 
144  MessageHandler* html_parse_message_handler();
145  MessageHandler* message_handler();
146  FileSystem* file_system();
147  NonceGenerator* nonce_generator();
150  Hasher* hasher();
151  UrlNamer* url_namer();
152  UserAgentMatcher* user_agent_matcher();
153  StaticAssetManager* static_asset_manager();
154  SHA1Signature* signature();
155  RewriteOptions* default_options() { return default_options_.get(); }
156  virtual RewriteOptionsManager* NewRewriteOptionsManager();
157 
162  Timer* timer();
163  NamedLockManager* lock_manager();
164  QueuedWorkerPool* WorkerPool(WorkerPoolCategory pool);
165  Scheduler* scheduler();
166  UsageDataReporter* usage_data_reporter();
167  const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns() const {
168  return js_tokenizer_patterns_;
169  }
170  const std::vector<const UserAgentNormalizer*>& user_agent_normalizers();
171 
176  virtual UrlAsyncFetcher* ComputeUrlAsyncFetcher();
177 
188  ServerContext* CreateServerContext();
189 
193  void InitServerContext(ServerContext* server_context);
194 
199  virtual void SetupCaches(ServerContext* server_context) = 0;
200 
204  virtual bool UseBeaconResultsInFilters() const = 0;
205 
209  virtual void AddPlatformSpecificRewritePasses(RewriteDriver* driver);
210 
219  virtual void AddPlatformSpecificDecodingPasses(RewriteDriver* driver);
220 
224  virtual void ApplyPlatformSpecificConfiguration(RewriteDriver* driver);
225 
226  ThreadSystem* thread_system() { return thread_system_.get(); }
227 
230  const StringSet& created_directories() const {
231  return created_directories_;
232  }
233 
234  bool async_rewrites() { return true; }
235 
240  RewriteStats* rewrite_stats();
241 
243  virtual bool HasWaveforms() const { return false; }
244 
247  Statistics* statistics() { return statistics_; }
248 
251  static void InitStats(Statistics* statistics);
252 
254  static void Initialize();
255  static void Terminate();
256 
258  void SetStatistics(Statistics* stats);
259 
263  virtual void ShutDown();
264 
266  void AddCreatedDirectory(const GoogleString& dir);
267 
273 
278 
280  const GoogleString& version_string() const { return version_string_; }
281  void set_version_string(const StringPiece& version_string) {
282  version_string.CopyToString(&version_string_);
283  }
284 
288  void defer_cleanup(Function* f) { deferred_cleanups_.push_back(f); }
289 
292  template<class T> void TakeOwnership(T* obj) {
294  }
295 
297  virtual bool IsDebugClient(const GoogleString& ip) const {
298  return false;
299  }
300 
304 
305  protected:
306  bool FetchersComputed() const;
307  virtual void StopCacheActivity();
308  StringPiece filename_prefix();
309 
313  bool TerminateServerContext(ServerContext* server_context);
314 
320  virtual MessageHandler* DefaultHtmlParseMessageHandler() = 0;
321  virtual MessageHandler* DefaultMessageHandler() = 0;
322  virtual FileSystem* DefaultFileSystem() = 0;
323  virtual NonceGenerator* DefaultNonceGenerator();
324  virtual Timer* DefaultTimer();
325  virtual SHA1Signature* DefaultSignature();
326 
327  virtual Hasher* NewHasher() = 0;
328 
331  virtual ServerContext* NewServerContext() = 0;
332 
343 
344  virtual CriticalImagesFinder* DefaultCriticalImagesFinder(
345  ServerContext* server_context);
346  virtual CriticalSelectorFinder* DefaultCriticalSelectorFinder(
347  ServerContext* server_context);
348 
352 
355  virtual UrlNamer* DefaultUrlNamer();
356 
357  virtual UserAgentMatcher* DefaultUserAgentMatcher();
358  virtual UsageDataReporter* DefaultUsageDataReporter();
359 
367  std::vector<const UserAgentNormalizer*>* out);
368 
373  StringPiece name);
374 
381  virtual int LowPriorityLoadSheddingThreshold() const;
382 
385  virtual Scheduler* CreateScheduler();
386 
388  virtual void FetcherSetupHooks();
389 
393  virtual StringPiece LockFilePrefix();
394 
397  StaticAssetManager* static_asset_manager) {}
398 
402 
407  virtual std::shared_ptr<CentralController> GetCentralController(
408  NamedLockManager* lock_manager);
409 
411  void RebuildDecodingDriverForTests(ServerContext* server_context);
412 
413  void reset_default_options(RewriteOptions* new_defaults);
414 
415  private:
419  StaticAssetManager* DefaultStaticAssetManager();
420 
421  void SetupSlurpDirectories();
422 
423  void InitDecodingDriver(ServerContext* server_context);
424 
425  scoped_ptr<MessageHandler> html_parse_message_handler_;
426  scoped_ptr<MessageHandler> message_handler_;
427  scoped_ptr<FileSystem> file_system_;
428  UrlAsyncFetcher* url_async_fetcher_;
429  scoped_ptr<UrlAsyncFetcher> base_url_async_fetcher_;
430  scoped_ptr<Hasher> hasher_;
431  scoped_ptr<NonceGenerator> nonce_generator_;
432  scoped_ptr<SHA1Signature> signature_;
433  scoped_ptr<UrlNamer> url_namer_;
434  scoped_ptr<UserAgentMatcher> user_agent_matcher_;
435 
439  std::vector<const UserAgentNormalizer*> user_agent_normalizers_;
440  scoped_ptr<StaticAssetManager> static_asset_manager_;
441  scoped_ptr<Timer> timer_;
442  scoped_ptr<Scheduler> scheduler_;
443  scoped_ptr<UsageDataReporter> usage_data_reporter_;
445  const pagespeed::js::JsTokenizerPatterns* js_tokenizer_patterns_;
446 
447  GoogleString filename_prefix_;
448  GoogleString slurp_directory_;
449  bool force_caching_;
450  bool slurp_read_only_;
451  bool slurp_print_urls_;
452 
453  scoped_ptr<ThreadSystem> thread_system_;
454 
456  typedef std::set<ServerContext*> ServerContextSet;
457  ServerContextSet server_contexts_;
458  scoped_ptr<AbstractMutex> server_context_mutex_;
459 
462  scoped_ptr<RewriteOptions> default_options_;
463 
471  scoped_ptr<ServerContext> decoding_server_context_;
472  scoped_ptr<RewriteDriver> decoding_driver_;
473 
475  scoped_ptr<NamedLockManager> lock_manager_;
476 
479  NullStatistics null_statistics_;
480  Statistics* statistics_;
481 
482  StringSet created_directories_;
483 
484  std::vector<QueuedWorkerPool*> worker_pools_;
485 
488  scoped_ptr<RewriteStats> rewrite_stats_;
489 
492  std::vector<Function*> deferred_cleanups_;
493 
496  GoogleString version_string_;
497 
499  GoogleString hostname_;
500 
501 
502 };
503 
506 template<class T> class RewriteDriverFactory::Deleter : public Function {
507  public:
508  explicit Deleter(T* obj) : obj_(obj) {}
509  virtual void Run() { delete obj_; }
510  private:
511  T* obj_;
512 
513 };
514 
515 }
516 
517 #endif
Definition: url_namer.h:38
virtual RewriteOptions * NewRewriteOptions()
virtual int LowPriorityLoadSheddingThreshold() const
bool slurping_enabled() const
Determines whether Slurping is enabled.
Definition: rewrite_driver_factory.h:142
virtual RewriteOptions * NewRewriteOptionsForQuery()
virtual void InitStaticAssetManager(StaticAssetManager *static_asset_manager)
Initializes the StaticAssetManager.
Definition: rewrite_driver_factory.h:396
virtual void SetupCaches(ServerContext *server_context)=0
Implementation class of Signature, using HMAC-SHA1 for signing.
Definition: sha1_signature.h:32
virtual void AddPlatformSpecificDecodingPasses(RewriteDriver *driver)
Definition: experiment_matcher.h:38
void RebuildDecodingDriverForTests(ServerContext *server_context)
For use in tests.
Definition: critical_images_finder.h:65
Definition: static_asset_manager.h:48
Definition: named_lock_manager.h:82
const GoogleString & version_string() const
get/set the version placed into the X-[Mod-]Page(s|-S)peed header.
Definition: rewrite_driver_factory.h:280
const StringSet & created_directories() const
Definition: rewrite_driver_factory.h:230
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
bool TerminateServerContext(ServerContext *server_context)
virtual UrlNamer * DefaultUrlNamer()
void set_force_caching(bool u)
Definition: rewrite_driver_factory.h:128
Definition: user_agent_matcher.h:43
WorkerPoolCategory
Definition: rewrite_driver_factory.h:75
void set_html_parse_message_handler(MessageHandler *message_handler)
virtual ServerContext * NewServerContext()=0
virtual NamedLockManager * DefaultLockManager()
virtual Scheduler * CreateScheduler()
virtual ServerContext * NewDecodingServerContext()=0
void TakeOwnership(T *obj)
Definition: rewrite_driver_factory.h:292
void AddCreatedDirectory(const GoogleString &dir)
Registers the directory as having been created by us.
Make sure to insert new values above this line.
Definition: rewrite_driver_factory.h:80
Definition: critical_selector_finder.h:51
void InitStubDecodingServerContext(ServerContext *context)
virtual ExperimentMatcher * NewExperimentMatcher()
virtual UrlAsyncFetcher * ComputeUrlAsyncFetcher()
virtual void AddPlatformSpecificRewritePasses(RewriteDriver *driver)
virtual void AddPlatformSpecificUserAgentNormalizers(std::vector< const UserAgentNormalizer * > *out)
virtual void FetcherSetupHooks()
Called before creating the url fetchers.
Definition: scoped_ptr.h:30
Definition: function.h:47
void defer_cleanup(Function *f)
Definition: rewrite_driver_factory.h:288
RewriteDriverFactory(const ProcessContext &process_context, ThreadSystem *thread_system)
Takes ownership of thread_system.
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: queued_worker_pool.h:50
virtual UrlAsyncFetcher * DefaultAsyncUrlFetcher()=0
Definition: js_tokenizer.h:194
ServerContext * CreateServerContext()
virtual void Run()
Definition: rewrite_driver_factory.h:509
Definition: file_system.h:76
void set_base_url_async_fetcher(UrlAsyncFetcher *url_fetcher)
void InitServerContext(ServerContext *server_context)
Definition: server_context.h:99
static void Initialize()
Initializes static variables. Initialize/Terminate calls must be paired.
Definition: process_context.h:35
virtual QueuedWorkerPool * CreateWorkerPool(WorkerPoolCategory pool, StringPiece name)
Statistics * statistics()
Definition: rewrite_driver_factory.h:247
virtual StringPiece LockFilePrefix()
virtual void ApplyPlatformSpecificConfiguration(RewriteDriver *driver)
Definition: usage_data_reporter.h:35
Definition: thread_system.h:40
void set_slurp_directory(const StringPiece &directory)
Definition: message_handler.h:39
Definition: scheduler.h:47
Definition: nonce_generator.h:28
Helper for users of defer_cleanup; see below.
Definition: rewrite_driver_factory.h:73
void SetStatistics(Statistics *stats)
Does not take ownership of Statistics.
Definition: rewrite_options.h:84
virtual bool IsDebugClient(const GoogleString &ip) const
Base method that returns true if the given ip is a debug ip.
Definition: rewrite_driver_factory.h:297
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
virtual std::shared_ptr< CentralController > GetCentralController(NamedLockManager *lock_manager)
Simple name/value pair statistics implementation.
Definition: null_statistics.h:44
Definition: hasher.h:30
Definition: url_async_fetcher.h:33
static void InitStats(Statistics *statistics)
virtual bool UseBeaconResultsInFilters() const =0
Definition: rewrite_driver_factory.h:70
virtual bool HasWaveforms() const
Returns true if the server supports waveforms in its status page.
Definition: rewrite_driver_factory.h:243