summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_plugin_registry.cc
blob: b91ba72ed3582b8f4ea313b4a5aec92dd7ae1a1a (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
// 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 "content/renderer/pepper/pepper_plugin_registry.h"

#include <stddef.h>

#include "base/logging.h"
#include "content/common/pepper_plugin_list.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "content/renderer/pepper/plugin_module.h"
#include "ppapi/shared_impl/ppapi_permissions.h"

namespace content {

// static
PepperPluginRegistry* PepperPluginRegistry::GetInstance() {
  static PepperPluginRegistry* registry = nullptr;
  // This object leaks.  It is a temporary hack to work around a crash.
  // http://code.google.com/p/chromium/issues/detail?id=63234
  if (!registry) {
    registry = new PepperPluginRegistry;
    registry->Initialize();
  }
  return registry;
}

const PepperPluginInfo* PepperPluginRegistry::GetInfoForPlugin(
    const WebPluginInfo& info) {
  for (size_t i = 0; i < plugin_list_.size(); ++i) {
    if (info.path == plugin_list_[i].path)
      return &plugin_list_[i];
  }
  // We did not find the plugin in our list. But wait! the plugin can also
  // be a latecomer, as it happens with pepper flash. This information
  // is actually in |info| and we can use it to construct it and add it to
  // the list. This same deal needs to be done in the browser side in
  // PluginService.
  PepperPluginInfo plugin;
  if (!MakePepperPluginInfo(info, &plugin))
    return nullptr;

  plugin_list_.push_back(plugin);
  return &plugin_list_.back();
}

PluginModule* PepperPluginRegistry::GetLiveModule(const base::FilePath& path) {
  NonOwningModuleMap::iterator module_iter = live_modules_.find(path);
  if (module_iter == live_modules_.end())
    return nullptr;

  // Check the instances for the module to see if they've all been Delete()d.
  // We don't want to return a PluginModule in that case, since the plugin may
  // have exited already.
  const PluginModule::PluginInstanceSet& instance_set =
      module_iter->second->GetAllInstances();

  // If instance_set is empty, InstanceCreated() hasn't been called yet, so
  // it's safe to return the PluginModule.
  if (instance_set.empty())
    return module_iter->second;

  PluginModule::PluginInstanceSet::const_iterator instance_iter =
      instance_set.begin();
  while (instance_iter != instance_set.end()) {
    if (!(*instance_iter)->is_deleted())
      return module_iter->second;
    ++instance_iter;
  }
  return nullptr;
}

void PepperPluginRegistry::AddLiveModule(const base::FilePath& path,
                                         PluginModule* module) {
  DCHECK(live_modules_.find(path) == live_modules_.end());
  live_modules_[path] = module;
}

void PepperPluginRegistry::PluginModuleDead(PluginModule* dead_module) {
  // DANGER: Don't dereference the dead_module pointer! It may be in the
  // process of being deleted.

  // Modules aren't destroyed very often and there are normally at most a
  // couple of them. So for now we just do a brute-force search.
  for (NonOwningModuleMap::iterator i = live_modules_.begin();
       i != live_modules_.end();
       ++i) {
    if (i->second == dead_module) {
      live_modules_.erase(i);
      return;
    }
  }
  // Can occur in tests.
}

PepperPluginRegistry::~PepperPluginRegistry() {
  // Explicitly clear all preloaded modules first. This will cause callbacks
  // to erase these modules from the live_modules_ list, and we don't want
  // that to happen implicitly out-of-order.
  preloaded_modules_.clear();

  DCHECK(live_modules_.empty());
}

PepperPluginRegistry::PepperPluginRegistry() {}

void PepperPluginRegistry::Initialize() {
  ComputePepperPluginList(&plugin_list_);

  // Note that in each case, AddLiveModule must be called before completing
  // initialization. If we bail out (in the continue clauses) before saving
  // the initialized module, it will still try to unregister itself in its
  // destructor.
  for (size_t i = 0; i < plugin_list_.size(); i++) {
    const PepperPluginInfo& current = plugin_list_[i];
    if (current.is_out_of_process)
      continue;  // Out of process plugins need no special pre-initialization.

    scoped_refptr<PluginModule> module =
        new PluginModule(current.name,
                         current.version,
                         current.path,
                         ppapi::PpapiPermissions(current.permissions));
    AddLiveModule(current.path, module.get());
    if (current.is_internal) {
      if (!module->InitAsInternalPlugin(current.internal_entry_points)) {
        DVLOG(1) << "Failed to load pepper module: " << current.path.value();
        continue;
      }
    } else {
      // Preload all external plugins we're not running out of process.
      if (!module->InitAsLibrary(current.path)) {
        DVLOG(1) << "Failed to load pepper module: " << current.path.value();
        continue;
      }
    }
    preloaded_modules_[current.path] = module;
  }
}

}  // namespace content