Page Speed Optimization Libraries  1.7.30.2
net/instaweb/rewriter/public/css_summarizer_base.h
Go to the documentation of this file.
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 RewriteContext;
00043 class RewriteDriver;
00044 
00049 class CssSummarizerBase : public RewriteFilter {
00050  public:
00051   explicit CssSummarizerBase(RewriteDriver* driver);
00052   virtual ~CssSummarizerBase();
00053 
00054  protected:
00055   enum SummaryState {
00057     kSummaryOk,
00058 
00060     kSummaryStillPending,
00061 
00063     kSummaryCssParseError,
00064 
00067     kSummaryResourceCreationFailed,
00068 
00070     kSummaryInputUnavailable,
00071 
00073     kSummarySlotRemoved,
00074   };
00075 
00076   struct SummaryInfo {
00077     SummaryInfo()
00078         : state(kSummaryStillPending),
00079           is_external(false),
00080           is_inside_noscript(false) {}
00081 
00084     GoogleString data;
00085 
00087     SummaryState state;
00088 
00091     GoogleString location;
00092 
00094     GoogleString base;
00095 
00097     GoogleString media_from_html;
00098 
00100     GoogleString rel;
00101 
00103     bool is_external;
00104 
00106     bool is_inside_noscript;
00107   };
00108 
00111   virtual GoogleString CacheKeySuffix() const;
00112 
00117   virtual bool MustSummarize(HtmlElement* element) const {
00118     return true;
00119   }
00120 
00128   virtual void Summarize(Css::Stylesheet* stylesheet,
00129                          GoogleString* out) const = 0;
00130 
00147   virtual void RenderSummary(int pos,
00148                              HtmlElement* element,
00149                              HtmlCharactersNode* char_node,
00150                              bool* is_element_deleted);
00151 
00160   virtual void WillNotRenderSummary(int pos,
00161                                     HtmlElement* element,
00162                                     HtmlCharactersNode* char_node,
00163                                     bool* is_element_deleted);
00164 
00179   virtual void SummariesDone();
00180 
00185   int NumStyles() const { return static_cast<int>(summaries_.size()); }
00186 
00192   const SummaryInfo& GetSummaryForStyle(int pos) const {
00193     return summaries_.at(pos);
00194   }
00195 
00198   virtual void StartDocumentImpl();
00199   virtual void EndDocument();
00200   virtual void StartElementImpl(HtmlElement* element);
00201   virtual void Characters(HtmlCharactersNode* characters);
00202   virtual void EndElementImpl(HtmlElement* element);
00203   virtual void RenderDone();
00204 
00205   virtual RewriteContext* MakeRewriteContext();
00206 
00207  private:
00208   class Context;
00209 
00211   void Clear();
00212 
00215   void ReportSummariesDone();
00216 
00219   void StartInlineRewrite(HtmlElement* style, HtmlCharactersNode* text);
00220 
00223   void StartExternalRewrite(HtmlElement* link, HtmlElement::Attribute* src,
00224                             StringPiece rel);
00225 
00231   Context* CreateContextAndSummaryInfo(const HtmlElement* element,
00232                                        bool external,
00233                                        const ResourceSlotPtr& slot,
00234                                        const GoogleString& location,
00235                                        StringPiece base_for_resources,
00236                                        StringPiece rel);
00237 
00238   ResourceSlot* MakeSlotForInlineCss(const StringPiece& content);
00239 
00241   std::vector<SummaryInfo> summaries_;
00242 
00243   scoped_ptr<AbstractMutex> progress_lock_;
00244   int outstanding_rewrites_; 
00245   bool saw_end_of_document_; 
00246 
00247 
00248 
00249   std::vector<int> canceled_summaries_; 
00250 
00251   HtmlElement* style_element_; 
00252 
00253   DISALLOW_COPY_AND_ASSIGN(CssSummarizerBase);
00254 };
00255 
00256 }  
00257 
00258 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines