2
* Copyright 2013 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmaessen@google.com (Jan-Willem Maessen)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_
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"
37
namespace net_instaweb {
39
struct BeaconMetadata;
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 {
51
static const char kInitializePageSpeedJs[];
54
static const char kCriticalCssBeaconAddedCount[];
55
static const char kCriticalCssNoBeaconDueToMissingData[];
56
static const char kCriticalCssSkippedDueToCharset[];
58
explicit CriticalCssBeaconFilter(RewriteDriver* driver);
59
virtual ~CriticalCssBeaconFilter();
61
static void InitStats(Statistics* statistics);
63
virtual const char* Name() const { return "CriticalCssBeacon"; }
64
virtual const char* id() const { return "cb"; }
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()
72
return driver()->options()->HasInlineUnauthorizedResourceType(
73
semantic_type::kStylesheet) ?
74
RewriteDriver::kInlineUnauthorizedResources :
75
RewriteDriver::kInlineOnlyAuthorizedResources;
78
// Selectors are inlined into javascript.
79
virtual bool IntendedForInlining() const { return true; }
82
virtual bool MustSummarize(HtmlElement* element) const;
83
virtual void Summarize(Css::Stylesheet* stylesheet,
84
GoogleString* out) const;
85
virtual void SummariesDone();
87
virtual void DetermineEnabled(GoogleString* disabled_reason);
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);
96
// Append the selectors initialization JavaScript.
97
void AppendSelectorsInitJs(GoogleString* script, const StringSet& selectors);
99
// Append the beaconing initialization JavaScript.
100
void AppendBeaconInitJs(const BeaconMetadata& metadata, GoogleString* script);
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_;
111
DISALLOW_COPY_AND_ASSIGN(CriticalCssBeaconFilter);
114
} // namespace net_instaweb
116
#endif // NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_BEACON_FILTER_H_