// 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 #include #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; class SdchOwner; } // namespace net namespace cronet { struct URLRequestContextConfig; typedef base::Callback 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 { public: virtual void OnContextInitialized(URLRequestContextAdapter* context) = 0; protected: friend class base::RefCountedThreadSafe; virtual ~URLRequestContextAdapterDelegate() {} }; URLRequestContextAdapter(URLRequestContextAdapterDelegate* delegate, std::string user_agent); void Initialize(scoped_ptr 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 GetNetworkTaskRunner() const override; void StartNetLogToFile(const std::string& file_name, bool log_all); 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, bool log_all); // Helper function to stop writing NetLog data to file. This should only be // run after context is initialized. void StopNetLogHelper(); scoped_refptr delegate_; scoped_ptr context_; std::string user_agent_; bool load_disable_cache_; base::Thread* network_thread_; scoped_ptr net_log_observer_; scoped_ptr write_to_file_observer_; scoped_ptr proxy_config_service_; scoped_ptr sdch_owner_; scoped_ptr config_; // A queue of tasks that need to be run after context has been initialized. std::queue tasks_waiting_for_context_; bool is_context_initialized_; DISALLOW_COPY_AND_ASSIGN(URLRequestContextAdapter); }; } // namespace cronet #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_CONTEXT_ADAPTER_H_