[WK2] Make the WebNetworkInfoManager a supplement to the WebProcess
[WebKit-https.git] / Source / WebKit2 / WebProcess / NetworkInfo / WebNetworkInfoManager.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebNetworkInfoManager.h"
28
29 #if ENABLE(NETWORK_INFO)
30
31 #include "WebNetworkInfoManagerMessages.h"
32 #include "WebNetworkInfoManagerProxyMessages.h"
33 #include "WebPage.h"
34 #include "WebProcess.h"
35 #include <WebCore/NetworkInfo.h>
36 #include <WebCore/NetworkInfoController.h>
37 #include <limits.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 const char* WebNetworkInfoManager::supplementName()
44 {
45     return "WebNetworkInfoManager";
46 }
47
48 WebNetworkInfoManager::WebNetworkInfoManager(WebProcess* process)
49     : m_process(process)
50 {
51     m_process->addMessageReceiver(Messages::WebNetworkInfoManager::messageReceiverName(), this);
52 }
53
54 WebNetworkInfoManager::~WebNetworkInfoManager()
55 {
56 }
57
58 void WebNetworkInfoManager::registerWebPage(WebPage* page)
59 {
60     bool wasEmpty = m_pageSet.isEmpty();
61
62     m_pageSet.add(page);
63
64     if (wasEmpty)
65         m_process->connection()->send(Messages::WebNetworkInfoManagerProxy::StartUpdating(), 0);
66 }
67
68 void WebNetworkInfoManager::unregisterWebPage(WebPage* page)
69 {
70     m_pageSet.remove(page);
71
72     if (m_pageSet.isEmpty())
73         m_process->connection()->send(Messages::WebNetworkInfoManagerProxy::StopUpdating(), 0);
74 }
75
76 double WebNetworkInfoManager::bandwidth(WebPage* page) const
77 {
78     // The spec indicates that we should return "infinity" if the bandwidth is unknown.
79     double bandwidth = std::numeric_limits<double>::infinity();
80     m_process->connection()->sendSync(Messages::WebNetworkInfoManagerProxy::GetBandwidth(), Messages::WebNetworkInfoManagerProxy::GetBandwidth::Reply(bandwidth), page->pageID());
81     return bandwidth;
82 }
83
84 bool WebNetworkInfoManager::metered(WebPage* page) const
85 {
86     bool metered = false;
87     m_process->connection()->sendSync(Messages::WebNetworkInfoManagerProxy::IsMetered(), Messages::WebNetworkInfoManagerProxy::IsMetered::Reply(metered), page->pageID());
88     return metered;
89 }
90
91 void WebNetworkInfoManager::didChangeNetworkInformation(const AtomicString& eventType, const WebNetworkInfo::Data& data)
92 {
93     RefPtr<NetworkInfo> networkInformation = NetworkInfo::create(data.bandwidth, data.metered);
94
95     HashSet<WebPage*>::const_iterator it = m_pageSet.begin();
96     HashSet<WebPage*>::const_iterator end = m_pageSet.end();
97     for (; it != end; ++it) {
98         WebPage* page = *it;
99         if (page->corePage())
100             NetworkInfoController::from(page->corePage())->didChangeNetworkInformation(eventType, networkInformation.get());
101     }
102 }
103
104 } // namespace WebKit
105
106 #endif // ENABLE(NETWORK_INFO)