• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "WebKitDLL.h"
28 #include "WebIconFetcher.h"
29 
30 #include "MemoryStream.h"
31 #include <WebCore/COMPtr.h>
32 #include <WebCore/SharedBuffer.h>
33 #include <wtf/PassRefPtr.h>
34 
35 using namespace WebCore;
36 
37 class WebIconFetcherClient : public IconFetcherClient {
38 public:
WebIconFetcherClient(IWebIconFetcherDelegate * delegate)39     WebIconFetcherClient(IWebIconFetcherDelegate* delegate)
40         : m_fetcher(0)
41         , m_delegate(delegate)
42     {
43     }
44 
finishedFetchingIcon(PassRefPtr<SharedBuffer> iconData)45     virtual void finishedFetchingIcon(PassRefPtr<SharedBuffer> iconData)
46     {
47         COMPtr<MemoryStream> memoryStream;
48         if (iconData)
49             memoryStream = MemoryStream::createInstance(iconData);
50 
51         m_delegate->finishedLoadingIcon(m_fetcher, memoryStream.get());
52 
53         delete this;
54     }
55 
setFetcher(WebIconFetcher * fetcher)56    void setFetcher(WebIconFetcher *fetcher) { m_fetcher = fetcher; }
57 
58 private:
59     WebIconFetcher* m_fetcher;
60     COMPtr<IWebIconFetcherDelegate> m_delegate;
61 };
62 
63 // WebIconFetcher -------------------------------------------------------------------
64 
fetchApplicationIcon(Frame * frame,IWebIconFetcherDelegate * delegate)65 WebIconFetcher* WebIconFetcher::fetchApplicationIcon(Frame* frame, IWebIconFetcherDelegate* delegate)
66 {
67     WebIconFetcherClient* client = new WebIconFetcherClient(delegate);
68 
69     RefPtr<IconFetcher> fetcher = IconFetcher::create(frame, client);
70 
71     if (!fetcher)
72         return 0;
73 
74     COMPtr<WebIconFetcher> iconFetcher = new WebIconFetcher(fetcher.release());
75     client->setFetcher(iconFetcher.get());
76 
77     return iconFetcher.releaseRef();
78 }
79 
WebIconFetcher(PassRefPtr<IconFetcher> iconFetcher)80 WebIconFetcher::WebIconFetcher(PassRefPtr<IconFetcher> iconFetcher)
81     : m_refCount(0)
82     , m_iconFetcher(iconFetcher)
83 {
84     gClassCount++;
85 }
86 
~WebIconFetcher()87 WebIconFetcher::~WebIconFetcher()
88 {
89     gClassCount--;
90 }
91 
92 // IUnknown -------------------------------------------------------------------
93 
QueryInterface(REFIID riid,void ** ppvObject)94 HRESULT STDMETHODCALLTYPE WebIconFetcher::QueryInterface(REFIID riid, void** ppvObject)
95 {
96     *ppvObject = 0;
97     if (IsEqualGUID(riid, IID_IUnknown))
98         *ppvObject = static_cast<IUnknown*>(this);
99     else if (IsEqualGUID(riid, IID_IWebIconFetcher))
100         *ppvObject = static_cast<IWebIconFetcher*>(this);
101     else
102         return E_NOINTERFACE;
103 
104     AddRef();
105     return S_OK;
106 }
107 
AddRef()108 ULONG STDMETHODCALLTYPE WebIconFetcher::AddRef()
109 {
110     return ++m_refCount;
111 }
112 
Release()113 ULONG STDMETHODCALLTYPE WebIconFetcher::Release()
114 {
115     ULONG newRef = --m_refCount;
116     if (!newRef)
117         delete this;
118 
119     return newRef;
120 }
121 
122 // IUnknown -------------------------------------------------------------------
123 
cancel()124 HRESULT STDMETHODCALLTYPE WebIconFetcher::cancel()
125 {
126     m_iconFetcher->cancel();
127 
128     return S_OK;
129 }
130