Page Speed Optimization Libraries  1.7.30.1
net/instaweb/rewriter/public/google_analytics_filter.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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 
00047 
00048 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_GOOGLE_ANALYTICS_FILTER_H_
00049 #define NET_INSTAWEB_REWRITER_PUBLIC_GOOGLE_ANALYTICS_FILTER_H_
00050 
00051 #include <vector>
00052 
00053 #include "net/instaweb/htmlparse/public/empty_html_filter.h"
00054 #include "net/instaweb/util/public/basictypes.h"
00055 #include "net/instaweb/util/public/scoped_ptr.h"
00056 #include "net/instaweb/util/public/string.h"
00057 #include "net/instaweb/util/public/string_util.h"
00058 
00059 namespace net_instaweb {
00060 class HtmlCdataNode;
00061 class HtmlCharactersNode;
00062 class HtmlCommentNode;
00063 class HtmlElement;
00064 class HtmlIEDirectiveNode;
00065 class HtmlParse;
00066 class Statistics;
00067 class Variable;
00068 
00069 
00071 class ScriptEditor {
00072  public:
00073   enum Type {
00074     kGaJsScriptSrcLoad = 0,
00075     kGaJsDocWriteLoad,
00076     kGaJsInit,
00077   };
00078   ScriptEditor(HtmlElement* script_element_,
00079                HtmlCharactersNode* characters_node,
00080                GoogleString::size_type pos,
00081                GoogleString::size_type len,
00082                Type editor_type);
00083 
00084   HtmlElement* GetScriptElement() const { return script_element_; }
00085   HtmlCharactersNode* GetScriptCharactersNode() const {
00086     return script_characters_node_;
00087   }
00088   Type GetType() const { return editor_type_; }
00089 
00090   void NewContents(const StringPiece &replacement,
00091                    GoogleString* contents) const;
00092 
00093  private:
00094   HtmlElement* script_element_;
00095   HtmlCharactersNode* script_characters_node_;
00096 
00097   GoogleString::size_type pos_;
00098   GoogleString::size_type len_;
00099 
00100   Type editor_type_;
00101   DISALLOW_COPY_AND_ASSIGN(ScriptEditor);
00102 };
00103 
00104 
00107 class GoogleAnalyticsFilter : public EmptyHtmlFilter {
00108  public:
00109   typedef StringPieceVector MethodVector;
00110 
00111   explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
00112                                  Statistics* statistics);
00113   virtual ~GoogleAnalyticsFilter();
00114 
00116   explicit GoogleAnalyticsFilter(HtmlParse* html_parse,
00117                                  Statistics* statistics,
00118                                  MethodVector* glue_methods,
00119                                  MethodVector* unhandled_methods);
00120 
00121   static void InitStats(Statistics* statistics);
00122 
00123   virtual void StartDocument();
00124   virtual void EndDocument();
00125   virtual void StartElement(HtmlElement* element);
00126   virtual void EndElement(HtmlElement* element);
00127 
00128   virtual void Flush();
00129 
00131   virtual void Characters(HtmlCharactersNode* characters_node);
00132 
00134   virtual void Comment(HtmlCommentNode* comment);
00135   virtual void Cdata(HtmlCdataNode* cdata);
00136   virtual void IEDirective(HtmlIEDirectiveNode* directive);
00137 
00138   virtual const char* Name() const { return "GoogleAnalytics"; }
00139 
00140   static const char kPageLoadCount[];
00141   static const char kRewrittenCount[];
00142 
00143  private:
00144   void ResetFilter();
00145 
00146   bool MatchSyncLoad(StringPiece contents,
00147                      GoogleString::size_type* pos,
00148                      GoogleString::size_type* len) const;
00149   bool MatchSyncInit(StringPiece contents,
00150                      GoogleString::size_type start_pos,
00151                      GoogleString::size_type* pos,
00152                      GoogleString::size_type* len) const;
00153   bool MatchUnhandledCalls(StringPiece contents,
00154                            GoogleString::size_type start_pos) const;
00155   void FindRewritableScripts();
00156   void GetSyncToAsyncScript(GoogleString* buffer) const;
00157   bool RewriteAsAsync();
00158 
00159   bool is_load_found_;
00160   bool is_init_found_;
00161   std::vector<ScriptEditor*> script_editors_;
00162 
00163   scoped_ptr<MethodVector> glue_methods_; 
00164   scoped_ptr<MethodVector> unhandled_methods_; 
00165 
00166   HtmlParse* html_parse_;
00167   HtmlElement* script_element_; 
00168   HtmlCharactersNode* script_characters_node_; 
00169 
00170   Variable* page_load_count_;
00171   Variable* rewritten_count_;
00172 
00173   DISALLOW_COPY_AND_ASSIGN(GoogleAnalyticsFilter);
00174 };
00175 
00176 }  
00177 
00178 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_GOOGLE_ANALYTICS_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines