Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_request_handler.cc
index 4fbd76e..7b615b3 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "content/browser/service_worker/service_worker_request_handler.h"
 
+#include <string>
+
 #include "content/browser/service_worker/service_worker_context_core.h"
 #include "content/browser/service_worker/service_worker_context_wrapper.h"
 #include "content/browser/service_worker/service_worker_provider_host.h"
@@ -12,6 +14,8 @@
 #include "content/browser/service_worker/service_worker_utils.h"
 #include "content/common/service_worker/service_worker_types.h"
 #include "net/url_request/url_request.h"
+#include "net/url_request/url_request_interceptor.h"
+#include "webkit/browser/blob/blob_storage_context.h"
 
 namespace content {
 
@@ -20,11 +24,11 @@ namespace {
 int kUserDataKey;  // Key value is not important.
 
 class ServiceWorkerRequestInterceptor
-    : public net::URLRequestJobFactory::ProtocolHandler {
+    : public net::URLRequestInterceptor {
  public:
   ServiceWorkerRequestInterceptor() {}
   virtual ~ServiceWorkerRequestInterceptor() {}
-  virtual net::URLRequestJob* MaybeCreateJob(
+  virtual net::URLRequestJob* MaybeInterceptRequest(
       net::URLRequest* request,
       net::NetworkDelegate* network_delegate) const OVERRIDE {
     ServiceWorkerRequestHandler* handler =
@@ -38,16 +42,27 @@ class ServiceWorkerRequestInterceptor
   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
 };
 
+bool IsMethodSupportedForAppCache(const std::string& method) {
+  return (method == "GET") || (method == "HEAD");
+}
+
+bool IsSchemeAndMethodSupportedForAppCache(const net::URLRequest* request) {
+  return request->url().SchemeIsHTTPOrHTTPS() &&
+         IsMethodSupportedForAppCache(request->method());
+}
+
 }  // namespace
 
 void ServiceWorkerRequestHandler::InitializeHandler(
     net::URLRequest* request,
     ServiceWorkerContextWrapper* context_wrapper,
+    webkit_blob::BlobStorageContext* blob_storage_context,
     int process_id,
     int provider_id,
-    ResourceType::Type resource_type) {
-  if (!ServiceWorkerUtils::IsFeatureEnabled())
+    ResourceType resource_type) {
+  if (!IsSchemeAndMethodSupportedForAppCache(request)) {
     return;
+  }
 
   if (!context_wrapper || !context_wrapper->context() ||
       provider_id == kInvalidServiceWorkerProviderId) {
@@ -56,11 +71,12 @@ void ServiceWorkerRequestHandler::InitializeHandler(
 
   ServiceWorkerProviderHost* provider_host =
       context_wrapper->context()->GetProviderHost(process_id, provider_id);
-  if (!provider_host)
+  if (!provider_host || !provider_host->IsContextAlive())
     return;
 
   scoped_ptr<ServiceWorkerRequestHandler> handler(
-      provider_host->CreateRequestHandler(resource_type));
+      provider_host->CreateRequestHandler(resource_type,
+                                          blob_storage_context->AsWeakPtr()));
   if (!handler)
     return;
 
@@ -73,9 +89,9 @@ ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
       request->GetUserData(&kUserDataKey));
 }
 
-scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
+scoped_ptr<net::URLRequestInterceptor>
 ServiceWorkerRequestHandler::CreateInterceptor() {
-  return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
+  return scoped_ptr<net::URLRequestInterceptor>(
       new ServiceWorkerRequestInterceptor);
 }
 
@@ -85,9 +101,11 @@ ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
     base::WeakPtr<ServiceWorkerContextCore> context,
     base::WeakPtr<ServiceWorkerProviderHost> provider_host,
-    ResourceType::Type resource_type)
+    base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context,
+    ResourceType resource_type)
     : context_(context),
       provider_host_(provider_host),
+      blob_storage_context_(blob_storage_context),
       resource_type_(resource_type) {
 }