1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 21 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_ADD_INSTRUMENTATION_FILTER_H_ 22 #define NET_INSTAWEB_REWRITER_PUBLIC_ADD_INSTRUMENTATION_FILTER_H_ 23 24 #include "net/instaweb/rewriter/public/common_filter.h" 25 #include "net/instaweb/rewriter/public/rewrite_driver.h" 26 #include "pagespeed/kernel/base/basictypes.h" 27 #include "pagespeed/kernel/base/string.h" 28 #include "pagespeed/kernel/base/string_util.h" 29 #include "pagespeed/kernel/html/html_element.h" 30 #include "pagespeed/kernel/html/html_filter.h" 31 32 namespace net_instaweb { 33 34 class Statistics; 35 class Variable; 36 37 // Injects javascript instrumentation for monitoring page-rendering time. 38 class AddInstrumentationFilter : public CommonFilter { 39 public: 40 static const char kLoadTag[]; 41 static const char kUnloadTag[]; 42 static GoogleString* kUnloadScriptFormatXhtml; 43 static GoogleString* kTailScriptFormatXhtml; 44 45 // Counters. 46 static const char kInstrumentationScriptAddedCount[]; 47 48 explicit AddInstrumentationFilter(RewriteDriver* driver); 49 virtual ~AddInstrumentationFilter(); 50 51 static void InitStats(Statistics* statistics); 52 53 virtual void StartDocumentImpl(); 54 virtual void EndDocument(); 55 virtual void StartElementImpl(HtmlElement* element); 56 virtual void EndElementImpl(HtmlElement* element); 57 virtual const char* Name() const { return "AddInstrumentation"; } 58 59 ScriptUsage GetScriptUsage() const override { return kWillInjectScripts; } 60 61 protected: 62 virtual void DetermineEnabled(GoogleString* disabled_reason); 63 64 // The total number of times instrumentation script is added. 65 Variable* instrumentation_script_added_count_; 66 67 private: 68 // Returns JS using the specified event. 69 GoogleString GetScriptJs(StringPiece event); 70 71 // Adds the kHeadScript just before the current event only if the element is 72 // not a <title> or <meta>. 73 void AddHeadScript(HtmlElement* element); 74 75 bool found_head_; 76 bool added_head_script_; 77 bool added_unload_script_; 78 79 DISALLOW_COPY_AND_ASSIGN(AddInstrumentationFilter); 80 }; 81 82 } // namespace net_instaweb 83 84 #endif // NET_INSTAWEB_REWRITER_PUBLIC_ADD_INSTRUMENTATION_FILTER_H_