Page Speed Optimization Libraries
1.4.26.1
|
00001 /* 00002 * Copyright 2013 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_ 00021 00022 #include <vector> 00023 00024 #include "net/instaweb/htmlparse/public/html_element.h" 00025 #include "net/instaweb/rewriter/public/resource_slot.h" 00026 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/scoped_ptr.h" 00029 #include "net/instaweb/util/public/string.h" 00030 #include "net/instaweb/util/public/string_util.h" 00031 00032 namespace Css { 00033 00034 class Stylesheet; 00035 00036 } 00037 00038 namespace net_instaweb { 00039 00040 class AbstractMutex; 00041 class HtmlCharactersNode; 00042 class HtmlNode; 00043 class RewriteContext; 00044 class RewriteDriver; 00045 00050 class CssSummarizerBase : public RewriteFilter { 00051 public: 00052 explicit CssSummarizerBase(RewriteDriver* driver); 00053 virtual ~CssSummarizerBase(); 00054 00055 protected: 00056 enum SummaryState { 00058 kSummaryOk, 00059 00061 kSummaryStillPending, 00062 00064 kSummaryCssParseError, 00065 00068 kSummaryResourceCreationFailed, 00069 00071 kSummaryInputUnavailable 00072 }; 00073 00074 struct SummaryInfo { 00075 SummaryInfo() : state(kSummaryStillPending) {} 00076 00079 GoogleString data; 00080 00082 SummaryState state; 00083 00086 GoogleString location; 00087 }; 00088 00091 virtual GoogleString CacheKeySuffix() const; 00092 00100 virtual void Summarize(Css::Stylesheet* stylesheet, 00101 GoogleString* out) const = 0; 00102 00110 virtual void SummariesDone() = 0; 00111 00118 void InjectSummaryData(HtmlNode* data); 00119 00124 int NumStyles() const { return static_cast<int>(summaries_.size()); } 00125 00131 const SummaryInfo& GetSummaryForStyle(int pos) const { 00132 return summaries_.at(pos); 00133 } 00134 00141 virtual void NotifyInlineCss(HtmlElement* style_element, 00142 HtmlCharactersNode* content); 00143 virtual void NotifyExternalCss(HtmlElement* link); 00144 00147 virtual void StartDocumentImpl(); 00148 virtual void EndDocument(); 00149 virtual void StartElementImpl(HtmlElement* element); 00150 virtual void Characters(HtmlCharactersNode* characters); 00151 virtual void EndElementImpl(HtmlElement* element); 00152 00153 virtual RewriteContext* MakeRewriteContext(); 00154 00155 private: 00156 class Context; 00157 00159 void Clear(); 00160 00163 void ReportSummariesDone(); 00164 00166 void StartInlineRewrite(HtmlCharactersNode* text); 00167 00170 void StartExternalRewrite(HtmlElement* link, HtmlElement::Attribute* src); 00171 00177 Context* CreateContextForSlot(const ResourceSlotPtr& slot, 00178 const GoogleString& location); 00179 00180 ResourceSlot* MakeSlotForInlineCss(const StringPiece& content); 00181 00183 std::vector<SummaryInfo> summaries_; 00184 00185 scoped_ptr<AbstractMutex> progress_lock_; 00186 int outstanding_rewrites_; 00187 bool saw_end_of_document_; 00188 00189 HtmlElement* style_element_; 00190 HtmlElement* injection_point_; 00191 00192 DISALLOW_COPY_AND_ASSIGN(CssSummarizerBase); 00193 }; 00194 00195 } 00196 00197 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_