summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/browser_state/chrome_browser_state_impl_io_data.h
blob: 8da2ba493f38a1284fc5d0f36d3606c9c261e6d2 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2015 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 IOS_CHROME_BROWSER_BROWSER_STATE_CHROME_BROWSER_STATE_IMPL_IO_DATA_H_
#define IOS_CHROME_BROWSER_BROWSER_STATE_CHROME_BROWSER_STATE_IMPL_IO_DATA_H_

#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "components/prefs/pref_store.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_io_data.h"
#include "ios/chrome/browser/net/net_types.h"

class JsonPrefStore;

namespace ios {
class ChromeBrowserState;
}

namespace net {
class CookieStore;
class HttpNetworkSession;
class HttpServerPropertiesManager;
class HttpTransactionFactory;
class SdchManager;
class SdchOwner;
}  // namespace net

class ChromeBrowserStateImplIOData : public ChromeBrowserStateIOData {
 public:
  class Handle {
   public:
    explicit Handle(ios::ChromeBrowserState* browser_state);
    ~Handle();

    // Init() must be called before ~Handle(). It records most of the
    // parameters needed to construct a ChromeURLRequestContextGetter.
    void Init(const base::FilePath& cookie_path,
              const base::FilePath& channel_id_path,
              const base::FilePath& cache_path,
              int cache_max_size,
              const base::FilePath& profile_path);

    // These Create*ContextGetter() functions are only exposed because the
    // circular relationship between ChromeBrowserState,
    // ChromeBrowserStateIOData::Handle, and the
    // IOSChromeURLRequestContextGetter factories requires ChromeBrowserState be
    // able to call these functions.
    scoped_refptr<IOSChromeURLRequestContextGetter>
    CreateMainRequestContextGetter(ProtocolHandlerMap* protocol_handlers,
                                   PrefService* local_state,
                                   IOSChromeIOThread* io_thread) const;
    scoped_refptr<IOSChromeURLRequestContextGetter>
    CreateIsolatedAppRequestContextGetter(
        const base::FilePath& partition_path) const;

    ChromeBrowserStateIOData* io_data() const;

    // Deletes all network related data since |time|. It deletes transport
    // security state since |time| and also deletes HttpServerProperties data.
    // Works asynchronously, however if the |completion| callback is non-null,
    // it will be posted on the UI thread once the removal process completes.
    void ClearNetworkingHistorySince(base::Time time,
                                     const base::Closure& completion);

   private:
    typedef std::map<base::FilePath,
                     scoped_refptr<IOSChromeURLRequestContextGetter>>
        IOSChromeURLRequestContextGetterMap;

    // Lazily initialize ProfileParams. We do this on the calls to
    // Get*RequestContextGetter(), so we only initialize ProfileParams right
    // before posting a task to the IO thread to start using them. This prevents
    // objects that are supposed to be deleted on the IO thread, but are created
    // on the UI thread from being unnecessarily initialized.
    void LazyInitialize() const;

    // Collect references to context getters in reverse order, i.e. last item
    // will be main request getter. This list is passed to |io_data_|
    // for invalidation on IO thread.
    scoped_ptr<IOSChromeURLRequestContextGetterVector> GetAllContextGetters();

    // The getters will be invalidated on the IO thread before
    // ChromeBrowserStateIOData instance is deleted.
    mutable scoped_refptr<IOSChromeURLRequestContextGetter>
        main_request_context_getter_;
    mutable IOSChromeURLRequestContextGetterMap app_request_context_getter_map_;
    ChromeBrowserStateImplIOData* const io_data_;

    ios::ChromeBrowserState* const browser_state_;

    mutable bool initialized_;

    DISALLOW_COPY_AND_ASSIGN(Handle);
  };

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

  struct LazyParams {
    LazyParams();
    ~LazyParams();

    // All of these parameters are intended to be read on the IO thread.
    base::FilePath cookie_path;
    base::FilePath channel_id_path;
    base::FilePath cache_path;
    int cache_max_size;
  };

  ChromeBrowserStateImplIOData();
  ~ChromeBrowserStateImplIOData() override;

  void InitializeInternal(
      scoped_ptr<IOSChromeNetworkDelegate> chrome_network_delegate,
      ProfileParams* profile_params,
      ProtocolHandlerMap* protocol_handlers) const override;
  AppRequestContext* InitializeAppRequestContext(
      net::URLRequestContext* main_context) const override;
  AppRequestContext* AcquireIsolatedAppRequestContext(
      net::URLRequestContext* main_context) const override;

  // Deletes all network related data since |time|. It deletes transport
  // security state since |time| and also deletes HttpServerProperties data.
  // Works asynchronously, however if the |completion| callback is non-null,
  // it will be posted on the UI thread once the removal process completes.
  void ClearNetworkingHistorySinceOnIOThread(base::Time time,
                                             const base::Closure& completion);

  mutable scoped_ptr<IOSChromeNetworkDelegate> network_delegate_;

  // Lazy initialization params.
  mutable scoped_ptr<LazyParams> lazy_params_;

  mutable scoped_refptr<JsonPrefStore> network_json_store_;

  mutable scoped_ptr<net::HttpNetworkSession> http_network_session_;
  mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;

  // Same as |ChromeBrowserState::http_server_properties_|, owned there to
  // maintain destruction ordering.
  mutable net::HttpServerPropertiesManager* http_server_properties_manager_;

  mutable scoped_ptr<net::CookieStore> main_cookie_store_;

  mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;

  mutable scoped_ptr<net::SdchManager> sdch_manager_;
  mutable scoped_ptr<net::SdchOwner> sdch_policy_;

  // Parameters needed for isolated apps.
  base::FilePath profile_path_;
  int app_cache_max_size_;

  DISALLOW_COPY_AND_ASSIGN(ChromeBrowserStateImplIOData);
};

#endif  // IOS_CHROME_BROWSER_BROWSER_STATE_CHROME_BROWSER_STATE_IMPL_IO_DATA_H_