| 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 "chrome/browser/plugin_data_remover.h" | 5 #include "content/browser/plugin_data_remover_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop_proxy.h" | |
| 10 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 11 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/version.h" | 10 #include "base/version.h" |
| 13 #include "chrome/browser/plugin_prefs.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/chrome_switches.h" | |
| 16 #include "content/browser/plugin_service.h" | 11 #include "content/browser/plugin_service.h" |
| 17 #include "content/common/plugin_messages.h" | 12 #include "content/common/plugin_messages.h" |
| 18 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 19 #include "webkit/plugins/npapi/plugin_group.h" | 14 #include "webkit/plugins/npapi/plugin_group.h" |
| 20 | 15 |
| 21 #if defined(OS_POSIX) | |
| 22 #include "ipc/ipc_channel_posix.h" | |
| 23 #endif | |
| 24 | |
| 25 using content::BrowserThread; | 16 using content::BrowserThread; |
| 26 | 17 |
| 27 namespace { | 18 namespace { |
| 28 | 19 |
| 29 const char kFlashMimeType[] = "application/x-shockwave-flash"; | 20 const char kFlashMimeType[] = "application/x-shockwave-flash"; |
| 30 // The minimum Flash Player version that implements NPP_ClearSiteData. | 21 // The minimum Flash Player version that implements NPP_ClearSiteData. |
| 31 const char kMinFlashVersion[] = "10.3"; | 22 const char kMinFlashVersion[] = "10.3"; |
| 32 const int64 kRemovalTimeoutMs = 10000; | 23 const int64 kRemovalTimeoutMs = 10000; |
| 33 const uint64 kClearAllData = 0; | 24 const uint64 kClearAllData = 0; |
| 34 | 25 |
| 35 } // namespace | 26 } // namespace |
| 36 | 27 |
| 37 PluginDataRemover::PluginDataRemover(Profile* profile) | 28 namespace content { |
| 38 : mime_type_(kFlashMimeType), | 29 |
| 39 is_removing_(false), | 30 // static |
| 40 context_(profile->GetResourceContext()), | 31 PluginDataRemover* PluginDataRemover::Create( |
| 41 event_(new base::WaitableEvent(true, false)), | 32 const content::ResourceContext& resource_context) { |
| 42 channel_(NULL) { | 33 return new PluginDataRemoverImpl(resource_context); |
| 43 } | 34 } |
| 44 | 35 |
| 45 PluginDataRemover::~PluginDataRemover() { | 36 // static |
| 37 bool PluginDataRemover::IsSupported(webkit::WebPluginInfo* plugin) { |
| 38 bool allow_wildcard = false; |
| 39 std::vector<webkit::WebPluginInfo> plugins; |
| 40 PluginService::GetInstance()->GetPluginInfoArray( |
| 41 GURL(), kFlashMimeType, allow_wildcard, &plugins, NULL); |
| 42 std::vector<webkit::WebPluginInfo>::iterator plugin_it = plugins.begin(); |
| 43 if (plugin_it == plugins.end()) |
| 44 return false; |
| 45 scoped_ptr<Version> version( |
| 46 webkit::npapi::PluginGroup::CreateVersionFromString(plugin_it->version)); |
| 47 scoped_ptr<Version> min_version( |
| 48 Version::GetVersionFromString(kMinFlashVersion)); |
| 49 bool rv = version.get() && min_version->CompareTo(*version) == -1; |
| 50 if (rv) |
| 51 *plugin = *plugin_it; |
| 52 return rv; |
| 53 } |
| 54 |
| 55 } |
| 56 |
| 57 PluginDataRemoverImpl::PluginDataRemoverImpl( |
| 58 const content::ResourceContext& resource_context) |
| 59 : mime_type_(kFlashMimeType), |
| 60 is_starting_process_(false), |
| 61 is_removing_(false), |
| 62 context_(resource_context), |
| 63 event_(new base::WaitableEvent(true, false)), |
| 64 channel_(NULL), |
| 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 66 } |
| 67 |
| 68 PluginDataRemoverImpl::~PluginDataRemoverImpl() { |
| 69 if (is_starting_process_) |
| 70 PluginService::GetInstance()->CancelOpenChannelToNpapiPlugin(this); |
| 46 DCHECK(!is_removing_); | 71 DCHECK(!is_removing_); |
| 47 if (channel_) | 72 if (channel_) |
| 48 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); | 73 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); |
| 49 } | 74 } |
| 50 | 75 |
| 51 base::WaitableEvent* PluginDataRemover::StartRemoving(base::Time begin_time) { | 76 base::WaitableEvent* PluginDataRemoverImpl::StartRemoving( |
| 77 base::Time begin_time) { |
| 52 DCHECK(!is_removing_); | 78 DCHECK(!is_removing_); |
| 53 remove_start_time_ = base::Time::Now(); | 79 remove_start_time_ = base::Time::Now(); |
| 54 begin_time_ = begin_time; | 80 begin_time_ = begin_time; |
| 55 | 81 |
| 82 is_starting_process_ = true; |
| 56 is_removing_ = true; | 83 is_removing_ = true; |
| 57 | |
| 58 // Balanced in OnChannelOpened or OnError. Exactly one them will eventually be | |
| 59 // called, so we need to keep this object around until then. | |
| 60 AddRef(); | |
| 61 PluginService::GetInstance()->OpenChannelToNpapiPlugin( | 84 PluginService::GetInstance()->OpenChannelToNpapiPlugin( |
| 62 0, 0, GURL(), GURL(), mime_type_, this); | 85 0, 0, GURL(), GURL(), mime_type_, this); |
| 63 | 86 |
| 64 BrowserThread::PostDelayedTask( | 87 BrowserThread::PostDelayedTask( |
| 65 BrowserThread::IO, | 88 BrowserThread::IO, |
| 66 FROM_HERE, | 89 FROM_HERE, |
| 67 base::Bind(&PluginDataRemover::OnTimeout, this), | 90 base::Bind(&PluginDataRemoverImpl::OnTimeout, weak_factory_.GetWeakPtr()), |
| 68 kRemovalTimeoutMs); | 91 kRemovalTimeoutMs); |
| 69 | 92 |
| 70 return event_.get(); | 93 return event_.get(); |
| 71 } | 94 } |
| 72 | 95 |
| 73 void PluginDataRemover::Wait() { | 96 int PluginDataRemoverImpl::ID() { |
| 74 base::Time start_time(base::Time::Now()); | |
| 75 if (is_removing_) | |
| 76 event_->Wait(); | |
| 77 UMA_HISTOGRAM_TIMES("ClearPluginData.wait_at_shutdown", | |
| 78 base::Time::Now() - start_time); | |
| 79 UMA_HISTOGRAM_TIMES("ClearPluginData.time_at_shutdown", | |
| 80 base::Time::Now() - remove_start_time_); | |
| 81 } | |
| 82 | |
| 83 int PluginDataRemover::ID() { | |
| 84 // Generate a unique identifier for this PluginProcessHostClient. | 97 // Generate a unique identifier for this PluginProcessHostClient. |
| 85 return ChildProcessInfo::GenerateChildProcessUniqueId(); | 98 return ChildProcessInfo::GenerateChildProcessUniqueId(); |
| 86 } | 99 } |
| 87 | 100 |
| 88 bool PluginDataRemover::OffTheRecord() { | 101 bool PluginDataRemoverImpl::OffTheRecord() { |
| 89 return false; | 102 return false; |
| 90 } | 103 } |
| 91 | 104 |
| 92 const content::ResourceContext& PluginDataRemover::GetResourceContext() { | 105 const content::ResourceContext& PluginDataRemoverImpl::GetResourceContext() { |
| 93 return context_; | 106 return context_; |
| 94 } | 107 } |
| 95 | 108 |
| 96 void PluginDataRemover::SetPluginInfo( | 109 void PluginDataRemoverImpl::SetPluginInfo( |
| 97 const webkit::WebPluginInfo& info) { | 110 const webkit::WebPluginInfo& info) { |
| 98 } | 111 } |
| 99 | 112 |
| 100 void PluginDataRemover::OnFoundPluginProcessHost( | 113 void PluginDataRemoverImpl::OnFoundPluginProcessHost( |
| 101 PluginProcessHost* host) { | 114 PluginProcessHost* host) { |
| 102 } | 115 } |
| 103 | 116 |
| 104 void PluginDataRemover::OnSentPluginChannelRequest() { | 117 void PluginDataRemoverImpl::OnSentPluginChannelRequest() { |
| 105 } | 118 } |
| 106 | 119 |
| 107 void PluginDataRemover::OnChannelOpened(const IPC::ChannelHandle& handle) { | 120 void PluginDataRemoverImpl::OnChannelOpened(const IPC::ChannelHandle& handle) { |
| 121 is_starting_process_ = false; |
| 108 ConnectToChannel(handle); | 122 ConnectToChannel(handle); |
| 109 // Balancing the AddRef call in StartRemoving. | |
| 110 Release(); | |
| 111 } | 123 } |
| 112 | 124 |
| 113 void PluginDataRemover::ConnectToChannel(const IPC::ChannelHandle& handle) { | 125 void PluginDataRemoverImpl::ConnectToChannel(const IPC::ChannelHandle& handle) { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 115 | 127 |
| 116 // If we timed out, don't bother connecting. | 128 // If we timed out, don't bother connecting. |
| 117 if (!is_removing_) | 129 if (!is_removing_) |
| 118 return; | 130 return; |
| 119 | 131 |
| 120 DCHECK(!channel_); | 132 DCHECK(!channel_); |
| 121 channel_ = new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this); | 133 channel_ = new IPC::Channel(handle, IPC::Channel::MODE_CLIENT, this); |
| 122 if (!channel_->Connect()) { | 134 if (!channel_->Connect()) { |
| 123 NOTREACHED() << "Couldn't connect to plugin"; | 135 NOTREACHED() << "Couldn't connect to plugin"; |
| 124 SignalDone(); | 136 SignalDone(); |
| 125 return; | 137 return; |
| 126 } | 138 } |
| 127 | 139 |
| 128 if (!channel_->Send(new PluginMsg_ClearSiteData(std::string(), | 140 if (!channel_->Send(new PluginMsg_ClearSiteData(std::string(), |
| 129 kClearAllData, | 141 kClearAllData, |
| 130 begin_time_))) { | 142 begin_time_))) { |
| 131 NOTREACHED() << "Couldn't send ClearSiteData message"; | 143 NOTREACHED() << "Couldn't send ClearSiteData message"; |
| 132 SignalDone(); | 144 SignalDone(); |
| 133 return; | 145 return; |
| 134 } | 146 } |
| 135 } | 147 } |
| 136 | 148 |
| 137 void PluginDataRemover::OnError() { | 149 void PluginDataRemoverImpl::OnError() { |
| 138 LOG(DFATAL) << "Couldn't open plugin channel"; | 150 LOG(DFATAL) << "Couldn't open plugin channel"; |
| 139 SignalDone(); | 151 SignalDone(); |
| 140 // Balancing the AddRef call in StartRemoving. | |
| 141 Release(); | |
| 142 } | 152 } |
| 143 | 153 |
| 144 void PluginDataRemover::OnClearSiteDataResult(bool success) { | 154 void PluginDataRemoverImpl::OnClearSiteDataResult(bool success) { |
| 145 LOG_IF(ERROR, !success) << "ClearSiteData returned error"; | 155 LOG_IF(ERROR, !success) << "ClearSiteData returned error"; |
| 146 UMA_HISTOGRAM_TIMES("ClearPluginData.time", | 156 UMA_HISTOGRAM_TIMES("ClearPluginData.time", |
| 147 base::Time::Now() - remove_start_time_); | 157 base::Time::Now() - remove_start_time_); |
| 148 SignalDone(); | 158 SignalDone(); |
| 149 } | 159 } |
| 150 | 160 |
| 151 void PluginDataRemover::OnTimeout() { | 161 void PluginDataRemoverImpl::OnTimeout() { |
| 152 LOG_IF(ERROR, is_removing_) << "Timed out"; | 162 LOG_IF(ERROR, is_removing_) << "Timed out"; |
| 153 SignalDone(); | 163 SignalDone(); |
| 154 } | 164 } |
| 155 | 165 |
| 156 bool PluginDataRemover::OnMessageReceived(const IPC::Message& msg) { | 166 bool PluginDataRemoverImpl::OnMessageReceived(const IPC::Message& msg) { |
| 157 IPC_BEGIN_MESSAGE_MAP(PluginDataRemover, msg) | 167 IPC_BEGIN_MESSAGE_MAP(PluginDataRemoverImpl, msg) |
| 158 IPC_MESSAGE_HANDLER(PluginHostMsg_ClearSiteDataResult, | 168 IPC_MESSAGE_HANDLER(PluginHostMsg_ClearSiteDataResult, |
| 159 OnClearSiteDataResult) | 169 OnClearSiteDataResult) |
| 160 IPC_MESSAGE_UNHANDLED_ERROR() | 170 IPC_MESSAGE_UNHANDLED_ERROR() |
| 161 IPC_END_MESSAGE_MAP() | 171 IPC_END_MESSAGE_MAP() |
| 162 | 172 |
| 163 return true; | 173 return true; |
| 164 } | 174 } |
| 165 | 175 |
| 166 void PluginDataRemover::OnChannelError() { | 176 void PluginDataRemoverImpl::OnChannelError() { |
| 177 is_starting_process_ = false; |
| 167 if (is_removing_) { | 178 if (is_removing_) { |
| 168 NOTREACHED() << "Channel error"; | 179 NOTREACHED() << "Channel error"; |
| 169 SignalDone(); | 180 SignalDone(); |
| 170 } | 181 } |
| 171 } | 182 } |
| 172 | 183 |
| 173 void PluginDataRemover::SignalDone() { | 184 void PluginDataRemoverImpl::SignalDone() { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 175 if (!is_removing_) | 186 if (!is_removing_) |
| 176 return; | 187 return; |
| 177 is_removing_ = false; | 188 is_removing_ = false; |
| 178 event_->Signal(); | 189 event_->Signal(); |
| 179 } | 190 } |
| 180 | |
| 181 // static | |
| 182 bool PluginDataRemover::IsSupported(PluginPrefs* plugin_prefs) { | |
| 183 bool allow_wildcard = false; | |
| 184 std::vector<webkit::WebPluginInfo> plugins; | |
| 185 PluginService::GetInstance()->GetPluginInfoArray( | |
| 186 GURL(), kFlashMimeType, allow_wildcard, &plugins, NULL); | |
| 187 std::vector<webkit::WebPluginInfo>::iterator plugin = plugins.begin(); | |
| 188 if (plugin == plugins.end()) | |
| 189 return false; | |
| 190 scoped_ptr<Version> version( | |
| 191 webkit::npapi::PluginGroup::CreateVersionFromString(plugin->version)); | |
| 192 scoped_ptr<Version> min_version(Version::GetVersionFromString( | |
| 193 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 194 switches::kMinClearSiteDataFlashVersion))); | |
| 195 if (!min_version.get()) | |
| 196 min_version.reset(Version::GetVersionFromString(kMinFlashVersion)); | |
| 197 return plugin_prefs->IsPluginEnabled(*plugin) && | |
| 198 version.get() && | |
| 199 min_version->CompareTo(*version) == -1; | |
| 200 } | |
| OLD | NEW |