~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/rewriter/public/critical_css_beacon_filter.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Google Inc.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
// Author: jmaessen@google.com (Jan-Willem Maessen)
18
 
 
19
 
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_
20
 
#define NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_
21
 
 
22
 
#include "net/instaweb/http/public/semantic_type.h"
23
 
#include "net/instaweb/rewriter/public/css_summarizer_base.h"
24
 
#include "net/instaweb/rewriter/public/rewrite_driver.h"
25
 
#include "net/instaweb/rewriter/public/rewrite_options.h"
26
 
#include "net/instaweb/util/public/basictypes.h"
27
 
#include "net/instaweb/util/public/string.h"
28
 
#include "net/instaweb/util/public/string_util.h"
29
 
 
30
 
namespace Css {
31
 
 
32
 
class Ruleset;
33
 
class Stylesheet;
34
 
 
35
 
}  // namespace Css
36
 
 
37
 
namespace net_instaweb {
38
 
 
39
 
struct BeaconMetadata;
40
 
class HtmlElement;
41
 
class Statistics;
42
 
class Variable;
43
 
 
44
 
// Figure out the set of CSS selectors referenced from a page, saving those
45
 
// selectors in an OutputResource for each CSS <style> or <link> on the page.
46
 
// Based on that set of candidate critical selectors, inject javascript for
47
 
// detecting critical above the fold css selectors after the page has loaded.
48
 
// Assumes CSS @imports have been flattened first.
49
 
class CriticalCssBeaconFilter : public CssSummarizerBase {
50
 
 public:
51
 
  static const char kInitializePageSpeedJs[];
52
 
 
53
 
  // Statistics:
54
 
  static const char kCriticalCssBeaconAddedCount[];
55
 
  static const char kCriticalCssNoBeaconDueToMissingData[];
56
 
  static const char kCriticalCssSkippedDueToCharset[];
57
 
 
58
 
  explicit CriticalCssBeaconFilter(RewriteDriver* driver);
59
 
  virtual ~CriticalCssBeaconFilter();
60
 
 
61
 
  static void InitStats(Statistics* statistics);
62
 
 
63
 
  virtual const char* Name() const { return "CriticalCssBeacon"; }
64
 
  virtual const char* id() const { return "cb"; }
65
 
 
66
 
  // This filter needs access to all critical selectors (even those from
67
 
  // unauthorized domains) in order to let the clients use them while
68
 
  // detecting critical selectors that can be subsequently beaconed back
69
 
  // to the server and eventually inlined into the HTML.
70
 
  virtual RewriteDriver::InlineAuthorizationPolicy AllowUnauthorizedDomain()
71
 
      const {
72
 
    return driver()->options()->HasInlineUnauthorizedResourceType(
73
 
               semantic_type::kStylesheet) ?
74
 
           RewriteDriver::kInlineUnauthorizedResources :
75
 
           RewriteDriver::kInlineOnlyAuthorizedResources;
76
 
  }
77
 
 
78
 
  // Selectors are inlined into javascript.
79
 
  virtual bool IntendedForInlining() const { return true; }
80
 
 
81
 
 protected:
82
 
  virtual bool MustSummarize(HtmlElement* element) const;
83
 
  virtual void Summarize(Css::Stylesheet* stylesheet,
84
 
                         GoogleString* out) const;
85
 
  virtual void SummariesDone();
86
 
 
87
 
  virtual void DetermineEnabled(GoogleString* disabled_reason);
88
 
 
89
 
 private:
90
 
  static void FindSelectorsFromRuleset(const Css::Ruleset& ruleset,
91
 
                                       StringSet* selectors);
92
 
  // The following adds the selectors to the given StringSet.
93
 
  static void FindSelectorsFromStylesheet(const Css::Stylesheet& css,
94
 
                                          StringSet* selectors);
95
 
 
96
 
  // Append the selectors initialization JavaScript.
97
 
  void AppendSelectorsInitJs(GoogleString* script, const StringSet& selectors);
98
 
 
99
 
  // Append the beaconing initialization JavaScript.
100
 
  void AppendBeaconInitJs(const BeaconMetadata& metadata, GoogleString* script);
101
 
 
102
 
  // The total number of times the beacon is added to a page.
103
 
  Variable* critical_css_beacon_added_count_;
104
 
  // The number of times we abandon beacon insertion due to missing CSS data (it
105
 
  // was still being fetched / rewritten).
106
 
  Variable* critical_css_no_beacon_due_to_missing_data_;
107
 
  // The number of CSS files we ignore due to charset incompatibility.
108
 
  // Should these block critical CSS insertion?
109
 
  Variable* critical_css_skipped_due_to_charset_;
110
 
 
111
 
  DISALLOW_COPY_AND_ASSIGN(CriticalCssBeaconFilter);
112
 
};
113
 
 
114
 
}  // namespace net_instaweb
115
 
 
116
 
#endif  // NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_