profile_impl_io_data.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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 CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
6#define CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/hash_tables.h"
11#include "base/memory/ref_counted.h"
12#include "chrome/browser/profiles/profile_io_data.h"
13
14namespace net {
15class CookiePolicy;
16class NetworkDelegate;
17class DnsCertProvenanceChecker;
18class HttpTransactionFactory;
19}  // namespace net
20
21class ProfileImplIOData : public ProfileIOData {
22 public:
23  class Handle {
24   public:
25    explicit Handle(Profile* profile);
26    ~Handle();
27
28    bool HasMainRequestContext() const {
29      return main_request_context_getter_ != NULL;
30    }
31
32    // Init() must be called before ~Handle(). It records all the necessary
33    // parameters needed to construct a ChromeURLRequestContextGetter.
34    void Init(const FilePath& cookie_path,
35              const FilePath& cache_path,
36              int cache_max_size,
37              const FilePath& media_cache_path,
38              int media_cache_max_size,
39              const FilePath& extensions_cookie_path,
40              const FilePath& app_path);
41
42    const content::ResourceContext& GetResourceContext() const;
43    scoped_refptr<ChromeURLRequestContextGetter>
44        GetMainRequestContextGetter() const;
45    scoped_refptr<ChromeURLRequestContextGetter>
46        GetMediaRequestContextGetter() const;
47    scoped_refptr<ChromeURLRequestContextGetter>
48        GetExtensionsRequestContextGetter() const;
49    scoped_refptr<ChromeURLRequestContextGetter>
50        GetIsolatedAppRequestContextGetter(
51            const std::string& app_id) const;
52
53   private:
54    typedef base::hash_map<std::string,
55                           scoped_refptr<ChromeURLRequestContextGetter> >
56        ChromeURLRequestContextGetterMap;
57
58    // Lazily initialize ProfileParams. We do this on the calls to
59    // Get*RequestContextGetter(), so we only initialize ProfileParams right
60    // before posting a task to the IO thread to start using them. This prevents
61    // objects that are supposed to be deleted on the IO thread, but are created
62    // on the UI thread from being unnecessarily initialized.
63    void LazyInitialize() const;
64
65    // Ordering is important here. Do not reorder unless you know what you're
66    // doing. We need to release |io_data_| *before* the getters, because we
67    // want to make sure that the last reference for |io_data_| is on the IO
68    // thread. The getters will be deleted on the IO thread, so they will
69    // release their refs to their contexts, which will release the last refs to
70    // the ProfileIOData on the IO thread.
71    mutable scoped_refptr<ChromeURLRequestContextGetter>
72        main_request_context_getter_;
73    mutable scoped_refptr<ChromeURLRequestContextGetter>
74        media_request_context_getter_;
75    mutable scoped_refptr<ChromeURLRequestContextGetter>
76        extensions_request_context_getter_;
77    mutable ChromeURLRequestContextGetterMap app_request_context_getter_map_;
78    const scoped_refptr<ProfileImplIOData> io_data_;
79
80    Profile* const profile_;
81
82    mutable bool initialized_;
83
84    DISALLOW_COPY_AND_ASSIGN(Handle);
85  };
86
87 private:
88  friend class base::RefCountedThreadSafe<ProfileImplIOData>;
89
90  struct LazyParams {
91    LazyParams();
92    ~LazyParams();
93
94    // All of these parameters are intended to be read on the IO thread.
95    FilePath cookie_path;
96    FilePath cache_path;
97    int cache_max_size;
98    FilePath media_cache_path;
99    int media_cache_max_size;
100    FilePath extensions_cookie_path;
101  };
102
103  typedef base::hash_map<std::string, net::HttpTransactionFactory* >
104      HttpTransactionFactoryMap;
105
106  ProfileImplIOData();
107  virtual ~ProfileImplIOData();
108
109  virtual void LazyInitializeInternal(ProfileParams* profile_params) const;
110  virtual scoped_refptr<RequestContext> InitializeAppRequestContext(
111      scoped_refptr<ChromeURLRequestContext> main_context,
112      const std::string& app_id) const;
113  virtual scoped_refptr<ChromeURLRequestContext>
114      AcquireMediaRequestContext() const;
115  virtual scoped_refptr<ChromeURLRequestContext>
116      AcquireIsolatedAppRequestContext(
117          scoped_refptr<ChromeURLRequestContext> main_context,
118          const std::string& app_id) const;
119
120  // Lazy initialization params.
121  mutable scoped_ptr<LazyParams> lazy_params_;
122
123  mutable scoped_refptr<RequestContext> media_request_context_;
124
125  mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
126  mutable scoped_ptr<net::HttpTransactionFactory> media_http_factory_;
127
128  // One HttpTransactionFactory per isolated app.
129  mutable HttpTransactionFactoryMap app_http_factory_map_;
130
131  // Parameters needed for isolated apps.
132  FilePath app_path_;
133  mutable bool clear_local_state_on_exit_;
134
135  DISALLOW_COPY_AND_ASSIGN(ProfileImplIOData);
136};
137
138#endif  // CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
139