Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (c) 2008, 2009, Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "PluginDataChromium.h"
     33 
     34 #include "ChromiumBridge.h"
     35 
     36 namespace WebCore {
     37 
     38 static PluginInfo* clone(const PluginInfo* info)
     39 {
     40     PluginInfo* result = new PluginInfo();
     41     result->name = info->name;
     42     result->desc = info->desc;
     43     result->file = info->file;
     44     for (size_t i = 0; i < info->mimes.size(); ++i) {
     45         MimeClassInfo* mime = new MimeClassInfo();
     46         mime->type = info->mimes[i]->type;
     47         mime->desc = info->mimes[i]->desc;
     48         mime->suffixes = info->mimes[i]->suffixes;
     49         mime->plugin = result;
     50         result->mimes.append(mime);
     51     }
     52     return result;
     53 }
     54 
     55 class PluginCache {
     56 public:
     57     PluginCache() : m_loaded(false), m_refresh(false) {}
     58     ~PluginCache() { reset(false); }
     59 
     60     void reset(bool refresh)
     61     {
     62         for (size_t i = 0; i < m_plugins.size(); ++i)
     63             deleteAllValues(m_plugins[i]->mimes);
     64         deleteAllValues(m_plugins);
     65         m_plugins.clear();
     66         m_loaded = false;
     67         m_refresh = refresh;
     68     }
     69 
     70     const Vector<PluginInfo*>& plugins()
     71     {
     72         if (!m_loaded) {
     73             ChromiumBridge::plugins(m_refresh, &m_plugins);
     74             m_loaded = true;
     75             m_refresh = false;
     76         }
     77         return m_plugins;
     78     }
     79 
     80 private:
     81     Vector<PluginInfo*> m_plugins;
     82     bool m_loaded;
     83     bool m_refresh;
     84 };
     85 
     86 static PluginCache pluginCache;
     87 
     88 void PluginData::initPlugins()
     89 {
     90     const Vector<PluginInfo*>& plugins = pluginCache.plugins();
     91     for (size_t i = 0; i < plugins.size(); ++i)
     92         m_plugins.append(clone(plugins[i]));
     93 }
     94 
     95 void PluginData::refresh()
     96 {
     97     pluginCache.reset(true);
     98     pluginCache.plugins();  // Force the plugins to be reloaded now.
     99 }
    100 
    101 String getPluginMimeTypeFromExtension(const String& extension)
    102 {
    103     const Vector<PluginInfo*>& plugins = pluginCache.plugins();
    104     for (size_t i = 0; i < plugins.size(); ++i) {
    105         for (size_t j = 0; j < plugins[i]->mimes.size(); ++j) {
    106             MimeClassInfo* mime = plugins[i]->mimes[j];
    107             Vector<String> extensions;
    108             mime->suffixes.split(",", extensions);
    109             for (size_t k = 0; k < extensions.size(); ++k) {
    110                 if (extension == extensions[k])
    111                     return mime->type;
    112             }
    113         }
    114     }
    115     return String();
    116 }
    117 
    118 } // namespace WebCore
    119