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#ifndef CONTENT_BROWSER_FILEAPI_CHROME_BLOB_STORAGE_CONTEXT_H_
6#define CONTENT_BROWSER_FILEAPI_CHROME_BLOB_STORAGE_CONTEXT_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/sequenced_task_runner_helpers.h"
11#include "content/common/content_export.h"
12
13namespace storage {
14class BlobStorageContext;
15}
16
17namespace content {
18class BlobHandle;
19class BrowserContext;
20struct ChromeBlobStorageContextDeleter;
21
22// A context class that keeps track of BlobStorageController used by the chrome.
23// There is an instance associated with each BrowserContext. There could be
24// multiple URLRequestContexts in the same browser context that refers to the
25// same instance.
26//
27// All methods, except the ctor, are expected to be called on
28// the IO thread (unless specifically called out in doc comments).
29class CONTENT_EXPORT ChromeBlobStorageContext
30    : public base::RefCountedThreadSafe<ChromeBlobStorageContext,
31                                        ChromeBlobStorageContextDeleter> {
32 public:
33  ChromeBlobStorageContext();
34
35  static ChromeBlobStorageContext* GetFor(
36      BrowserContext* browser_context);
37
38  void InitializeOnIOThread();
39
40  storage::BlobStorageContext* context() const { return context_.get(); }
41
42  // Returns a NULL scoped_ptr on failure.
43  scoped_ptr<BlobHandle> CreateMemoryBackedBlob(const char* data,
44                                                size_t length);
45
46 protected:
47  virtual ~ChromeBlobStorageContext();
48
49 private:
50  friend class base::DeleteHelper<ChromeBlobStorageContext>;
51  friend class base::RefCountedThreadSafe<ChromeBlobStorageContext,
52                                          ChromeBlobStorageContextDeleter>;
53  friend struct ChromeBlobStorageContextDeleter;
54
55  void DeleteOnCorrectThread() const;
56
57  scoped_ptr<storage::BlobStorageContext> context_;
58};
59
60struct ChromeBlobStorageContextDeleter {
61  static void Destruct(const ChromeBlobStorageContext* context) {
62    context->DeleteOnCorrectThread();
63  }
64};
65
66}  // namespace content
67
68#endif  // CONTENT_BROWSER_FILEAPI_CHROME_BLOB_STORAGE_CONTEXT_H_
69