Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
in_place_rewrite_context.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 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_IN_PLACE_REWRITE_CONTEXT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_IN_PLACE_REWRITE_CONTEXT_H_
21 
34 #include "net/instaweb/util/public/proto_util.h"
38 
39 namespace net_instaweb {
40 
41 class CachedResult;
42 class CacheUrlAsyncFetcher;
43 class HtmlElement;
44 class InputInfo;
45 class MessageHandler;
46 class ResourceContext;
47 class ResponseHeaders;
48 class RewriteDriver;
49 class RewriteFilter;
50 class Statistics;
51 class Variable;
52 
57  public:
58  static const char kIproSlotLocation[];
59  explicit InPlaceRewriteResourceSlot(const ResourcePtr& resource);
60 
61  virtual HtmlElement* element() const { return NULL; }
62 
64  virtual void Render();
65 
67  virtual GoogleString LocationString();
68 
69  protected:
70  virtual ~InPlaceRewriteResourceSlot();
71 
72  private:
73  DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteResourceSlot);
74 };
75 
78  public:
82  static const char kInPlaceOversizedOptStream[];
83  static const char kInPlaceUncacheableRewrites[];
84 
85  InPlaceRewriteContext(RewriteDriver* driver, const StringPiece& url);
86  virtual ~InPlaceRewriteContext();
87 
89  virtual void RewriteSingle(const ResourcePtr& input,
90  const OutputResourcePtr& output);
92  virtual const char* id() const { return RewriteOptions::kInPlaceRewriteId; }
94  virtual OutputResourceKind kind() const { return kRewrittenResource; }
96  virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
97  MessageHandler* message_handler,
98  GoogleUrlStarVector* url_vector);
100  virtual void StartFetchReconstruction();
101 
102  static void InitStats(Statistics* statistics);
103 
104  bool proxy_mode() const { return proxy_mode_; }
105  void set_proxy_mode(bool x) { proxy_mode_ = x; }
106 
107  virtual int64 GetRewriteDeadlineAlarmMs() const;
108 
109  virtual GoogleString UserAgentCacheKey(
110  const ResourceContext* resource_context) const;
111  virtual void EncodeUserAgentIntoResourceContext(ResourceContext* context);
112 
115  virtual bool CreationLockBeforeStartFetch() { return false; }
116 
117  private:
118  friend class RecordingFetch;
120  virtual void Harvest();
121  void StartFetchReconstructionParent();
123  virtual void FixFetchFallbackHeaders(const CachedResult& cached_result,
124  ResponseHeaders* headers);
126  virtual void FetchTryFallback(const GoogleString& url,
127  const StringPiece& hash);
129  virtual void FetchCallbackDone(bool success);
130 
131  RewriteFilter* GetRewriteFilter(const ContentType& type);
132 
134  void UpdateDateAndExpiry(const protobuf::RepeatedPtrField<InputInfo>& inputs,
135  int64* date_ms, int64* expiry_ms);
138  bool InPlaceOptimizeForBrowserEnabled() const;
141  void AddVaryIfRequired(const CachedResult& cached_result,
142  ResponseHeaders* headers) const;
143 
144  GoogleString url_;
146  bool is_rewritten_;
149  GoogleString rewritten_hash_;
150 
152  ResourcePtr input_resource_;
153  OutputResourcePtr output_resource_;
154 
155  scoped_ptr<CacheUrlAsyncFetcher> cache_fetcher_;
156 
166  bool proxy_mode_;
167 
168  DISALLOW_COPY_AND_ASSIGN(InPlaceRewriteContext);
169 };
170 
174  public:
175  RecordingFetch(bool proxy_mode,
176  AsyncFetch* async_fetch,
177  const ResourcePtr& resource,
178  InPlaceRewriteContext* context,
179  MessageHandler* handler);
180 
181  virtual ~RecordingFetch();
182 
184  virtual void HandleHeadersComplete();
186  virtual bool HandleWrite(const StringPiece& content, MessageHandler* handler);
188  virtual bool HandleFlush(MessageHandler* handler);
190  virtual void HandleDone(bool success);
191 
192  private:
193  void FreeDriver();
194 
195  bool CanInPlaceRewrite();
196 
200  bool ShouldStream() const;
201 
202  bool proxy_mode_;
203  MessageHandler* handler_;
204  ResourcePtr resource_;
205  InPlaceRewriteContext* context_;
206 
209  bool can_in_place_rewrite_;
210 
212  bool streaming_;
213  HTTPValue cache_value_;
214  HTTPValueWriter cache_value_writer_;
215  scoped_ptr<ResponseHeaders> saved_headers_;
216  Variable* in_place_oversized_opt_stream_;
217  Variable* in_place_uncacheable_rewrites_;
218  DISALLOW_COPY_AND_ASSIGN(RecordingFetch);
219 };
220 
221 }
222 
223 #endif
virtual int64 GetRewriteDeadlineAlarmMs() const
virtual void HandleDone(bool success)
Implements SharedAsyncFetch::HandleDone().
Definition: http_value.h:36
virtual bool HandleFlush(MessageHandler *handler)
Implements SharedAsyncFetch::HandleFlush().
virtual bool DecodeFetchUrls(const OutputResourcePtr &output_resource, MessageHandler *message_handler, GoogleUrlStarVector *url_vector)
Implements RewriteContext::DecodeFetchUrls().
virtual GoogleString UserAgentCacheKey(const ResourceContext *resource_context) const
static const char kInPlaceOversizedOptStream[]
Definition: in_place_rewrite_context.h:82
virtual GoogleString LocationString()
Implements ResourceSlot::LocationString().
Definition: resource_slot.h:53
virtual bool CreationLockBeforeStartFetch()
Definition: in_place_rewrite_context.h:115
Definition: in_place_rewrite_context.h:173
virtual OutputResourceKind kind() const
Implements RewriteContext::kind().
Definition: in_place_rewrite_context.h:94
virtual const char * id() const
Implements RewriteContext::id().
Definition: in_place_rewrite_context.h:92
Definition: async_fetch.h:267
Definition: async_fetch.h:53
Definition: rewrite_filter.h:37
virtual void StartFetchReconstruction()
Implements RewriteContext::StartFetchReconstruction().
Definition: rewrite_driver.h:98
derived from some input resource URL or URLs.
Definition: output_resource_kind.h:27
Context that is used for an in-place rewrite.
Definition: in_place_rewrite_context.h:77
virtual void HandleHeadersComplete()
Implements SharedAsyncFetch::HandleHeadersComplete().
virtual bool HandleWrite(const StringPiece &content, MessageHandler *handler)
Implements SharedAsyncFetch::HandleWrite().
Definition: single_rewrite_context.h:36
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: in_place_rewrite_context.h:61
Definition: in_place_rewrite_context.h:56
virtual void EncodeUserAgentIntoResourceContext(ResourceContext *context)
virtual void Render()
Implements ResourceSlot::Render().
Definition: http_value_writer.h:33
OutputResourceKind
Definition: output_resource_kind.h:26
virtual void RewriteSingle(const ResourcePtr &input, const OutputResourcePtr &output)
Implements SingleRewriteContext::RewriteSingle().