Home | History | Annotate | Download | only in appcache
      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/strings/string_util.h"
     11 #include "net/url_request/url_request.h"
     12 #include "url/gurl.h"
     13 
     14 namespace {
     15 
     16 base::LazyInstance<std::set<std::string> >::Leaky g_supported_schemes =
     17     LAZY_INSTANCE_INITIALIZER;
     18 
     19 }  // namespace
     20 
     21 namespace appcache {
     22 
     23 const char kHttpScheme[] = "http";
     24 const char kHttpsScheme[] = "https";
     25 const char kHttpGETMethod[] = "GET";
     26 const char kHttpHEADMethod[] = "HEAD";
     27 
     28 const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
     29 
     30 const base::FilePath::CharType kAppCacheDatabaseName[] =
     31     FILE_PATH_LITERAL("Index");
     32 
     33 AppCacheInfo::AppCacheInfo()
     34     : cache_id(kNoCacheId),
     35       group_id(0),
     36       status(UNCACHED),
     37       size(0),
     38       is_complete(false) {
     39 }
     40 
     41 AppCacheInfo::~AppCacheInfo() {
     42 }
     43 
     44 AppCacheResourceInfo::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 
     56 AppCacheResourceInfo::~AppCacheResourceInfo() {
     57 }
     58 
     59 Namespace::Namespace()
     60     : type(FALLBACK_NAMESPACE),
     61       is_pattern(false),
     62       is_executable(false) {
     63 }
     64 
     65 Namespace::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 
     74 Namespace::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 
     84 Namespace::~Namespace() {
     85 }
     86 
     87 bool 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 
     99 void AddSupportedScheme(const char* scheme) {
    100   g_supported_schemes.Get().insert(scheme);
    101 }
    102 
    103 bool 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 
    121 bool IsMethodSupported(const std::string& method) {
    122   return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
    123 }
    124 
    125 bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
    126   return IsSchemeSupported(request->url()) &&
    127          IsMethodSupported(request->method());
    128 }
    129 
    130 }  // namespace appcache
    131