Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/plugin_data_remover.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | |
| 7 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/version.h" | |
| 8 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| 11 #include "chrome/browser/plugin_process_host.h" | |
| 9 #include "chrome/browser/plugin_service.h" | 12 #include "chrome/browser/plugin_service.h" |
| 10 #include "chrome/common/plugin_messages.h" | 13 #include "chrome/common/plugin_messages.h" |
| 11 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
| 15 #include "webkit/glue/plugins/plugin_group.h" | |
| 16 #include "webkit/glue/plugins/plugin_list.h" | |
| 12 | 17 |
| 13 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
| 14 #include "ipc/ipc_channel_posix.h" | 19 #include "ipc/ipc_channel_posix.h" |
| 15 #endif | 20 #endif |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 const std::string flash_mime_type = "application/x-shockwave-flash"; | 23 const char* flash_mime_type = "application/x-shockwave-flash"; |
| 24 // TODO(bauerb): Update minimum required Flash version as soon as there is one | |
| 25 // implementing the API. | |
| 26 const char* min_flash_version = "100"; | |
| 19 const int64 timeout_ms = 10000; | 27 const int64 timeout_ms = 10000; |
|
jam
2010/12/13 22:41:27
can you prefix globals with "g_"
| |
| 20 } | 28 } // namespace |
| 29 | |
| 30 class PluginDataRemover::Internal | |
| 31 : public base::RefCountedThreadSafe<Internal>, | |
| 32 public PluginProcessHost::Client { | |
| 33 public: | |
| 34 explicit Internal(PluginDataRemover* remover) : remover_(remover) { | |
| 35 // We increase our reference count until either OnChannelOpened or OnError | |
| 36 // is called. | |
| 37 AddRef(); | |
| 38 } | |
| 39 ~Internal() { | |
| 40 DCHECK(!remover_); | |
| 41 } | |
| 42 | |
| 43 void Invalidate() { | |
| 44 remover_ = NULL; | |
| 45 } | |
| 46 | |
| 47 virtual int ID() { | |
| 48 // Generate an ID for the browser process. | |
| 49 return ChildProcessInfo::GenerateChildProcessUniqueId(); | |
| 50 } | |
| 51 | |
| 52 virtual bool OffTheRecord() { | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 virtual void SetPluginInfo(const WebPluginInfo& info) { } | |
| 57 | |
| 58 virtual void OnChannelOpened(const IPC::ChannelHandle& handle) { | |
| 59 if (remover_) | |
| 60 remover_->OnChannelOpened(handle); | |
| 61 Release(); | |
| 62 } | |
| 63 | |
| 64 virtual void OnError() { | |
| 65 if (remover_) { | |
| 66 LOG(DFATAL) << "Couldn't open plugin channel"; | |
| 67 remover_->SignalDone(); | |
| 68 } | |
| 69 Release(); | |
| 70 } | |
| 71 | |
| 72 void OnTimeout() { | |
| 73 if (remover_) { | |
| 74 LOG(DFATAL) << "Timed out"; | |
| 75 remover_->SignalDone(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 PluginDataRemover* remover_; | |
| 81 }; | |
| 21 | 82 |
| 22 PluginDataRemover::PluginDataRemover() | 83 PluginDataRemover::PluginDataRemover() |
| 23 : channel_(NULL), | 84 : is_removing_(false), |
| 24 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 85 channel_(NULL) { } |
| 25 } | |
| 26 | 86 |
| 27 PluginDataRemover::~PluginDataRemover() { | 87 PluginDataRemover::~PluginDataRemover() { |
| 28 DCHECK(!done_task_.get()); | 88 DCHECK(!is_removing_); |
| 29 if (!BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_)) | 89 if (channel_) |
| 30 delete channel_; | 90 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, channel_); |
| 31 } | 91 } |
| 32 | 92 |
| 33 void PluginDataRemover::StartRemoving(base::Time begin_time, Task* done_task) { | 93 void PluginDataRemover::StartRemoving(base::Time begin_time, Task* done_task) { |
| 34 DCHECK(!done_task_.get()); | 94 DCHECK(!done_task_.get()); |
| 95 DCHECK(!is_removing_); | |
| 96 remove_start_time_ = base::Time::Now(); | |
| 35 begin_time_ = begin_time; | 97 begin_time_ = begin_time; |
| 36 | 98 |
| 37 message_loop_ = base::MessageLoopProxy::CreateForCurrentThread(); | 99 message_loop_ = base::MessageLoopProxy::CreateForCurrentThread(); |
| 38 done_task_.reset(done_task); | 100 done_task_.reset(done_task); |
| 101 is_removing_ = true; | |
| 39 | 102 |
| 103 internal_ = make_scoped_refptr(new Internal(this)); | |
| 40 PluginService::GetInstance()->OpenChannelToPlugin( | 104 PluginService::GetInstance()->OpenChannelToPlugin( |
| 41 GURL(), flash_mime_type, this); | 105 GURL(), |
| 106 flash_mime_type, | |
| 107 internal_); | |
| 42 | 108 |
| 43 BrowserThread::PostDelayedTask( | 109 BrowserThread::PostDelayedTask( |
| 44 BrowserThread::IO, | 110 BrowserThread::IO, |
| 45 FROM_HERE, | 111 FROM_HERE, |
| 46 method_factory_.NewRunnableMethod(&PluginDataRemover::OnTimeout), | 112 NewRunnableMethod(internal_.get(), &Internal::OnTimeout), |
| 47 timeout_ms); | 113 timeout_ms); |
| 48 } | 114 } |
| 49 | 115 |
| 50 int PluginDataRemover::ID() { | |
| 51 // Generate an ID for the browser process. | |
| 52 return ChildProcessInfo::GenerateChildProcessUniqueId(); | |
| 53 } | |
| 54 | |
| 55 bool PluginDataRemover::OffTheRecord() { | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 void PluginDataRemover::SetPluginInfo(const WebPluginInfo& info) { | |
| 60 } | |
| 61 | |
| 62 void PluginDataRemover::OnChannelOpened(const IPC::ChannelHandle& handle) { | 116 void PluginDataRemover::OnChannelOpened(const IPC::ChannelHandle& handle) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 118 | |
| 64 DCHECK(!channel_); | 119 DCHECK(!channel_); |
| 65 #if defined(OS_POSIX) | 120 #if defined(OS_POSIX) |
| 66 // If we received a ChannelHandle, register it now. | 121 // If we received a ChannelHandle, register it now. |
| 67 if (handle.socket.fd >= 0) | 122 if (handle.socket.fd >= 0) |
| 68 IPC::AddChannelSocket(handle.name, handle.socket.fd); | 123 IPC::AddChannelSocket(handle.name, handle.socket.fd); |
| 69 #endif | 124 #endif |
| 70 channel_ = new IPC::Channel(handle.name, IPC::Channel::MODE_CLIENT, this); | 125 channel_ = new IPC::Channel(handle.name, IPC::Channel::MODE_CLIENT, this); |
| 71 if (!channel_->Connect()) { | 126 if (!channel_->Connect()) { |
| 72 NOTREACHED() << "Couldn't connect to plugin"; | 127 LOG(DFATAL) << "Couldn't connect to plugin"; |
| 73 SignalDone(); | 128 SignalDone(); |
| 74 return; | 129 return; |
| 75 } | 130 } |
| 76 | 131 |
| 77 if (!channel_->Send( | 132 if (!channel_->Send( |
| 78 new PluginMsg_ClearSiteData(0, std::string(), begin_time_))) { | 133 new PluginMsg_ClearSiteData(0, std::string(), begin_time_))) { |
| 79 NOTREACHED() << "Couldn't send ClearSiteData message"; | 134 LOG(DFATAL) << "Couldn't send ClearSiteData message"; |
| 80 SignalDone(); | 135 SignalDone(); |
| 81 } | 136 } |
| 82 } | 137 } |
| 83 | 138 |
| 84 void PluginDataRemover::OnError() { | 139 void PluginDataRemover::OnClearSiteDataResult(bool success) { |
| 85 NOTREACHED() << "Couldn't open plugin channel"; | 140 if (!success) |
| 141 LOG(DFATAL) << "ClearSiteData returned error"; | |
| 142 UMA_HISTOGRAM_TIMES("ClearPluginData.time", | |
| 143 base::Time::Now() - remove_start_time_); | |
| 86 SignalDone(); | 144 SignalDone(); |
| 87 } | 145 } |
| 88 | 146 |
| 89 void PluginDataRemover::OnClearSiteDataResult(bool success) { | |
| 90 DCHECK(success) << "ClearSiteData returned error"; | |
| 91 SignalDone(); | |
| 92 } | |
| 93 | |
| 94 void PluginDataRemover::OnTimeout() { | |
| 95 NOTREACHED() << "Timed out"; | |
| 96 SignalDone(); | |
| 97 } | |
| 98 | |
| 99 void PluginDataRemover::OnMessageReceived(const IPC::Message& msg) { | 147 void PluginDataRemover::OnMessageReceived(const IPC::Message& msg) { |
| 100 IPC_BEGIN_MESSAGE_MAP(PluginDataRemover, msg) | 148 IPC_BEGIN_MESSAGE_MAP(PluginDataRemover, msg) |
| 101 IPC_MESSAGE_HANDLER(PluginHostMsg_ClearSiteDataResult, | 149 IPC_MESSAGE_HANDLER(PluginHostMsg_ClearSiteDataResult, |
| 102 OnClearSiteDataResult) | 150 OnClearSiteDataResult) |
| 103 IPC_MESSAGE_UNHANDLED_ERROR() | 151 IPC_MESSAGE_UNHANDLED_ERROR() |
| 104 IPC_END_MESSAGE_MAP() | 152 IPC_END_MESSAGE_MAP() |
| 105 } | 153 } |
| 106 | 154 |
| 107 void PluginDataRemover::OnChannelError() { | 155 void PluginDataRemover::OnChannelError() { |
| 108 NOTREACHED() << "Channel error"; | 156 LOG(DFATAL) << "Channel error"; |
| 109 SignalDone(); | 157 SignalDone(); |
| 110 } | 158 } |
| 111 | 159 |
| 112 void PluginDataRemover::SignalDone() { | 160 void PluginDataRemover::SignalDone() { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 114 if (!done_task_.get()) | 162 if (!is_removing_) |
| 115 return; | 163 return; |
| 116 message_loop_->PostTask(FROM_HERE, done_task_.release()); | 164 is_removing_ = false; |
| 117 message_loop_ = NULL; | 165 if (done_task_.get()) { |
| 166 message_loop_->PostTask(FROM_HERE, done_task_.release()); | |
| 167 message_loop_ = NULL; | |
| 168 } | |
| 169 internal_->Invalidate(); | |
| 118 } | 170 } |
| 171 | |
| 172 // static | |
| 173 bool PluginDataRemover::IsSupported() { | |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 175 bool allow_wildcard = false; | |
| 176 WebPluginInfo plugin; | |
| 177 std::string mime_type; | |
| 178 if (!NPAPI::PluginList::Singleton()->GetPluginInfo(GURL(), | |
| 179 flash_mime_type, | |
| 180 allow_wildcard, | |
| 181 &plugin, | |
| 182 &mime_type)) | |
| 183 return false; | |
| 184 scoped_ptr<Version> version( | |
| 185 PluginGroup::CreateVersionFromString(plugin.version)); | |
| 186 scoped_ptr<Version> min_version( | |
| 187 Version::GetVersionFromString(min_flash_version)); | |
| 188 return plugin.enabled && | |
| 189 version.get() | |
| 190 && min_version->CompareTo(*version) == -1; | |
| 191 } | |
| OLD | NEW |