1 /* 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) 4 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) 5 * Copyright (C) 2008 Alp Toker <alp (at) atoker.com> 6 * Copyright (C) Research In Motion Limited 2009. All rights reserved. 7 * Copyright (C) 2011 Kris Jordan <krisjordan (at) gmail.com> 8 * Copyright (C) 2011 Google Inc. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 20 * its contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "config.h" 36 #include "core/loader/IconController.h" 37 38 #include "core/dom/Document.h" 39 #include "core/dom/IconURL.h" 40 #include "core/page/Frame.h" 41 42 namespace WebCore { 43 44 IconController::IconController(Frame* frame) 45 : m_frame(frame) 46 { 47 } 48 49 IconController::~IconController() 50 { 51 } 52 53 KURL IconController::url() 54 { 55 IconURLs iconURLs = urlsForTypes(Favicon); 56 return iconURLs.isEmpty() ? KURL() : iconURLs[0].m_iconURL; 57 } 58 59 IconURL IconController::iconURL(IconType iconType) const 60 { 61 IconURL result; 62 const Vector<IconURL>& iconURLs = m_frame->document()->iconURLs(iconType); 63 Vector<IconURL>::const_iterator iter(iconURLs.begin()); 64 for (; iter != iconURLs.end(); ++iter) { 65 if (result.m_iconURL.isEmpty() || !iter->m_mimeType.isEmpty()) 66 result = *iter; 67 } 68 69 return result; 70 } 71 72 IconURLs IconController::urlsForTypes(int iconTypesMask) 73 { 74 IconURLs iconURLs; 75 if (m_frame->tree() && m_frame->tree()->parent()) 76 return iconURLs; 77 78 if (iconTypesMask & Favicon && !appendToIconURLs(Favicon, &iconURLs)) 79 iconURLs.append(defaultURL(Favicon)); 80 81 #if ENABLE(TOUCH_ICON_LOADING) 82 appendToIconURLs(TouchPrecomposedIcon, &iconURLs); 83 appendToIconURLs(TouchIcon, &iconURLs); 84 #endif 85 86 // Finally, append all remaining icons of this type. 87 const Vector<IconURL>& allIconURLs = m_frame->document()->iconURLs(iconTypesMask); 88 for (Vector<IconURL>::const_iterator iter = allIconURLs.begin(); iter != allIconURLs.end(); ++iter) { 89 int i; 90 int iconCount = iconURLs.size(); 91 for (i = 0; i < iconCount; ++i) { 92 if (*iter == iconURLs.at(i)) 93 break; 94 } 95 if (i == iconCount) 96 iconURLs.append(*iter); 97 } 98 99 return iconURLs; 100 } 101 102 bool IconController::appendToIconURLs(IconType iconType, IconURLs* iconURLs) 103 { 104 IconURL faviconURL = iconURL(iconType); 105 if (faviconURL.m_iconURL.isEmpty()) 106 return false; 107 108 iconURLs->append(faviconURL); 109 return true; 110 } 111 112 IconURL IconController::defaultURL(IconType iconType) 113 { 114 // Don't return a favicon iconURL unless we're http or https 115 KURL documentURL = m_frame->document()->url(); 116 if (!documentURL.protocolIsInHTTPFamily()) 117 return IconURL(); 118 119 KURL url; 120 bool couldSetProtocol = url.setProtocol(documentURL.protocol()); 121 ASSERT_UNUSED(couldSetProtocol, couldSetProtocol); 122 url.setHost(documentURL.host()); 123 if (documentURL.hasPort()) 124 url.setPort(documentURL.port()); 125 126 if (iconType == Favicon) { 127 url.setPath("/favicon.ico"); 128 return IconURL::defaultIconURL(url, Favicon); 129 } 130 return IconURL(); 131 } 132 133 } 134