appcache_interfaces.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "webkit/common/appcache/appcache_interfaces.h"
6
7#include <set>
8
9#include "base/lazy_instance.h"
10#include "base/string_util.h"
11#include "googleurl/src/gurl.h"
12#include "net/url_request/url_request.h"
13
14namespace {
15
16base::LazyInstance<std::set<std::string> >::Leaky g_supported_schemes =
17    LAZY_INSTANCE_INITIALIZER;
18
19}  // namespace
20
21namespace appcache {
22
23const char kHttpScheme[] = "http";
24const char kHttpsScheme[] = "https";
25const char kHttpGETMethod[] = "GET";
26const char kHttpHEADMethod[] = "HEAD";
27
28const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
29
30const base::FilePath::CharType kAppCacheDatabaseName[] =
31    FILE_PATH_LITERAL("Index");
32
33AppCacheInfo::AppCacheInfo()
34    : cache_id(kNoCacheId),
35      group_id(0),
36      status(UNCACHED),
37      size(0),
38      is_complete(false) {
39}
40
41AppCacheInfo::~AppCacheInfo() {
42}
43
44AppCacheResourceInfo::AppCacheResourceInfo()
45    : url(),
46      size(0),
47      is_master(false),
48      is_manifest(false),
49      is_intercept(false),
50      is_fallback(false),
51      is_foreign(false),
52      is_explicit(false),
53      response_id(kNoResponseId) {
54}
55
56AppCacheResourceInfo::~AppCacheResourceInfo() {
57}
58
59Namespace::Namespace()
60    : type(FALLBACK_NAMESPACE),
61      is_pattern(false),
62      is_executable(false) {
63}
64
65Namespace::Namespace(
66    NamespaceType type, const GURL& url, const GURL& target, bool is_pattern)
67    : type(type),
68      namespace_url(url),
69      target_url(target),
70      is_pattern(is_pattern),
71      is_executable(false) {
72}
73
74Namespace::Namespace(
75    NamespaceType type, const GURL& url, const GURL& target,
76    bool is_pattern, bool is_executable)
77    : type(type),
78      namespace_url(url),
79      target_url(target),
80      is_pattern(is_pattern),
81      is_executable(is_executable) {
82}
83
84Namespace::~Namespace() {
85}
86
87bool Namespace::IsMatch(const GURL& url) const {
88  if (is_pattern) {
89    // We have to escape '?' characters since MatchPattern also treats those
90    // as wildcards which we don't want here, we only do '*'s.
91    std::string pattern = namespace_url.spec();
92    if (namespace_url.has_query())
93      ReplaceSubstringsAfterOffset(&pattern, 0, "?", "\\?");
94    return MatchPattern(url.spec(), pattern);
95  }
96  return StartsWithASCII(url.spec(), namespace_url.spec(), true);
97}
98
99void AddSupportedScheme(const char* scheme) {
100  g_supported_schemes.Get().insert(scheme);
101}
102
103bool IsSchemeSupported(const GURL& url) {
104  bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
105      (!(g_supported_schemes == NULL) &&
106       g_supported_schemes.Get().find(url.scheme()) !=
107           g_supported_schemes.Get().end());
108
109#ifndef NDEBUG
110  // TODO(michaeln): It would be really nice if this could optionally work for
111  // file and filesystem urls too to help web developers experiment and test
112  // their apps, perhaps enabled via a cmd line flag or some other developer
113  // tool setting.  Unfortunately file scheme net::URLRequests don't produce the
114  // same signalling (200 response codes, headers) as http URLRequests, so this
115  // doesn't work just yet.
116  // supported |= url.SchemeIsFile();
117#endif
118  return supported;
119}
120
121bool IsMethodSupported(const std::string& method) {
122  return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
123}
124
125bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
126  return IsSchemeSupported(request->url()) &&
127         IsMethodSupported(request->method());
128}
129
130}  // namespace appcache
131