summaryrefslogtreecommitdiffstats
path: root/components/cronet/android/url_request_context_adapter.h
blob: 932ad9250b04865437922a0af0fd9836a7fb67f8 (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 2014 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 COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_
#define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_

#include <queue>
#include <string>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread.h"
#include "net/log/net_log.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"

namespace net {

class WriteToFileNetLogObserver;

class ProxyConfigService;

}  // namespace net

namespace cronet {

struct URLRequestContextConfig;
typedef base::Callback<void(void)> RunAfterContextInitTask;

// Implementation of the Chromium NetLog observer interface.
class NetLogObserver : public net::NetLog::ThreadSafeObserver {
 public:
  NetLogObserver() {}

  ~NetLogObserver() override {}

  void OnAddEntry(const net::NetLog::Entry& entry) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(NetLogObserver);
};

// Fully configured |URLRequestContext|.
class URLRequestContextAdapter : public net::URLRequestContextGetter {
 public:
  class URLRequestContextAdapterDelegate
      : public base::RefCountedThreadSafe<URLRequestContextAdapterDelegate> {
   public:
    virtual void OnContextInitialized(URLRequestContextAdapter* context) = 0;

   protected:
    friend class base::RefCountedThreadSafe<URLRequestContextAdapterDelegate>;

    virtual ~URLRequestContextAdapterDelegate() {}
  };

  URLRequestContextAdapter(URLRequestContextAdapterDelegate* delegate,
                           std::string user_agent);
  void Initialize(scoped_ptr<URLRequestContextConfig> config);

  // Posts a task that might depend on the context being initialized
  // to the network thread.
  void PostTaskToNetworkThread(const tracked_objects::Location& posted_from,
                               const RunAfterContextInitTask& callback);

  // Runs a task that might depend on the context being initialized.
  // This method should only be run on the network thread.
  void RunTaskAfterContextInitOnNetworkThread(
      const RunAfterContextInitTask& callback);

  const std::string& GetUserAgent(const GURL& url) const;

  bool load_disable_cache() const { return load_disable_cache_; }

  // net::URLRequestContextGetter implementation:
  net::URLRequestContext* GetURLRequestContext() override;
  scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
      const override;

  void StartNetLogToFile(const std::string& file_name);
  void StopNetLog();

  // Called on main Java thread to initialize URLRequestContext.
  void InitRequestContextOnMainThread();

 private:
  ~URLRequestContextAdapter() override;

  // Initializes |context_| on the Network thread.
  void InitRequestContextOnNetworkThread();

  // Helper function to start writing NetLog data to file. This should only be
  // run after context is initialized.
  void StartNetLogToFileHelper(const std::string& file_name);
  // Helper function to stop writing NetLog data to file. This should only be
  // run after context is initialized.
  void StopNetLogHelper();

  scoped_refptr<URLRequestContextAdapterDelegate> delegate_;
  scoped_ptr<net::URLRequestContext> context_;
  std::string user_agent_;
  bool load_disable_cache_;
  base::Thread* network_thread_;
  scoped_ptr<NetLogObserver> net_log_observer_;
  scoped_ptr<net::WriteToFileNetLogObserver> net_log_logger_;
  scoped_ptr<net::ProxyConfigService> proxy_config_service_;
  scoped_ptr<URLRequestContextConfig> config_;

  // A queue of tasks that need to be run after context has been initialized.
  std::queue<RunAfterContextInitTask> tasks_waiting_for_context_;
  bool is_context_initialized_;

  DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter);
};

}  // namespace cronet

#endif  // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_