2
* Copyright 2012 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.
16
// Author: nikhilmadan@google.com (Nikhil Madan)
18
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_FLUSH_EARLY_CONTENT_WRITER_FILTER_H_
19
#define NET_INSTAWEB_REWRITER_PUBLIC_FLUSH_EARLY_CONTENT_WRITER_FILTER_H_
23
#include "net/instaweb/htmlparse/public/html_writer_filter.h"
24
#include "net/instaweb/http/public/logging_proto.h"
25
#include "net/instaweb/http/public/logging_proto_impl.h"
26
#include "net/instaweb/http/public/semantic_type.h"
27
#include "net/instaweb/http/public/user_agent_matcher.h"
28
#include "net/instaweb/util/public/basictypes.h"
29
#include "net/instaweb/util/public/null_writer.h"
30
#include "net/instaweb/util/public/scoped_ptr.h"
31
#include "net/instaweb/util/public/string.h"
32
#include "net/instaweb/util/public/string_util.h"
34
namespace net_instaweb {
37
class HtmlCharactersNode;
45
// FlushEarlyContentWriterFilter finds rewritten resources in the DOM and
46
// inserts HTML that makes the browser download them. Note that we set a
47
// NullWriter as the writer for this driver, and directly write whatever we
48
// need to the original writer.
49
class FlushEarlyContentWriterFilter : public HtmlWriterFilter {
51
static const char kDisableLinkTag[];
52
static const char kPrefetchImageTagHtml[];
53
static const char kPrefetchStartTimeScript[];
54
static const char kNumResourcesFlushedEarly[];
55
static const char kPrefetchScriptTagHtml[];
56
static const char kPrefetchLinkTagHtml[];
57
static const char kFlushEarlyStyleTemplate[];
59
explicit FlushEarlyContentWriterFilter(RewriteDriver* driver);
61
virtual void StartDocument();
62
virtual void EndDocument();
64
virtual void StartElement(HtmlElement* element);
65
virtual void EndElement(HtmlElement* element);
67
virtual void Characters(HtmlCharactersNode* characters_node);
73
// Writes the string to original_writer_.
74
void WriteToOriginalWriter(const GoogleString& in);
76
// Check whether resource can be flushed or not.
77
bool IsFlushable(const GoogleUrl& gurl,
78
const FlushEarlyResourceInfo::ResourceType& resource_type);
80
// Flush the resource using kPrefetchImageTagHtml.
81
void FlushResourceAsImage(StringPiece url);
83
// Flush the resource and update time_consumed_ms_ based on time_to_download.
86
int64 time_to_download,
87
bool is_pagespeed_resource,
88
semantic_type::Category category);
90
void FlushDeferJavascriptEarly();
91
void UpdateStats(int64 time_to_download, bool is_pagespeed_resource);
92
GoogleString ComputeFlushEarlyCriticalCss(const GoogleString& style_id);
94
// Returns the type of resource based on the url.
95
FlushEarlyResourceInfo::ResourceType GetResourceType(
96
const GoogleUrl& gurl, bool is_pagespeed_resource);
98
RewriteDriver* driver_;
99
TimedVariable* num_resources_flushed_early_;
100
// Whether we need to insert a close script tag at EndDocument.
102
bool insert_close_script_;
103
int num_resources_flushed_;
104
NullWriter null_writer_;
105
Writer* original_writer_;
106
HtmlElement* current_element_;
107
UserAgentMatcher::PrefetchMechanism prefetch_mechanism_;
108
scoped_ptr<StringSet> private_cacheable_resources_;
109
scoped_ptr<StringSet> public_cacheable_resources_;
110
int64 time_consumed_ms_;
111
int64 max_available_time_ms_;
112
typedef std::list<ResourceInfo*> ResourceInfoList;
113
ResourceInfoList js_resources_info_;
114
bool defer_javascript_enabled_;
115
bool split_html_enabled_;
116
bool is_flushing_critical_style_element_;
117
GoogleString css_output_content_;
118
GoogleString flush_early_content_;
119
bool flush_more_resources_early_if_time_permits_;
120
bool stylesheets_flushed_;
122
DISALLOW_COPY_AND_ASSIGN(FlushEarlyContentWriterFilter);
125
} // namespace net_instaweb
127
#endif // NET_INSTAWEB_REWRITER_PUBLIC_FLUSH_EARLY_CONTENT_WRITER_FILTER_H_