summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profiles/profile_io_data.h
blob: 93c431ce297a504b31ac68a7c6a5f3b5e997472d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
#define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
#pragma once

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"

class ChromeURLRequestContext;
class ChromeURLRequestContextGetter;
class IOThread;
class Profile;

// ProfileImpl owns a ProfileIOData::Handle, which holds a reference to the
// ProfileIOData. ProfileIOData is intended to own all the objects owned by
// ProfileImpl which live on the IO thread, such as, but not limited to, network
// objects like CookieMonster, HttpTransactionFactory, etc. ProfileIOData is
// owned by the ProfileImpl and ProfileIOData's ChromeURLRequestContexts. When
// all of them go away, then ProfileIOData will be deleted. Note that the
// ProfileIOData will typically outlive the Profile it is "owned" by, so it's
// important for ProfileIOData not to hold any references to the Profile beyond
// what's used by LazyParams (which should be deleted after lazy
// initialization).
class ProfileIOData : public base::RefCountedThreadSafe<ProfileIOData> {
 public:
  class Handle {
   public:
    Handle();
    ~Handle();

    // Init() must be called before ~Handle(). It records all the necessary
    // parameters needed to construct a ChromeURLRequestContextGetter.
    void Init(const FilePath& cookie_path,
              const FilePath& cache_path,
              int cache_max_size,
              const FilePath& media_cache_path,
              int media_cache_max_size,
              const FilePath& extensions_cookie_path,
              Profile* profile);

    bool HasMainRequestContext() const {
      return main_request_context_getter_ != NULL;
    }
    scoped_refptr<ChromeURLRequestContextGetter>
        GetMainRequestContextGetter() const;
    scoped_refptr<ChromeURLRequestContextGetter>
        GetMediaRequestContextGetter() const;
    scoped_refptr<ChromeURLRequestContextGetter>
        GetExtensionsRequestContextGetter() const;

   private:
    // Ordering is important here. Do not reorder unless you know what you're
    // doing.
    const scoped_refptr<ProfileIOData> io_data_;
    mutable scoped_refptr<ChromeURLRequestContextGetter>
        main_request_context_getter_;
    mutable scoped_refptr<ChromeURLRequestContextGetter>
        media_request_context_getter_;
    mutable scoped_refptr<ChromeURLRequestContextGetter>
        extensions_request_context_getter_;

    DISALLOW_COPY_AND_ASSIGN(Handle);
  };

  // TODO(willchan): Move this to the private section when
  // ChromeURLRequestContextFactory subclasses don't need it anymore.
  struct LazyParams {
    // All of these parameters are intended to be read on the IO thread.
    FilePath cookie_path;
    FilePath cache_path;
    int cache_max_size;
    FilePath media_cache_path;
    int media_cache_max_size;
    FilePath extensions_cookie_path;
    IOThread* io_thread;

    // TODO(willchan): Kill this, since the IO thread shouldn't be reading from
    // the Profile. Instead, replace this with the parameters we want to copy
    // from the UI thread to the IO thread.
    Profile* profile;
  };

  // These should only be called at most once each. Ownership is reversed they
  // get called, from ProfileIOData owning ChromeURLRequestContext to vice
  // versa.
  scoped_refptr<ChromeURLRequestContext> GetMainRequestContext() const;
  scoped_refptr<ChromeURLRequestContext> GetMediaRequestContext() const;
  scoped_refptr<ChromeURLRequestContext> GetExtensionsRequestContext() const;

  // TODO(willchan): Delete this when ChromeURLRequestContextFactory subclasses
  // don't need it anymore.
  const LazyParams& lazy_params() const;

 private:
  friend class base::RefCountedThreadSafe<ProfileIOData>;

  class RequestContext;

  ProfileIOData();
  ~ProfileIOData();

  // Lazily initializes ProfileIOData.
  void LazyInitialize() const;

  // Lazy initialization params.
  // TODO(willchan): Delete after Initialize() finishes initializing all the
  // contexts.
  scoped_ptr<const LazyParams> lazy_params_;

  mutable bool initialized_;
  mutable scoped_refptr<RequestContext> main_request_context_;
  mutable scoped_refptr<RequestContext> media_request_context_;
  mutable scoped_refptr<RequestContext> extensions_request_context_;

  DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
};

#endif  // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_