19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
26 #include "net/instaweb/rewriter/cached_result.pb.h"
27 #include "net/instaweb/rewriter/input_info.pb.h"
45 namespace net_instaweb {
55 class FreshenMetadataUpdateManager;
59 kRenderOnlyCspWarning,
157 typedef std::vector<InputInfo*> InputInfoStarVector;
158 static const char kNumRewritesAbandonedForLockContention[];
159 static const char kNumDeadlineAlarmInvocations[];
160 static const char kHashMismatchMessage[];
167 can_revalidate(
false),
168 useable_cache_content(
false),
169 is_stale_rewrite(
false),
170 partitions(
new OutputPartitions) {}
174 bool useable_cache_content;
175 bool is_stale_rewrite;
176 InputInfoStarVector revalidate;
195 ResourceContext* resource_context);
213 const CachedResult* output_partition(
int i)
const;
214 CachedResult* mutable_output_partition(
int i);
236 void CallFetchInputs();
237 void CallLockFailed();
238 void CallStartFetchImpl();
263 bool slow()
const {
return slow_; }
287 RewriteDriver* Driver()
const {
294 bool rewrite_uncacheable()
const {
return rewrite_uncacheable_; }
295 void set_rewrite_uncacheable(
bool rewrite_uncacheable) {
296 rewrite_uncacheable_ = rewrite_uncacheable;
299 const ResourceContext* resource_context()
const {
300 return resource_context_.get();
305 GoogleString ToStringWithPrefix(StringPiece prefix)
const;
308 static void InitStats(Statistics* stats);
311 typedef std::vector<GoogleUrl*> GoogleUrlStarVector;
322 const RewriteOptions* Options()
const;
324 OutputPartitions* partitions() {
return partitions_.get(); }
352 virtual bool Partition(OutputPartitions* partitions,
353 OutputResourceVector* outputs);
361 OutputResourceVector* outputs);
390 virtual void Rewrite(
int partition_index,
391 CachedResult* partition,
410 StringPiece contents,
487 const ResourceContext* context)
const {
498 virtual const char*
id()
const = 0;
559 GoogleUrlStarVector* url_vector);
577 const StringPiece& hash);
582 bool notify_driver_on_fetch_done()
const {
583 return notify_driver_on_fetch_done_;
585 void set_notify_driver_on_fetch_done(
bool value) {
586 notify_driver_on_fetch_done_ = value;
651 CacheLookupResultCallback* callback);
654 class OutputCacheCallback;
655 class WriteIfChanged;
656 class LookupMetadataForOutputResourceCallback;
657 class HTTPCacheCallback;
658 class ResourceCallbackUtils;
659 class ResourceFetchCallback;
660 class ResourceReconstructCallback;
661 class ResourceRevalidateCallback;
662 class InvokeRewriteFunction;
663 class RewriteFreshenCallback;
664 class TryLockFunction;
667 typedef std::set<RewriteContext*> ContextSet;
672 enum FallbackCondition {
673 kFallbackDiscretional,
681 void SetPartitionKey();
683 void StartFetchImpl();
685 void OutputCacheDone(CacheLookupResult* cache_result);
686 void OutputCacheHit(
bool write_partitions);
687 void OutputCacheRevalidate(
const InputInfoStarVector& to_revalidate);
688 void OutputCacheMiss();
689 void ResourceFetchDone(
bool success, ResourcePtr resource,
int slot_index);
690 void ResourceRevalidateDone(InputInfo* input_info,
bool success);
691 void LogMetadataCacheInfo(
bool cache_ok,
bool can_revalidate);
698 void RepeatedFailure();
733 bool CreateOutputResourceFromContent(
const CachedResult& cached_result,
734 const ResponseHeaders& response_headers,
736 OutputResourcePtr* output_resource);
740 bool IsFetchRewrite()
const {
return fetch_.get() != NULL; }
776 void Propagate(RenderOp render_op);
784 void StartRewriteForHtml();
785 void StartRewriteForFetch();
793 bool ReadyToRewrite()
const;
803 void RunSuccessors();
807 void WritePartition();
818 void FinalizeRewriteForHtml();
825 void RetireRewriteForHtml(RenderOp permit_render);
838 void CollectDependentTopLevel(ContextSet* contexts);
842 void RewriteDoneImpl(
RewriteResult result,
int partition_index);
846 void StartNestedTasksImpl();
851 void RenderPartitionOnDetach(
int partition_index);
856 const OutputResourcePtr& output_resource,
858 MessageHandler* message_handler);
862 bool CreateOutputResourceForCachedOutput(
const CachedResult* cached_result,
863 OutputResourcePtr* output_resource);
866 void FetchCacheDone(CacheLookupResult* cache_result);
870 void FetchFallbackCacheDone(HTTPCache::FindResult result,
871 HTTPCache::Callback* data);
877 bool CanFetchFallbackToOriginal(FallbackCondition circumstance)
const;
881 bool HasDuplicateOtherDependency(
const InputInfo& input);
886 void CheckAndAddOtherDependency(
const InputInfo& input);
890 void CheckAndFreshenResource(
const InputInfo& input_info,
891 ResourcePtr resource,
int partition_index,
893 FreshenMetadataUpdateManager* freshen_manager);
894 ResourcePtr CreateUrlResource(
const StringPiece& input_url);
897 ResourceSlotVector slots_;
902 std::vector<bool> render_slots_;
922 scoped_ptr<OutputPartitions> partitions_;
923 OutputResourceVector outputs_;
924 int outstanding_fetches_;
925 int outstanding_rewrites_;
926 scoped_ptr<ResourceContext> resource_context_;
929 UrlSegmentEncoder default_encoder_;
933 scoped_ptr<NamedLock> lock_;
939 scoped_ptr<FetchContext> fetch_;
943 std::vector<RewriteContext*> successors_;
948 std::vector<RewriteContext*> repeated_;
953 int num_pending_nested_;
954 std::vector<RewriteContext*> nested_;
967 RewriteDriver* driver_;
970 int num_predecessors_;
1008 bool ok_to_write_output_partitions_;
1021 bool revalidate_ok_;
1025 bool notify_driver_on_fetch_done_;
1029 bool force_rewrite_;
1033 bool stale_rewrite_;
1037 bool is_metadata_cache_miss_;
1041 bool rewrite_uncacheable_;
1045 RequestTrace* dependent_request_trace_;
1048 StringIntMap other_dependency_map_;
1053 scoped_ptr<ScheduleRewriteContext> schedule_rewrite_context_;
1055 Variable*
const num_rewrites_abandoned_for_lock_contention_;
RewriteContext * parent()
Allows a nested rewriter to walk up its parent hierarchy.
Definition: rewrite_context.h:280
class GoogleUrl
Definition: google_url.h:58
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
virtual bool ScheduleNestedContextViaCentalController() const
Definition: rewrite_context.h:628
bool stale_rewrite() const
Indicates whether we are serving a stale rewrite.
Definition: rewrite_context.h:599
int num_outputs() const
Definition: rewrite_context.h:207
static bool LookupMetadataForOutputResourceImpl(OutputResourcePtr output_resource, const GoogleUrl &gurl, RewriteContext *rewrite_context, RewriteDriver *driver, GoogleString *error_out, CacheLookupResultCallback *callback)
void AddNestedContext(RewriteContext *context)
void ReleaseCreationLock(bool succeeded)
virtual OutputResourceKind kind() const =0
void ObtainLockForCreation(ServerContext *server_context, Function *callback)
virtual bool ScheduleViaCentralController()
Definition: rewrite_context.h:621
static void InitStats(Statistics *stats)
Initializes statistics.
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context.h:164
virtual GoogleString UserAgentCacheKey(const ResourceContext *context) const
Definition: rewrite_context.h:486
void Freshen()
Freshens resources proactively to avoid expiration in the near future.
int num_output_partitions() const
void TracePrintf(const char *fmt,...)
virtual bool DecodeFetchUrls(const OutputResourcePtr &output_resource, MessageHandler *message_handler, GoogleUrlStarVector *url_vector)
Definition: ref_counted_ptr.h:69
CspDirective
Definition: csp_directive.h:37
bool IsNestedIn(StringPiece id) const
virtual bool FailOnHashMismatch() const
Definition: rewrite_context.h:616
void RewriteDone(RewriteResult result, int partition_index)
Definition: function.h:47
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void PartitionDone(RewriteResult result)
bool Fetch(const OutputResourcePtr &output_resource, AsyncFetch *fetch, MessageHandler *message_handler)
ServerContext * FindServerContext() const
void CrossThreadPartitionDone(RewriteResult result)
Used for LookupMetadataForOutputResource.
Definition: rewrite_context.h:181
virtual const char * id() const =0
Returns the filter ID.
virtual bool OptimizationOnly() const
Definition: rewrite_context.h:333
Definition: async_fetch.h:53
virtual void StartFetchReconstruction()
int num_nested() const
Accessors for the nested rewrites.
Definition: rewrite_context.h:284
virtual void FixFetchFallbackHeaders(const CachedResult &cached_result, ResponseHeaders *headers)
RequestTrace * dependent_request_trace()
Definition: rewrite_context.h:521
void AddRecheckDependency()
Definition: rewrite_driver.h:100
virtual bool SendFallbackResponse(StringPiece output_url_base, StringPiece contents, AsyncFetch *async_fetch, MessageHandler *handler)
Definition: server_context.h:99
RewriteResult
Definition: rewrite_result.h:27
virtual GoogleString CacheKeySuffix() const
bool chained() const
Definition: rewrite_context.h:218
Definition: rewrite_context.h:155
int num_slots() const
Definition: rewrite_context.h:202
virtual bool CreationLockBeforeStartFetch() const
Should the context call LockForCreation before checking the cache?
virtual bool PolicyPermitsRendering() const =0
virtual void Rewrite(int partition_index, CachedResult *partition, const OutputResourcePtr &output)=0
Definition: url_segment_encoder.h:33
virtual void FetchCallbackDone(bool success)
virtual void EncodeUserAgentIntoResourceContext(ResourceContext *context)
Definition: rewrite_context.h:495
Definition: message_handler.h:39
GoogleString ToString() const
Returns debug information about this RewriteContext.
Definition: request_trace.h:31
virtual int64 GetRewriteDeadlineAlarmMs() const
bool slow() const
Definition: rewrite_context.h:263
bool is_metadata_cache_miss() const
This particular rewrite was a metadata cache miss.
Definition: rewrite_context.h:266
MessageHandler * fetch_message_handler()
The message handler for the fetch.
virtual void PartitionAsync(OutputPartitions *partitions, OutputResourceVector *outputs)
virtual void WillNotRender()
bool FetchContextDetached()
Is fetch_ detached? Only call this in the fetch flow.
RewriteContext(RewriteDriver *driver, RewriteContext *parent, ResourceContext *resource_context)
void AddSlot(const ResourceSlotPtr &slot)
virtual void FetchTryFallback(const GoogleString &url, const StringPiece &hash)
bool has_parent() const
Returns true if this is a nested rewriter.
Definition: rewrite_context.h:269
void set_force_rewrite(bool x)
If called with true, forces a rewrite and re-generates the output.
Definition: rewrite_context.h:292
bool AreOutputsAllowedByCsp(CspDirective role) const
Helper that checks that all output resources are OK with CSP as given role.
void AttachDependentRequestTrace(const StringPiece &label)
OutputResourceKind
Definition: output_resource_kind.h:26
AsyncFetch * async_fetch()
Note that the following must only be called in the fetch flow.