service_worker_context_request_handler.cc revision 010d83a9304c5a91596085d917d248abff47903a
15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Use of this source code is governed by a BSD-style license that can be
35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// found in the LICENSE file.
45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
55c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_context_request_handler.h"
65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/browser/service_worker/service_worker_provider_host.h"
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/browser/service_worker/service_worker_read_from_cache_job.h"
9010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "content/browser/service_worker/service_worker_version.h"
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "net/url_request/url_request.h"
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace content {
135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuServiceWorkerContextRequestHandler::ServiceWorkerContextRequestHandler(
155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    base::WeakPtr<ServiceWorkerContextCore> context,
165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    base::WeakPtr<ServiceWorkerProviderHost> provider_host,
175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ResourceType::Type resource_type)
18010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    : ServiceWorkerRequestHandler(context, provider_host, resource_type),
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      version_(provider_host_->running_hosted_version()) {
20010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(provider_host_->IsHostToRunningServiceWorker());
215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuServiceWorkerContextRequestHandler::~ServiceWorkerContextRequestHandler() {
245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunet::URLRequestJob* ServiceWorkerContextRequestHandler::MaybeCreateJob(
275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    net::URLRequest* request,
285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    net::NetworkDelegate* network_delegate) {
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!provider_host_ || !version_)
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return NULL;
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // We only use the script cache for main script loading and
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // importScripts(), even if a cached script is xhr'd, we don't
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // retrieve it from the script cache.
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(michaeln): Get the desired behavior clarified in the spec,
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // and make tweak the behavior here to match.
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (resource_type_ != ResourceType::SERVICE_WORKER &&
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      resource_type_ != ResourceType::SCRIPT) {
39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return NULL;
40010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
41010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
42010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (ShouldAddToScriptCache(request->url())) {
43010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return NULL;
44010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // TODO(michaeln): return new ServiceWorkerWriteToCacheJob();
45010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
46010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
47010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int64 response_id = kInvalidServiceWorkerResponseId;
48010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (ShouldReadFromScriptCache(request->url(), &response_id)) {
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return new ServiceWorkerReadFromCacheJob(
50010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        request, network_delegate, context_, response_id);
51010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
52010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
53010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // NULL means use the network.
545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  return NULL;
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ServiceWorkerContextRequestHandler::ShouldAddToScriptCache(
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const GURL& url) {
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(michaeln): Ensure the transition to INSTALLING can't
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // happen prior to the initial eval completion.
61010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (version_->status() != ServiceWorkerVersion::NEW)
62010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return false;
63010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return version_->LookupInScriptCache(url) == kInvalidServiceWorkerResponseId;
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ServiceWorkerContextRequestHandler::ShouldReadFromScriptCache(
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const GURL& url, int64* response_id_out) {
68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // We don't read from the script cache until the version is INSTALLED.
69010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (version_->status() == ServiceWorkerVersion::NEW ||
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      version_->status() == ServiceWorkerVersion::INSTALLING)
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return false;
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  *response_id_out = version_->LookupInScriptCache(url);
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return *response_id_out != kInvalidServiceWorkerResponseId;
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}  // namespace content
77