1 // Copyright 2014 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 COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_PEER_H_ 6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_PEER_H_ 7 8 #include <string> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/threading/thread.h" 15 #include "net/base/net_log.h" 16 #include "net/base/network_change_notifier.h" 17 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context_getter.h" 19 20 namespace net { 21 class NetLogLogger; 22 } // namespace net 23 24 namespace cronet { 25 26 struct URLRequestContextConfig; 27 28 // Implementation of the Chromium NetLog observer interface. 29 class NetLogObserver : public net::NetLog::ThreadSafeObserver { 30 public: 31 explicit NetLogObserver(int log_level) { log_level_ = log_level; } 32 33 virtual ~NetLogObserver() {} 34 35 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; 36 37 private: 38 int log_level_; 39 40 DISALLOW_COPY_AND_ASSIGN(NetLogObserver); 41 }; 42 43 // Fully configured |URLRequestContext|. 44 class URLRequestContextPeer : public net::URLRequestContextGetter { 45 public: 46 class URLRequestContextPeerDelegate 47 : public base::RefCountedThreadSafe<URLRequestContextPeerDelegate> { 48 public: 49 virtual void OnContextInitialized(URLRequestContextPeer* context) = 0; 50 51 protected: 52 friend class base::RefCountedThreadSafe<URLRequestContextPeerDelegate>; 53 54 virtual ~URLRequestContextPeerDelegate() {} 55 }; 56 57 URLRequestContextPeer(URLRequestContextPeerDelegate* delegate, 58 std::string user_agent, 59 int log_level, 60 const char* version); 61 void Initialize(scoped_ptr<URLRequestContextConfig> config); 62 63 const std::string& GetUserAgent(const GURL& url) const; 64 65 int logging_level() const { return logging_level_; } 66 67 const char* version() const { return version_; } 68 69 // net::URLRequestContextGetter implementation: 70 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; 71 virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() 72 const OVERRIDE; 73 74 void StartNetLogToFile(const std::string& file_name); 75 void StopNetLog(); 76 77 private: 78 scoped_refptr<URLRequestContextPeerDelegate> delegate_; 79 scoped_ptr<net::URLRequestContext> context_; 80 int logging_level_; 81 const char* version_; 82 std::string user_agent_; 83 base::Thread* network_thread_; 84 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 85 scoped_ptr<NetLogObserver> net_log_observer_; 86 scoped_ptr<net::NetLogLogger> net_log_logger_; 87 88 virtual ~URLRequestContextPeer(); 89 90 // Initializes |context_| on the IO thread. 91 void InitializeURLRequestContext(scoped_ptr<URLRequestContextConfig> config); 92 93 DISALLOW_COPY_AND_ASSIGN(URLRequestContextPeer); 94 }; 95 96 } // namespace cronet 97 98 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_PEER_H_ 99