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: bharathbhushan@google.com (Bharath Bhushan)
18
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_INSERT_DNS_PREFETCH_FILTER_H_
19
#define NET_INSTAWEB_REWRITER_PUBLIC_INSERT_DNS_PREFETCH_FILTER_H_
21
#include "net/instaweb/htmlparse/public/html_element.h"
22
#include "net/instaweb/rewriter/public/common_filter.h"
23
#include "net/instaweb/util/public/basictypes.h"
24
#include "net/instaweb/util/public/string_util.h" // for StringSet, etc
26
namespace net_instaweb {
31
// Injects <link rel="dns-prefetch" href="//www.example.com"> tags in the HEAD
32
// to enable the browser to do DNS prefetching.
33
class InsertDnsPrefetchFilter : public CommonFilter {
35
explicit InsertDnsPrefetchFilter(RewriteDriver* driver);
36
virtual ~InsertDnsPrefetchFilter();
38
virtual void StartDocumentImpl();
39
virtual void EndDocument();
40
virtual void StartElementImpl(HtmlElement* element);
41
virtual void EndElementImpl(HtmlElement* element);
43
virtual const char* Name() const { return "InsertDnsPrefetchFilter"; }
44
// Override DetermineEnabled to enable writing of the property cache DOM
45
// cohort in the RewriteDriver.
46
virtual void DetermineEnabled(GoogleString* disabled_reason);
47
virtual const char* id() const { return "idp"; }
52
// Add a domain found in the page to the list of domains for which DNS
53
// prefetch tags can be inserted.
54
void MarkAlreadyInHead(HtmlElement::Attribute* urlattr);
56
// Returns true if the list of domains for DNS prefetch tags is "stable".
57
// Refer to the implementation for details about stability. This filter will
58
// insert the tags into the HEAD once the list is stable.
59
bool IsDomainListStable(const FlushEarlyInfo& flush_early_info) const;
60
void DebugPrint(const char* msg);
62
// This flag is useful if multiple HEADs are present. This filter inserts the
63
// DNS prefetch tags only in the first HEAD.
64
bool dns_prefetch_inserted_;
66
// This flag indicates if we are currently processing elements in HEAD.
69
// The set of domains that we should not insert DNS prefetch tags for. This
70
// includes the current domain we are rewriting, and resource links in HEAD.
71
StringSet domains_to_ignore_;
73
// The set of domains seen in resource links in BODY and not already seen in
75
StringSet domains_in_body_;
77
// The list of domains for which DNS prefetch tags can be inserted, in the
78
// order they were seen in BODY.
79
StringVector dns_prefetch_domains_;
81
// Whether this user agent supports dns prefetch filter.
82
bool user_agent_supports_dns_prefetch_;
84
DISALLOW_COPY_AND_ASSIGN(InsertDnsPrefetchFilter);
87
} // namespace net_instaweb
89
#endif // NET_INSTAWEB_REWRITER_PUBLIC_INSERT_DNS_PREFETCH_FILTER_H_