Page Speed Optimization Libraries  1.2.24.1
net/instaweb/rewriter/public/split_html_filter.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 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_SPLIT_HTML_FILTER_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_SPLIT_HTML_FILTER_H_
00021 
00022 #include <map>
00023 #include <vector>
00024 
00025 #include "net/instaweb/rewriter/public/suppress_prehead_filter.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/json.h"
00028 #include "net/instaweb/util/public/json_writer.h"
00029 #include "net/instaweb/util/public/scoped_ptr.h"
00030 #include "net/instaweb/util/public/string.h"
00031 #include "net/instaweb/util/public/string_util.h"
00032 
00033 namespace net_instaweb {
00034 
00035 struct XpathUnit {
00036   GoogleString tag_name;
00037   GoogleString attribute_value;
00038   int child_number;
00039 };
00040 
00041 class CriticalLineInfo;
00042 class HtmlElement;
00043 class Panel;
00044 class RewriteDriver;
00045 class RewriteOptions;
00046 class StaticJavascriptManager;
00047 class Writer;
00048 
00049 typedef std::map<GoogleString, const Panel*> PanelIdToSpecMap;
00050 typedef std::vector<XpathUnit> XpathUnits;
00052 typedef std::map<GoogleString, XpathUnits*> XpathMap;
00053 
00056 
00059 class SplitHtmlFilter : public SuppressPreheadFilter {
00060  public:
00061   static const char kSplitInit[];
00062   static const char kPagespeedFunc[];
00063   static const char kSplitSuffixJsFormatString[];
00064 
00065   explicit SplitHtmlFilter(RewriteDriver* rewrite_driver);
00066   virtual ~SplitHtmlFilter();
00067 
00068   virtual void StartDocument();
00069   virtual void EndDocument();
00070 
00071   virtual void StartElement(HtmlElement* element);
00072   virtual void EndElement(HtmlElement* element);
00073 
00074   static const GoogleString& GetBlinkJsUrl(
00075       const RewriteOptions* options,
00076       StaticJavascriptManager* static_js_manager);
00077 
00078   virtual const char* Name() const { return "SplitHtmlFilter"; }
00079 
00080  private:
00081   void ServeNonCriticalPanelContents(const Json::Value& json);
00082 
00085   void MarkElementWithPanelId(HtmlElement* element,
00086                               const GoogleString& panel_id);
00087 
00089   GoogleString GetPanelIdForInstance(HtmlElement* element);
00090 
00092   GoogleString MatchPanelIdForElement(HtmlElement* element);
00093 
00096   bool IsElementSiblingOfCurrentPanel(HtmlElement* element);
00097 
00099   bool IsElementParentOfCurrentPanel(HtmlElement* element);
00100 
00103   void EndPanelInstance();
00104 
00106   void StartPanelInstance(HtmlElement* element);
00107 
00109   void InsertPanelStub(HtmlElement* element, const GoogleString& panel_id);
00110 
00113   bool IsEndMarkerForCurrentPanel(HtmlElement* element);
00114 
00116   void ProcessCriticalLineConfig();
00117 
00119   void PopulateXpathMap(const CriticalLineInfo& critical_line_info);
00120 
00122   void PopulateXpathMap(const GoogleString& xpath);
00123 
00125   void AppendJsonData(Json::Value* dictionary, const Json::Value& dict);
00126 
00127   void WriteString(const StringPiece& str);
00128 
00129   void Cleanup();
00130 
00134   void InsertSplitInitScripts(HtmlElement* element);
00135 
00136   bool ElementMatchesXpath(const HtmlElement* element,
00137                            const std::vector<XpathUnit>& xpath_units);
00138 
00139   bool ParseXpath(const GoogleString& xpath,
00140                   std::vector<XpathUnit>* xpath_units);
00141 
00142   void ComputePanels(const CriticalLineInfo& critical_line_info,
00143                      PanelIdToSpecMap* panel_id_to_spec);
00144 
00145   void InvokeBaseHtmlFilterStartDocument();
00146 
00147   void InvokeBaseHtmlFilterStartElement(HtmlElement* element);
00148 
00149   void InvokeBaseHtmlFilterEndElement(HtmlElement* element);
00150 
00151   void InvokeBaseHtmlFilterEndDocument();
00152 
00153   RewriteDriver* rewrite_driver_;
00154   const RewriteOptions* options_;
00155   PanelIdToSpecMap panel_id_to_spec_;
00156   XpathMap xpath_map_;
00157   std::vector<ElementJsonPair> element_json_stack_;
00158   std::vector<std::vector<XpathUnit> > xpath_units_;
00159   std::vector<int> num_children_stack_;
00160   Json::FastWriter fast_writer_;
00161   scoped_ptr<JsonWriter> json_writer_;
00162   Writer* original_writer_;
00163   const CriticalLineInfo* critical_line_info_; 
00164   GoogleString current_panel_id_;
00165   StringPiece url_;
00166   bool script_written_;
00167   bool flush_head_enabled_;
00168   bool disable_filter_;
00169   bool send_lazyload_script_;
00170   int num_low_res_images_inlined_;
00171   HtmlElement* current_panel_parent_element_;
00172   StaticJavascriptManager* static_js_manager_; 
00173 
00174   DISALLOW_COPY_AND_ASSIGN(SplitHtmlFilter);
00175 };
00176 
00177 }  
00178 
00179 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_SPLIT_HTML_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines