1 /*
2  * Copyright (C) 2010 Apple Inc. 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 "WebPageGroup.h"
28 
29 #include "WebPageProxy.h"
30 #include "WebPreferences.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/text/StringConcatenate.h>
33 
34 namespace WebKit {
35 
generatePageGroupID()36 static uint64_t generatePageGroupID()
37 {
38     static uint64_t uniquePageGroupID = 1;
39     return uniquePageGroupID++;
40 }
41 
42 typedef HashMap<uint64_t, WebPageGroup*> WebPageGroupMap;
43 
webPageGroupMap()44 static WebPageGroupMap& webPageGroupMap()
45 {
46     DEFINE_STATIC_LOCAL(WebPageGroupMap, map, ());
47     return map;
48 }
49 
create(const String & identifier,bool visibleToInjectedBundle,bool visibleToHistoryClient)50 PassRefPtr<WebPageGroup> WebPageGroup::create(const String& identifier, bool visibleToInjectedBundle, bool visibleToHistoryClient)
51 {
52     RefPtr<WebPageGroup> pageGroup = adoptRef(new WebPageGroup(identifier, visibleToInjectedBundle, visibleToHistoryClient));
53 
54     webPageGroupMap().set(pageGroup->pageGroupID(), pageGroup.get());
55 
56     return pageGroup.release();
57 }
58 
get(uint64_t pageGroupID)59 WebPageGroup* WebPageGroup::get(uint64_t pageGroupID)
60 {
61     return webPageGroupMap().get(pageGroupID);
62 }
63 
WebPageGroup(const String & identifier,bool visibleToInjectedBundle,bool visibleToHistoryClient)64 WebPageGroup::WebPageGroup(const String& identifier, bool visibleToInjectedBundle, bool visibleToHistoryClient)
65 {
66     m_data.pageGroupID = generatePageGroupID();
67 
68     if (!identifier.isNull())
69         m_data.identifer = identifier;
70     else
71         m_data.identifer = m_data.identifer = makeString("__uniquePageGroupID-", String::number(m_data.pageGroupID));
72 
73     m_data.visibleToInjectedBundle = visibleToInjectedBundle;
74     m_data.visibleToHistoryClient = visibleToHistoryClient;
75 }
76 
~WebPageGroup()77 WebPageGroup::~WebPageGroup()
78 {
79     if (m_preferences)
80         m_preferences->removePageGroup(this);
81     webPageGroupMap().remove(pageGroupID());
82 }
83 
addPage(WebPageProxy * page)84 void WebPageGroup::addPage(WebPageProxy* page)
85 {
86     m_pages.add(page);
87 }
88 
removePage(WebPageProxy * page)89 void WebPageGroup::removePage(WebPageProxy* page)
90 {
91     m_pages.remove(page);
92 }
93 
setPreferences(WebPreferences * preferences)94 void WebPageGroup::setPreferences(WebPreferences* preferences)
95 {
96     if (preferences == m_preferences)
97         return;
98 
99     if (!m_preferences) {
100         m_preferences = preferences;
101         m_preferences->addPageGroup(this);
102     } else {
103         m_preferences->removePageGroup(this);
104         m_preferences = preferences;
105         m_preferences->addPageGroup(this);
106 
107         preferencesDidChange();
108     }
109 }
110 
preferences() const111 WebPreferences* WebPageGroup::preferences() const
112 {
113     if (!m_preferences) {
114         if (!m_data.identifer.isNull())
115             m_preferences = WebPreferences::create(m_data.identifer);
116         else
117             m_preferences = WebPreferences::create();
118         m_preferences->addPageGroup(const_cast<WebPageGroup*>(this));
119     }
120     return m_preferences.get();
121 }
122 
preferencesDidChange()123 void WebPageGroup::preferencesDidChange()
124 {
125     for (HashSet<WebPageProxy*>::iterator it = m_pages.begin(), end = m_pages.end(); it != end; ++it) {
126         WebPageProxy* page = *it;
127         page->preferencesDidChange();
128     }
129 }
130 
131 } // namespace WebKit
132