Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/plugin_loader_posix.h" | 5 #include "content/browser/plugin_loader_posix.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | |
| 8 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/metrics/histogram.h" | |
| 9 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 10 #include "content/common/utility_messages.h" | 12 #include "content/common/utility_messages.h" |
| 11 #include "webkit/plugins/npapi/plugin_list.h" | 13 #include "webkit/plugins/npapi/plugin_list.h" |
| 12 | 14 |
| 13 namespace { | 15 using webkit::npapi::PluginList; |
| 14 | 16 |
| 15 void RunGetPluginsCallback(const PluginService::GetPluginsCallback& callback, | 17 PluginLoaderPosix::PluginLoaderPosix() |
| 16 const std::vector<webkit::WebPluginInfo> plugins) { | 18 : next_load_index_(0) { |
| 17 callback.Run(plugins); | |
| 18 } | 19 } |
| 19 | 20 |
| 20 } // namespace | |
| 21 | |
| 22 // static | |
| 23 void PluginLoaderPosix::LoadPlugins( | 21 void PluginLoaderPosix::LoadPlugins( |
| 24 base::MessageLoopProxy* target_loop, | 22 scoped_refptr<base::MessageLoopProxy> target_loop, |
| 25 const PluginService::GetPluginsCallback& callback) { | 23 const PluginService::GetPluginsCallback& callback) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 27 | 25 |
| 28 PluginLoaderPosix* client = new PluginLoaderPosix(target_loop, callback); | 26 callbacks_.push_back(PendingCallback(target_loop, callback)); |
| 29 UtilityProcessHost* process_host = | |
| 30 new UtilityProcessHost(client, BrowserThread::IO); | |
| 31 process_host->set_no_sandbox(true); | |
| 32 #if defined(OS_MACOSX) | |
| 33 process_host->set_child_flags(ChildProcessHost::CHILD_ALLOW_HEAP_EXECUTION); | |
| 34 #endif | |
| 35 | 27 |
| 36 std::vector<FilePath> extra_plugin_paths; | 28 if (callbacks_.size() == 1) { |
| 37 std::vector<FilePath> extra_plugin_dirs; | 29 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 38 std::vector<webkit::WebPluginInfo> internal_plugins; | 30 base::Bind(&PluginLoaderPosix::GetPluginsToLoad, this)); |
| 39 webkit::npapi::PluginList::Singleton()->GetPluginPathListsToLoad( | 31 } |
| 40 &extra_plugin_paths, &extra_plugin_dirs, &internal_plugins); | |
| 41 | |
| 42 process_host->Send(new UtilityMsg_LoadPlugins( | |
| 43 extra_plugin_paths, extra_plugin_dirs, internal_plugins)); | |
| 44 } | 32 } |
| 45 | 33 |
| 46 bool PluginLoaderPosix::OnMessageReceived(const IPC::Message& message) { | 34 bool PluginLoaderPosix::OnMessageReceived(const IPC::Message& message) { |
| 47 bool handled = true; | 35 bool handled = true; |
| 48 IPC_BEGIN_MESSAGE_MAP(PluginLoaderPosix, message) | 36 IPC_BEGIN_MESSAGE_MAP(PluginLoaderPosix, message) |
| 49 IPC_MESSAGE_HANDLER(UtilityHostMsg_LoadedPlugins, OnGotPlugins) | 37 IPC_MESSAGE_HANDLER(UtilityHostMsg_LoadedPlugin, OnPluginLoaded) |
| 38 IPC_MESSAGE_HANDLER(UtilityHostMsg_LoadPluginFailed, OnPluginLoadFailed) | |
| 50 IPC_MESSAGE_UNHANDLED(handled = false) | 39 IPC_MESSAGE_UNHANDLED(handled = false) |
| 51 IPC_END_MESSAGE_MAP() | 40 IPC_END_MESSAGE_MAP() |
| 52 return handled; | 41 return handled; |
| 53 } | 42 } |
| 54 | 43 |
| 55 void PluginLoaderPosix::OnProcessCrashed(int exit_code) { | 44 void PluginLoaderPosix::OnProcessCrashed(int exit_code) { |
| 56 LOG(ERROR) << "Out-of-process plugin loader crashed with code " << exit_code | 45 canonical_list_.erase(canonical_list_.begin(), |
| 57 << ". You will have no plugins!"; | 46 canonical_list_.begin() + next_load_index_ + 1); |
| 58 // Don't leave callers hanging. | 47 next_load_index_ = 0; |
| 59 OnGotPlugins(std::vector<webkit::WebPluginInfo>()); | 48 LoadPluginsInternal(); |
| 60 } | 49 } |
| 61 | 50 |
| 62 PluginLoaderPosix::PluginLoaderPosix( | 51 bool PluginLoaderPosix::Send(IPC::Message* message) { |
| 63 base::MessageLoopProxy* target_loop, | 52 DCHECK(process_host_); |
|
jam
2011/10/20 00:16:16
nit: avoid this dcheck before variable use. in rel
Robert Sesek
2011/10/20 16:00:21
Done.
| |
| 64 const PluginService::GetPluginsCallback& callback) | 53 return process_host_->Send(message); |
| 65 : target_loop_(target_loop), | |
| 66 callback_(callback) { | |
| 67 } | 54 } |
| 68 | 55 |
| 69 PluginLoaderPosix::~PluginLoaderPosix() { | 56 PluginLoaderPosix::~PluginLoaderPosix() { |
| 70 } | 57 } |
| 71 | 58 |
| 72 void PluginLoaderPosix::OnGotPlugins( | 59 void PluginLoaderPosix::GetPluginsToLoad() { |
| 73 const std::vector<webkit::WebPluginInfo>& plugins) { | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 74 webkit::npapi::PluginList::Singleton()->SetPlugins(plugins); | 61 |
| 75 target_loop_->PostTask(FROM_HERE, | 62 base::TimeTicks start_time(base::TimeTicks::Now()); |
| 76 base::Bind(&RunGetPluginsCallback, callback_, plugins)); | 63 |
| 64 loaded_plugins_.clear(); | |
| 65 next_load_index_ = 0; | |
| 66 | |
| 67 canonical_list_.clear(); | |
| 68 webkit::npapi::PluginList::Singleton()->GetPluginPathsToLoad( | |
| 69 &canonical_list_); | |
| 70 | |
| 71 internal_plugins_.clear(); | |
| 72 PluginList::Singleton()->GetInternalPlugins(&internal_plugins_); | |
| 73 | |
| 74 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 75 base::Bind(&PluginLoaderPosix::LoadPluginsInternal, | |
| 76 make_scoped_refptr(this))); | |
| 77 | |
| 78 HISTOGRAM_TIMES("PluginLoaderPosix.GetPluginList", | |
| 79 (base::TimeTicks::Now() - start_time) * | |
| 80 base::Time::kMicrosecondsPerMillisecond); | |
| 77 } | 81 } |
| 82 | |
| 83 void PluginLoaderPosix::LoadPluginsInternal() { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 85 | |
| 86 if (load_start_time_.is_null()) | |
| 87 load_start_time_ = base::TimeTicks::Now(); | |
| 88 | |
| 89 process_host_ = new UtilityProcessHost(this, BrowserThread::IO); | |
| 90 process_host_->set_no_sandbox(true); | |
| 91 #if defined(OS_MACOSX) | |
| 92 process_host_->set_child_flags(ChildProcessHost::CHILD_ALLOW_HEAP_EXECUTION); | |
| 93 #endif | |
| 94 | |
| 95 process_host_->Send(new UtilityMsg_LoadPlugins(canonical_list_)); | |
| 96 } | |
| 97 | |
| 98 void PluginLoaderPosix::OnPluginLoaded(size_t index, | |
| 99 const webkit::WebPluginInfo& plugin) { | |
| 100 if (index >= canonical_list_.size()) | |
|
Bernhard Bauer
2011/10/20 08:20:04
When can that happen?
Robert Sesek
2011/10/20 16:00:21
It's just being defensive against malformed/malici
Bernhard Bauer
2011/10/20 16:15:08
Shouldn't we be more defensive in that case, i.e.
Robert Sesek
2011/10/20 16:22:36
Is there an API for that? I couldn't find one in C
Bernhard Bauer
2011/10/20 16:27:57
Not sure, but we do it for renderers and extension
Robert Sesek
2011/10/20 16:30:28
Yes, but I think that may be a feature of RenderPr
| |
| 101 return; | |
| 102 | |
| 103 DCHECK_EQ(index, next_load_index_); | |
| 104 DCHECK_EQ(plugin.path.value(), canonical_list_[index].value()); | |
| 105 | |
| 106 if (!MaybeAddInternalPlugin(canonical_list_[index])) | |
| 107 loaded_plugins_.push_back(plugin); | |
| 108 | |
| 109 ++next_load_index_; | |
|
Bernhard Bauer
2011/10/20 08:20:04
Maybe I'm just not understanding it, but why do we
Robert Sesek
2011/10/20 16:00:21
I thought about this but was hesitant because it w
Bernhard Bauer
2011/10/20 16:15:08
And if you use a linked list?
Robert Sesek
2011/10/20 16:22:36
I don't think we have param traits, and even if we
Bernhard Bauer
2011/10/20 16:27:57
Hm :-/
What if we just remove the index from the
Robert Sesek
2011/10/20 16:30:28
What's wrong with sending the index?
Robert Sesek
2011/10/20 16:38:05
Actually, I now see that I don't need to do it bec
| |
| 110 | |
| 111 RunPendingCallbacks(); | |
| 112 } | |
| 113 | |
| 114 void PluginLoaderPosix::OnPluginLoadFailed(size_t index, | |
| 115 const FilePath& plugin_path) { | |
| 116 if (index >= canonical_list_.size()) | |
| 117 return; | |
| 118 | |
| 119 DCHECK_EQ(index, next_load_index_); | |
| 120 DCHECK_EQ(plugin_path.value(), canonical_list_[index].value()); | |
| 121 ++next_load_index_; | |
| 122 | |
| 123 MaybeAddInternalPlugin(plugin_path); | |
| 124 RunPendingCallbacks(); | |
| 125 } | |
| 126 | |
| 127 bool PluginLoaderPosix::MaybeAddInternalPlugin(const FilePath& plugin_path) { | |
| 128 for (std::vector<webkit::WebPluginInfo>::iterator it = | |
| 129 internal_plugins_.begin(); | |
|
Bernhard Bauer
2011/10/20 08:20:04
Nit: one more space
Robert Sesek
2011/10/20 16:00:21
Done.
| |
| 130 it != internal_plugins_.end(); | |
| 131 ++it) { | |
| 132 if (it->path == plugin_path) { | |
| 133 loaded_plugins_.push_back(*it); | |
| 134 internal_plugins_.erase(it); | |
| 135 return true; | |
| 136 } | |
| 137 } | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 void PluginLoaderPosix::RunPendingCallbacks() { | |
| 142 if (next_load_index_ >= canonical_list_.size()) { | |
|
jam
2011/10/20 00:16:16
nit: better to early return instead to reduce tabb
Robert Sesek
2011/10/20 16:00:21
Done.
| |
| 143 PluginList::Singleton()->SetPlugins(loaded_plugins_); | |
| 144 for (std::vector<PendingCallback>::iterator it = callbacks_.begin(); | |
| 145 it != callbacks_.end(); | |
| 146 ++it) { | |
| 147 it->target_loop->PostTask(FROM_HERE, | |
| 148 base::Bind(it->callback, loaded_plugins_)); | |
| 149 } | |
| 150 callbacks_.clear(); | |
| 151 | |
| 152 HISTOGRAM_TIMES("PluginLoaderPosix.LoadDone", | |
| 153 (base::TimeTicks::Now() - load_start_time_) | |
| 154 * base::Time::kMicrosecondsPerMillisecond); | |
| 155 load_start_time_ = base::TimeTicks(); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 PluginLoaderPosix::PendingCallback::PendingCallback( | |
| 160 scoped_refptr<base::MessageLoopProxy> loop, | |
| 161 const PluginService::GetPluginsCallback& cb) | |
| 162 : target_loop(loop), | |
| 163 callback(cb) { | |
| 164 } | |
| 165 | |
| 166 PluginLoaderPosix::PendingCallback::~PendingCallback() { | |
| 167 } | |
| OLD | NEW |