1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "webkit/common/appcache/appcache_interfaces.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <set>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
95e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/url_request/url_request.h"
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace appcache {
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kHttpScheme[] = "http";
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kHttpsScheme[] = "https";
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)const char kDevToolsScheme[] = "chrome-devtools";
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kHttpGETMethod[] = "GET";
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kHttpHEADMethod[] = "HEAD";
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const base::FilePath::CharType kAppCacheDatabaseName[] =
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FILE_PATH_LITERAL("Index");
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)AppCacheInfo::AppCacheInfo()
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : cache_id(kNoCacheId),
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      group_id(0),
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      status(UNCACHED),
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      size(0),
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_complete(false) {
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)AppCacheInfo::~AppCacheInfo() {
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)AppCacheResourceInfo::AppCacheResourceInfo()
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : url(),
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      size(0),
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_master(false),
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_manifest(false),
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_intercept(false),
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_fallback(false),
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_foreign(false),
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_explicit(false),
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      response_id(kNoResponseId) {
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)AppCacheResourceInfo::~AppCacheResourceInfo() {
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Namespace::Namespace()
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : type(FALLBACK_NAMESPACE),
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_pattern(false),
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_executable(false) {
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Namespace::Namespace(
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NamespaceType type, const GURL& url, const GURL& target, bool is_pattern)
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : type(type),
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      namespace_url(url),
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      target_url(target),
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_pattern(is_pattern),
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_executable(false) {
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Namespace::Namespace(
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    NamespaceType type, const GURL& url, const GURL& target,
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    bool is_pattern, bool is_executable)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : type(type),
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      namespace_url(url),
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      target_url(target),
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_pattern(is_pattern),
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_executable(is_executable) {
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Namespace::~Namespace() {
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool Namespace::IsMatch(const GURL& url) const {
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (is_pattern) {
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // We have to escape '?' characters since MatchPattern also treats those
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // as wildcards which we don't want here, we only do '*'s.
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    std::string pattern = namespace_url.spec();
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (namespace_url.has_query())
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ReplaceSubstringsAfterOffset(&pattern, 0, "?", "\\?");
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return MatchPattern(url.spec(), pattern);
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return StartsWithASCII(url.spec(), namespace_url.spec(), true);
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsSchemeSupported(const GURL& url) {
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      url.SchemeIs(kDevToolsScheme);
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef NDEBUG
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // TODO(michaeln): It would be really nice if this could optionally work for
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // file and filesystem urls too to help web developers experiment and test
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // their apps, perhaps enabled via a cmd line flag or some other developer
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // tool setting.  Unfortunately file scheme net::URLRequests don't produce the
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // same signalling (200 response codes, headers) as http URLRequests, so this
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // doesn't work just yet.
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // supported |= url.SchemeIsFile();
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return supported;
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsMethodSupported(const std::string& method) {
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return IsSchemeSupported(request->url()) &&
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)         IsMethodSupported(request->method());
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace appcache
118