summaryrefslogtreecommitdiffstats
path: root/chrome/browser/plugin_finder.cc
blob: 2e90b5450293574e103bdf6b6c221d2a1e8dfa49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/plugin_finder.h"

#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/message_loop.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/plugin_installer.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"

// static
PluginFinder* PluginFinder::GetInstance() {
  return Singleton<PluginFinder>::get();
}

PluginFinder::PluginFinder() : plugin_list_(LoadPluginList()) {
  if (!plugin_list_.get()) {
    NOTREACHED();
    plugin_list_.reset(new base::ListValue());
  }
}

// static
scoped_ptr<base::ListValue> PluginFinder::LoadPluginList() {
  return scoped_ptr<base::ListValue>(LoadPluginListInternal()).Pass();
}

base::ListValue* PluginFinder::LoadPluginListInternal() {
#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
  base::StringPiece json_resource(
      ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_PLUGIN_DB_JSON));
  bool allow_trailing_comma = false;
  std::string error_str;
  scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
      json_resource.as_string(),
      allow_trailing_comma,
      NULL,
      &error_str));
  if (!value.get()) {
    DLOG(ERROR) << error_str;
    return NULL;
  }
  base::DictionaryValue* dict = NULL;
  if (!value->GetAsDictionary(&dict))
    return NULL;
  base::ListValue* list = NULL;
  if (!dict->GetList("plugins", &list))
    return NULL;
  return list->DeepCopy();
#else
  return new base::ListValue();
#endif
}

PluginFinder::~PluginFinder() {
  STLDeleteValues(&installers_);
}

void PluginFinder::FindPlugin(
    const std::string& mime_type,
    const std::string& language,
    const FindPluginCallback& callback) {
  PluginInstaller* installer = FindPluginInternal(mime_type, language);
  MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, installer));
}

void PluginFinder::FindPluginWithIdentifier(
    const std::string& identifier,
    const FindPluginCallback& found_callback) {
  PluginInstaller* installer = NULL;
  std::map<std::string, PluginInstaller*>::const_iterator it =
      installers_.find(identifier);
  if (it != installers_.end()) {
    installer = it->second;
  } else {
    for (ListValue::const_iterator plugin_it = plugin_list_->begin();
         plugin_it != plugin_list_->end(); ++plugin_it) {
      const base::DictionaryValue* plugin = NULL;
      if (!(*plugin_it)->GetAsDictionary(&plugin)) {
        NOTREACHED();
        continue;
      }
      std::string id;
      bool success = plugin->GetString("identifier", &id);
      DCHECK(success);
      if (id == identifier) {
        installer = CreateInstaller(identifier, plugin);
        break;
      }
    }
  }
  MessageLoop::current()->PostTask(FROM_HERE,
                                   base::Bind(found_callback, installer));
}

PluginInstaller* PluginFinder::CreateInstaller(
    const std::string& identifier,
    const base::DictionaryValue* plugin_dict) {
  DCHECK(!installers_[identifier]);
  std::string url;
  bool success = plugin_dict->GetString("url", &url);
  DCHECK(success);
  std::string help_url;
  plugin_dict->GetString("help_url", &help_url);
  string16 name;
  success = plugin_dict->GetString("name", &name);
  DCHECK(success);
  bool display_url = false;
  plugin_dict->GetBoolean("displayurl", &display_url);
  PluginInstaller*installer = new PluginInstaller(identifier,
                                                  GURL(url),
                                                  GURL(help_url),
                                                  name,
                                                  display_url);
  installers_[identifier] = installer;
  return installer;
}

PluginInstaller* PluginFinder::FindPluginInternal(
    const std::string& mime_type,
    const std::string& language) {
  if (!g_browser_process->local_state()->GetBoolean(
          prefs::kDisablePluginFinder)) {
    for (ListValue::const_iterator plugin_it = plugin_list_->begin();
         plugin_it != plugin_list_->end(); ++plugin_it) {
      const base::DictionaryValue* plugin = NULL;
      if (!(*plugin_it)->GetAsDictionary(&plugin)) {
        NOTREACHED();
        continue;
      }
      std::string language_str;
      bool success = plugin->GetString("lang", &language_str);
      DCHECK(success);
      if (language_str != language)
        continue;
      ListValue* mime_types = NULL;
      success = plugin->GetList("mime_types", &mime_types);
      DCHECK(success);
      for (ListValue::const_iterator mime_type_it = mime_types->begin();
           mime_type_it != mime_types->end(); ++mime_type_it) {
        std::string mime_type_str;
        success = (*mime_type_it)->GetAsString(&mime_type_str);
        DCHECK(success);
        if (mime_type_str == mime_type) {
          std::string identifier;
          bool success = plugin->GetString("identifier", &identifier);
          DCHECK(success);
          std::map<std::string, PluginInstaller*>::const_iterator it =
              installers_.find(identifier);
          if (it != installers_.end())
            return it->second;
          return CreateInstaller(identifier, plugin);
        }
      }
    }
  }
  return NULL;
}