Page Speed Optimization Libraries  1.11.33.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
resource_slot.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_RESOURCE_SLOT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
21 
22 #include <set>
23 #include <vector>
24 
26 #include "pagespeed/kernel/base/basictypes.h"
27 #include "pagespeed/kernel/base/ref_counted_ptr.h"
28 #include "pagespeed/kernel/base/string.h"
29 #include "pagespeed/kernel/base/string_util.h"
30 #include "pagespeed/kernel/base/vector_deque.h"
31 #include "pagespeed/kernel/html/html_element.h"
32 #include "pagespeed/kernel/http/google_url.h"
33 
34 namespace net_instaweb {
35 
36 class CachedResults;
37 class HtmlResourceSlot;
38 class ResourceSlot;
39 class RewriteContext;
40 class RewriteDriver;
41 class RewriteOptions;
42 
43 typedef RefCountedPtr<ResourceSlot> ResourceSlotPtr;
44 typedef RefCountedPtr<HtmlResourceSlot> HtmlResourceSlotPtr;
45 typedef std::vector<ResourceSlotPtr> ResourceSlotVector;
46 
53 class ResourceSlot : public RefCounted<ResourceSlot> {
54  public:
55  explicit ResourceSlot(const ResourcePtr& resource)
56  : resource_(resource),
57  preserve_urls_(false),
58  disable_rendering_(false),
59  should_delete_element_(false),
60  disable_further_processing_(false),
61  was_optimized_(false) {
62  }
63 
64  ResourcePtr resource() const { return resource_; }
66  virtual HtmlElement* element() const = 0;
67 
81  void SetResource(const ResourcePtr& resource);
82 
85  void set_preserve_urls(bool x) { preserve_urls_ = x; }
86  bool preserve_urls() const { return preserve_urls_; }
87 
91  void set_disable_rendering(bool x) { disable_rendering_ = x; }
92  bool disable_rendering() const { return disable_rendering_; }
93 
101  should_delete_element_ = true;
102  disable_further_processing_ = true;
103  }
104  bool should_delete_element() const { return should_delete_element_; }
105 
112  bool was_optimized() const { return was_optimized_; }
113 
115  void set_was_optimized(bool x) { was_optimized_ = x; }
116 
125  disable_further_processing_ = x;
126  }
127 
128  bool disable_further_processing() const {
129  return disable_further_processing_;
130  }
131 
137  virtual void Render() = 0;
138 
142  virtual void Finished() {}
143 
154  virtual bool DirectSetUrl(const StringPiece& url);
155 
158  virtual bool CanDirectSetUrl() { return false; }
159 
162  RewriteContext* LastContext() const;
163 
165  void AddContext(RewriteContext* context) { contexts_.push_back(context); }
166 
169  void DetachContext(RewriteContext* context);
170 
173  virtual GoogleString LocationString() const = 0;
174 
178  static GoogleString RelativizeOrPassthrough(const RewriteOptions* options,
179  StringPiece url,
180  UrlRelativity url_relativity,
181  const GoogleUrl& base_url);
182 
183  protected:
184  virtual ~ResourceSlot();
185  REFCOUNT_FRIEND_DECLARATION(ResourceSlot);
186 
187  private:
188  ResourcePtr resource_;
189  bool preserve_urls_;
190  bool disable_rendering_;
191  bool should_delete_element_;
192  bool disable_further_processing_;
193  bool was_optimized_;
194 
197  VectorDeque<RewriteContext*> contexts_;
198 
199 
200 };
201 
205  public:
206  NullResourceSlot(const ResourcePtr& resource, StringPiece location);
207  virtual HtmlElement* element() const { return NULL; }
208  virtual void Render() {}
209  virtual GoogleString LocationString() const { return location_; }
210 
211  protected:
212  REFCOUNT_FRIEND_DECLARATION(NullResourceSlot);
213  virtual ~NullResourceSlot();
214 
215  private:
216  GoogleString location_;
217 
218 
219 };
220 
224  public:
225  explicit FetchResourceSlot(const ResourcePtr& resource)
226  : ResourceSlot(resource) {
227  }
228  virtual HtmlElement* element() const { return NULL; }
229  virtual void Render();
230  virtual GoogleString LocationString() const;
231 
232  protected:
233  REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot);
234  virtual ~FetchResourceSlot();
235 
236  private:
237 
238 };
239 
241  public:
242  HtmlResourceSlot(const ResourcePtr& resource,
243  HtmlElement* element,
244  HtmlElement::Attribute* attribute,
245  RewriteDriver* driver);
246 
247  virtual HtmlElement* element() const { return element_; }
248  HtmlElement::Attribute* attribute() const { return attribute_; }
249 
250  virtual void Render();
251  virtual GoogleString LocationString() const;
252  virtual bool DirectSetUrl(const StringPiece& url);
253  virtual bool CanDirectSetUrl() { return true; }
254 
257  UrlRelativity url_relativity() const { return url_relativity_; }
258 
259  protected:
260  REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot);
261  virtual ~HtmlResourceSlot();
262 
263  private:
264  HtmlElement* element_;
265  HtmlElement::Attribute* attribute_;
266  RewriteDriver* driver_;
267  UrlRelativity url_relativity_;
268 
269  int begin_line_number_;
270  int end_line_number_;
271 
272 
273 };
274 
276  public:
277  bool operator()(const HtmlResourceSlotPtr& p,
278  const HtmlResourceSlotPtr& q) const;
279 };
280 
281 typedef std::set<HtmlResourceSlotPtr,
282  HtmlResourceSlotComparator> HtmlResourceSlotSet;
283 
284 }
285 
286 #endif
virtual void Finished()
Definition: resource_slot.h:142
void set_disable_rendering(bool x)
Definition: resource_slot.h:91
bool was_optimized() const
Definition: resource_slot.h:112
virtual GoogleString LocationString() const
Definition: resource_slot.h:209
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:207
void SetResource(const ResourcePtr &resource)
virtual void Render()=0
static GoogleString RelativizeOrPassthrough(const RewriteOptions *options, StringPiece url, UrlRelativity url_relativity, const GoogleUrl &base_url)
Definition: resource_slot.h:53
virtual GoogleString LocationString() const
virtual bool DirectSetUrl(const StringPiece &url)
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:158
Definition: resource_slot.h:240
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:247
void RequestDeleteElement()
Definition: resource_slot.h:100
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:228
virtual GoogleString LocationString() const
void set_disable_further_processing(bool x)
Definition: resource_slot.h:124
UrlRelativity url_relativity() const
Definition: resource_slot.h:257
Definition: resource_slot.h:223
Definition: rewrite_driver.h:98
RewriteContext * LastContext() const
void AddContext(RewriteContext *context)
Adds a new context to this slot.
Definition: resource_slot.h:165
Definition: resource_slot.h:204
virtual bool DirectSetUrl(const StringPiece &url)
Definition: rewrite_context.h:146
virtual GoogleString LocationString() const =0
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:253
Definition: resource_slot.h:275
virtual void Render()
Definition: resource_slot.h:208
Definition: rewrite_options.h:84
void DetachContext(RewriteContext *context)
virtual HtmlElement * element() const =0
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
void set_was_optimized(bool x)
Marks the slot as having been optimized.
Definition: resource_slot.h:115
void set_preserve_urls(bool x)
Definition: resource_slot.h:85