Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
srcset_slot.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 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 
20 
21 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_SRCSET_SLOT_H_
22 #define NET_INSTAWEB_REWRITER_PUBLIC_SRCSET_SLOT_H_
23 
24 #include <memory>
25 #include <set>
26 #include <vector>
27 
36 
37 namespace net_instaweb {
38 
39 class CommonFilter;
40 class RewriteDriver;
41 class SrcSetSlot;
42 
45 class SrcSetSlotCollection : public RefCounted<SrcSetSlotCollection> {
46  public:
47  struct ImageCandidate {
48  ImageCandidate() : slot(nullptr) {}
49 
50  GoogleString url;
51  GoogleString descriptor;
52 
56  };
57 
62  HtmlElement* element,
63  HtmlElement::Attribute* attribute);
64 
68 
69  int num_image_candidates() { return candidates_.size(); }
70 
72  SrcSetSlot* slot(int idx) { return candidates_[idx].slot; }
73  const GoogleString& url(int idx) { return candidates_[idx].url; }
74  void set_url(int idx, GoogleString new_url) {
75  candidates_[idx].url = new_url;
76  }
77  const GoogleString& descriptor(int idx) {
78  return candidates_[idx].descriptor;
79  }
80 
81  HtmlElement* element() const { return element_; }
82  HtmlElement::Attribute* attribute() const { return attribute_; }
83 
85  CommonFilter* filter() const { return filter_; }
86  RewriteDriver* driver() { return driver_; }
87 
88  int begin_line_number() const { return begin_line_number_; }
89  int end_line_number() const { return end_line_number_; }
90 
94  void Commit();
95 
99  static void ParseSrcSet(StringPiece input, std::vector<ImageCandidate>* out);
100 
101  static GoogleString Serialize(const std::vector<ImageCandidate>& in);
102 
103  protected:
104  virtual ~SrcSetSlotCollection() {}
105  REFCOUNT_FRIEND_DECLARATION(SrcSetSlotCollection);
106 
107  private:
108  std::vector<ImageCandidate> candidates_;
109  RewriteDriver* driver_;
110  HtmlElement* element_;
111  HtmlElement::Attribute* attribute_;
112  CommonFilter* filter_;
113  int begin_line_number_;
114  int end_line_number_;
115 
116 
117 };
118 
119 typedef RefCountedPtr<SrcSetSlotCollection> SrcSetSlotCollectionPtr;
120 
123  public:
124  bool operator()(const SrcSetSlotCollectionPtr& p,
125  const SrcSetSlotCollectionPtr& q) const;
126 };
127 
128 typedef std::set<SrcSetSlotCollectionPtr,
129  SrcSetSlotCollectionComparator> SrcSetSlotCollectionSet;
130 
131 
132 class SrcSetSlot : public ResourceSlot {
133  public:
134  HtmlElement* element() const override { return parent_->element(); }
135  void Render() override;
136  GoogleString LocationString() const override;
137 
138  protected:
139  friend class SrcSetSlotCollection;
140  SrcSetSlot(const ResourcePtr& resource,
141  SrcSetSlotCollection* parent,
142  int index);
143 
145  ~SrcSetSlot() override;
146 
147  private:
149  int index_;
150  UrlRelativity url_relativity_;
151 
152 
153 };
154 
155 }
156 
157 #endif
HtmlElement * element() const override
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: srcset_slot.h:134
Definition: common_filter.h:47
Definition: html_element.h:66
void Render() override
GoogleString LocationString() const override
Definition: srcset_slot.h:132
Definition: ref_counted_ptr.h:41
Definition: resource_slot.h:54
Definition: html_element.h:42
Definition: ref_counted_ptr.h:69
SrcSetSlotCollection(RewriteDriver *driver, HtmlElement *element, HtmlElement::Attribute *attribute)
Definition: srcset_slot.h:45
static void ParseSrcSet(StringPiece input, std::vector< ImageCandidate > *out)
CommonFilter * filter() const
The first filter that created this slot collection. There may be others.
Definition: srcset_slot.h:85
SrcSetSlot * slot(int idx)
may be nullptr.
Definition: srcset_slot.h:72
void Initialize(CommonFilter *filter)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: rewrite_driver.h:100
UrlRelativity
namespace url
Definition: google_url.h:51
Note: this is non-deterministic between executions.
Definition: srcset_slot.h:122
SrcSetSlot * slot
Definition: srcset_slot.h:55
#define REFCOUNT_FRIEND_DECLARATION(class_name)
Definition: ref_counted_ptr.h:209