Home | History | Annotate | Download | only in common
      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 "content/common/appcache_interfaces.h"
      6 
      7 #include <set>
      8 
      9 #include "base/strings/string_util.h"
     10 #include "net/url_request/url_request.h"
     11 #include "url/gurl.h"
     12 
     13 namespace content {
     14 
     15 const char kHttpScheme[] = "http";
     16 const char kHttpsScheme[] = "https";
     17 const char kDevToolsScheme[] = "chrome-devtools";
     18 const char kHttpGETMethod[] = "GET";
     19 const char kHttpHEADMethod[] = "HEAD";
     20 
     21 const char kEnableExecutableHandlers[] = "enable-appcache-executable-handlers";
     22 
     23 const base::FilePath::CharType kAppCacheDatabaseName[] =
     24     FILE_PATH_LITERAL("Index");
     25 
     26 AppCacheInfo::AppCacheInfo()
     27     : cache_id(kAppCacheNoCacheId),
     28       group_id(0),
     29       status(APPCACHE_STATUS_UNCACHED),
     30       size(0),
     31       is_complete(false) {
     32 }
     33 
     34 AppCacheInfo::~AppCacheInfo() {
     35 }
     36 
     37 AppCacheResourceInfo::AppCacheResourceInfo()
     38     : url(),
     39       size(0),
     40       is_master(false),
     41       is_manifest(false),
     42       is_intercept(false),
     43       is_fallback(false),
     44       is_foreign(false),
     45       is_explicit(false),
     46       response_id(kAppCacheNoResponseId) {
     47 }
     48 
     49 AppCacheResourceInfo::~AppCacheResourceInfo() {
     50 }
     51 
     52 AppCacheErrorDetails::AppCacheErrorDetails()
     53     : message(),
     54       reason(APPCACHE_UNKNOWN_ERROR),
     55       url(),
     56       status(0),
     57       is_cross_origin(false) {}
     58 
     59 AppCacheErrorDetails::AppCacheErrorDetails(
     60     std::string in_message,
     61     AppCacheErrorReason in_reason,
     62     GURL in_url,
     63     int in_status,
     64     bool in_is_cross_origin)
     65     : message(in_message),
     66       reason(in_reason),
     67       url(in_url),
     68       status(in_status),
     69       is_cross_origin(in_is_cross_origin) {}
     70 
     71 AppCacheErrorDetails::~AppCacheErrorDetails() {}
     72 
     73 AppCacheNamespace::AppCacheNamespace()
     74     : type(APPCACHE_FALLBACK_NAMESPACE),
     75       is_pattern(false),
     76       is_executable(false) {
     77 }
     78 
     79 AppCacheNamespace::AppCacheNamespace(
     80     AppCacheNamespaceType type, const GURL& url, const GURL& target,
     81     bool is_pattern)
     82     : type(type),
     83       namespace_url(url),
     84       target_url(target),
     85       is_pattern(is_pattern),
     86       is_executable(false) {
     87 }
     88 
     89 AppCacheNamespace::AppCacheNamespace(
     90     AppCacheNamespaceType type, const GURL& url, const GURL& target,
     91     bool is_pattern, bool is_executable)
     92     : type(type),
     93       namespace_url(url),
     94       target_url(target),
     95       is_pattern(is_pattern),
     96       is_executable(is_executable) {
     97 }
     98 
     99 AppCacheNamespace::~AppCacheNamespace() {
    100 }
    101 
    102 bool AppCacheNamespace::IsMatch(const GURL& url) const {
    103   if (is_pattern) {
    104     // We have to escape '?' characters since MatchPattern also treats those
    105     // as wildcards which we don't want here, we only do '*'s.
    106     std::string pattern = namespace_url.spec();
    107     if (namespace_url.has_query())
    108       ReplaceSubstringsAfterOffset(&pattern, 0, "?", "\\?");
    109     return MatchPattern(url.spec(), pattern);
    110   }
    111   return StartsWithASCII(url.spec(), namespace_url.spec(), true);
    112 }
    113 
    114 bool IsSchemeSupportedForAppCache(const GURL& url) {
    115   bool supported = url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme) ||
    116       url.SchemeIs(kDevToolsScheme);
    117 
    118 #ifndef NDEBUG
    119   // TODO(michaeln): It would be really nice if this could optionally work for
    120   // file and filesystem urls too to help web developers experiment and test
    121   // their apps, perhaps enabled via a cmd line flag or some other developer
    122   // tool setting.  Unfortunately file scheme net::URLRequests don't produce the
    123   // same signalling (200 response codes, headers) as http URLRequests, so this
    124   // doesn't work just yet.
    125   // supported |= url.SchemeIsFile();
    126 #endif
    127   return supported;
    128 }
    129 
    130 bool IsMethodSupportedForAppCache(const std::string& method) {
    131   return (method == kHttpGETMethod) || (method == kHttpHEADMethod);
    132 }
    133 
    134 bool IsSchemeAndMethodSupportedForAppCache(const net::URLRequest* request) {
    135   return IsSchemeSupportedForAppCache(request->url()) &&
    136          IsMethodSupportedForAppCache(request->method());
    137 }
    138 
    139 }  // namespace content
    140