1// Copyright 2014 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#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
6#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
7
8#include <map>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/weak_ptr.h"
13#include "content/browser/service_worker/service_worker_database.h"
14#include "content/common/content_export.h"
15#include "net/url_request/url_request_status.h"
16
17class GURL;
18
19namespace content {
20
21class ServiceWorkerContextCore;
22class ServiceWorkerVersion;
23
24// Class that maintains the mapping between urls and a resource id
25// for a particular version's implicit script resources.
26class CONTENT_EXPORT ServiceWorkerScriptCacheMap {
27 public:
28  int64 Lookup(const GURL& url);
29
30  // Used during the initial run of a new version to build the map
31  // of resources ids.
32  void NotifyStartedCaching(const GURL& url, int64 resource_id);
33  void NotifyFinishedCaching(const GURL& url,
34                             const net::URLRequestStatus& status);
35
36  // Used to retrieve the results of the initial run of a new version.
37  void GetResources(
38      std::vector<ServiceWorkerDatabase::ResourceRecord>* resources);
39
40  // Used when loading an existing version.
41  void SetResources(
42     const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources);
43
44  size_t size() const { return resource_ids_.size(); }
45
46  const net::URLRequestStatus& main_script_status() const {
47    return main_script_status_;
48  }
49
50 private:
51  typedef std::map<GURL, int64> ResourceIDMap;
52
53  // The version objects owns its script cache and provides a rawptr to it.
54  friend class ServiceWorkerVersion;
55  ServiceWorkerScriptCacheMap(
56      ServiceWorkerVersion* owner,
57      base::WeakPtr<ServiceWorkerContextCore> context);
58  ~ServiceWorkerScriptCacheMap();
59
60  ServiceWorkerVersion* owner_;
61  base::WeakPtr<ServiceWorkerContextCore> context_;
62  ResourceIDMap resource_ids_;
63  net::URLRequestStatus main_script_status_;
64
65  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerScriptCacheMap);
66};
67
68}  // namespace content
69
70#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
71