All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WebRequestService.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Web.h"
6 #include "IWebRequestService.h"
7 #include "BidirectionalWebIOBus.h"
8 #include "ICallback.h"
9 #include "CallbackCollection.h"
10 #include "IWebLoadRequest.h"
11 #include "CacheStoreOperation.h"
12 #include "WebServiceCacheTasks.h"
13 #include "Tasks.h"
14 #include "Helpers.h"
15 #include "WebServiceCacheLoadWorkItemPool.h"
16 #include "WebServiceCacheStoreWorkItemPool.h"
17 
18 #include <set>
19 #include <string>
20 
21 namespace Eegeo
22 {
23  namespace Web
24  {
26  {
27  public:
29  const Eegeo::Web::WebIOConfig& config,
30  Eegeo::Web::TMessageBus& webMessageBus,
31  Helpers::IFileIO& fileIO,
32  Helpers::IHttpCache& httpCache,
33  const Eegeo::Web::IUrlProxyResolver& urlProxyResolver,
35  const Eegeo::Web::SSLSettings& sslSetting);
36 
38 
39  void EnqueueLoad(IWebLoadRequest* pWebRequest);
40  void EnqueueCancel(IWebLoadRequest* pWebRequest);
41 
42  void Update();
43 
44  bool HasOngoingNetworkActivity() const;
45  void RegisterNetworkActivityChanged(Helpers::ICallback1<const bool&>& callback);
46  void UnregisterNetworkActivityChanged(Helpers::ICallback1<const bool&>& callback);
47 
48  private:
49  void EnqueueCacheLoad(IWebLoadRequest* pWebRequest);
50  void DeleteCancelledRequests();
51  void OnWebResponseFromWireMessageHandler(const WebResponseFromWireMessage& message);
52  void OnWebResponseFromCacheMessageHandler(const WebResponseFromCacheMessage& message);
53  void OnWebResponseFallBackToCacheMessageHandler(const WebResponseFallBackToCacheMessage& message);
54  void ExecuteCallbackAndCleanup(IWebLoadRequest* pWebRequest, IWebResponse* pWebResponse);
55 
56  void CheckNetworkActivityChanged();
57 
58  Eegeo::Web::TMessageBus& m_webMessageBus;
59 
63 
64  std::set<IWebLoadRequest*> m_webRequestsAwaitingResponse;
65  std::vector<IWebLoadRequest*> m_cancelledRequests;
66 
67  // caching stuff, TODO: refactor out into own types
68  void DispatchCacheLoadRequestToWorkPool(IWebLoadRequest* pWebRequest);
69  void IssueCacheLoadRequests();
70  void IssueCacheStoreRequests();
71 
72  bool PendingCacheStoreOperationExists(const std::string& url) const;
73  void DestroyPendingCacheStoreOperations();
74 
75  std::list<IWebLoadRequest*> m_cacheRequestsNotYetSent;
76  std::set<IWebLoadRequest*> m_cacheRequestsAwaitingResponse;
77 
78  Eegeo::Helpers::IFileIO& m_fileIO;
79  Eegeo::Helpers::IHttpCache& m_httpCache;
80  const Eegeo::Web::IUrlProxyResolver& m_urlProxyResolver;
81  const Eegeo::Web::SSLSettings& m_sslSettings;
82 
84 
85  typedef std::deque<Cache::WebRequestCacheTasks::CacheStoreOperation> TCacheStoreOperations;
86  TCacheStoreOperations m_pendingCacheStoreOperations;
89  bool m_cacheLoadPoolExhaustedLastFrame;
90  bool m_cacheStorePoolExhaustedLastFrame;
91 
92  Helpers::CallbackCollection1<const bool&> m_networkActivityChangedCallbacks;
93  bool m_hasNetworkActivity;
94  };
95  }
96 }