Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / plugin_data_remover_impl.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/plugin_data_remover_impl.h"
6
7 #include <limits>
8
9 #include "base/bind.h"
10 #include "base/metrics/histogram.h"
11 #include "base/sequenced_task_runner_helpers.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/version.h"
15 #include "content/browser/plugin_process_host.h"
16 #include "content/browser/plugin_service_impl.h"
17 #include "content/browser/renderer_host/pepper/pepper_flash_file_message_filter.h"
18 #include "content/common/child_process_host_impl.h"
19 #include "content/common/plugin_process_messages.h"
20 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/common/content_constants.h"
23 #include "content/public/common/pepper_plugin_info.h"
24 #include "ppapi/proxy/ppapi_messages.h"
25
26 namespace content {
27
28 namespace {
29
30 // The minimum Flash Player version that implements NPP_ClearSiteData.
31 const char kMinFlashVersion[] = "10.3";
32 const int64 kRemovalTimeoutMs = 10000;
33 const uint64 kClearAllData = 0;
34
35 }  // namespace
36
37 // static
38 PluginDataRemover* PluginDataRemover::Create(BrowserContext* browser_context) {
39   return new PluginDataRemoverImpl(browser_context);
40 }
41
42 // static
43 void PluginDataRemover::GetSupportedPlugins(
44     std::vector<WebPluginInfo>* supported_plugins) {
45   bool allow_wildcard = false;
46   std::vector<WebPluginInfo> plugins;
47   PluginService::GetInstance()->GetPluginInfoArray(
48       GURL(), kFlashPluginSwfMimeType, allow_wildcard, &plugins, NULL);
49   Version min_version(kMinFlashVersion);
50   for (std::vector<WebPluginInfo>::iterator it = plugins.begin();
51        it != plugins.end(); ++it) {
52     Version version;
53     WebPluginInfo::CreateVersionFromString(it->version, &version);
54     if (version.IsValid() && min_version.CompareTo(version) == -1)
55       supported_plugins->push_back(*it);
56   }
57 }
58
59 class PluginDataRemoverImpl::Context
60     : public PluginProcessHost::Client,
61       public PpapiPluginProcessHost::BrokerClient,
62       public IPC::Listener,
63       public base::RefCountedThreadSafe<Context,
64                                         BrowserThread::DeleteOnIOThread> {
65  public:
66   Context(base::Time begin_time, BrowserContext* browser_context)
67       : event_(new base::WaitableEvent(true, false)),
68         begin_time_(begin_time),
69         is_removing_(false),
70         browser_context_path_(browser_context->GetPath()),
71         resource_context_(browser_context->GetResourceContext()) {
72     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73   }
74
75   void Init(const std::string& mime_type) {
76     BrowserThread::PostTask(
77         BrowserThread::IO,
78         FROM_HERE,
79         base::Bind(&Context::InitOnIOThread, this, mime_type));
80     BrowserThread::PostDelayedTask(
81         BrowserThread::IO,
82         FROM_HERE,
83         base::Bind(&Context::OnTimeout, this),
84         base::TimeDelta::FromMilliseconds(kRemovalTimeoutMs));
85   }
86
87   void InitOnIOThread(const std::string& mime_type) {
88     PluginServiceImpl* plugin_service = PluginServiceImpl::GetInstance();
89
90     // Get the plugin file path.
91     std::vector<WebPluginInfo> plugins;
92     plugin_service->GetPluginInfoArray(
93         GURL(), mime_type, false, &plugins, NULL);
94     base::FilePath plugin_path;
95     if (!plugins.empty())  // May be empty for some tests.
96       plugin_path = plugins[0].path;
97
98     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
99     remove_start_time_ = base::Time::Now();
100     is_removing_ = true;
101     // Balanced in On[Ppapi]ChannelOpened or OnError. Exactly one them will
102     // eventually be called, so we need to keep this object around until then.
103     AddRef();
104
105     PepperPluginInfo* pepper_info =
106         plugin_service->GetRegisteredPpapiPluginInfo(plugin_path);
107     if (pepper_info) {
108       plugin_name_ = pepper_info->name;
109       // Use the broker since we run this function outside the sandbox.
110       plugin_service->OpenChannelToPpapiBroker(0, plugin_path, this);
111     } else {
112       plugin_service->OpenChannelToNpapiPlugin(
113           0, 0, GURL(), GURL(), mime_type, this);
114     }
115   }
116
117   // Called when a timeout happens in order not to block the client
118   // indefinitely.
119   void OnTimeout() {
120     LOG_IF(ERROR, is_removing_) << "Timed out";
121     SignalDone();
122   }
123
124   // PluginProcessHost::Client methods.
125   int ID() override {
126     // Generate a unique identifier for this PluginProcessHostClient.
127     return ChildProcessHostImpl::GenerateChildProcessUniqueId();
128   }
129
130   bool OffTheRecord() override { return false; }
131
132   ResourceContext* GetResourceContext() override { return resource_context_; }
133
134   void SetPluginInfo(const WebPluginInfo& info) override {}
135
136   void OnFoundPluginProcessHost(PluginProcessHost* host) override {}
137
138   void OnSentPluginChannelRequest() override {}
139
140   void OnChannelOpened(const IPC::ChannelHandle& handle) override {
141     ConnectToChannel(handle, false);
142     // Balancing the AddRef call.
143     Release();
144   }
145
146   void OnError() override {
147     LOG(ERROR) << "Couldn't open plugin channel";
148     SignalDone();
149     // Balancing the AddRef call.
150     Release();
151   }
152
153   // PpapiPluginProcessHost::BrokerClient implementation.
154   void GetPpapiChannelInfo(base::ProcessHandle* renderer_handle,
155                            int* renderer_id) override {
156     *renderer_handle = base::kNullProcessHandle;
157     *renderer_id = 0;
158   }
159
160   void OnPpapiChannelOpened(const IPC::ChannelHandle& channel_handle,
161                             base::ProcessId /* peer_pid */,
162                             int /* child_id */) override {
163     if (!channel_handle.name.empty())
164       ConnectToChannel(channel_handle, true);
165
166     // Balancing the AddRef call.
167     Release();
168   }
169
170   // IPC::Listener methods.
171   bool OnMessageReceived(const IPC::Message& message) override {
172     IPC_BEGIN_MESSAGE_MAP(Context, message)
173       IPC_MESSAGE_HANDLER(PluginProcessHostMsg_ClearSiteDataResult,
174                           OnClearSiteDataResult)
175       IPC_MESSAGE_HANDLER(PpapiHostMsg_ClearSiteDataResult,
176                           OnPpapiClearSiteDataResult)
177       IPC_MESSAGE_UNHANDLED_ERROR()
178     IPC_END_MESSAGE_MAP()
179
180     return true;
181   }
182
183   void OnChannelError() override {
184     if (is_removing_) {
185       NOTREACHED() << "Channel error";
186       SignalDone();
187     }
188   }
189
190   base::WaitableEvent* event() { return event_.get(); }
191
192  private:
193   friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
194   friend class base::DeleteHelper<Context>;
195   ~Context() override {}
196
197   IPC::Message* CreatePpapiClearSiteDataMsg(uint64 max_age) {
198     base::FilePath profile_path =
199         PepperFlashFileMessageFilter::GetDataDirName(browser_context_path_);
200     // TODO(vtl): This "duplicates" logic in webkit/plugins/ppapi/file_path.cc
201     // (which prepends the plugin name to the relative part of the path
202     // instead, with the absolute, profile-dependent part being enforced by
203     // the browser).
204 #if defined(OS_WIN)
205     base::FilePath plugin_data_path =
206         profile_path.Append(base::FilePath(base::UTF8ToUTF16(plugin_name_)));
207 #else
208     base::FilePath plugin_data_path =
209         profile_path.Append(base::FilePath(plugin_name_));
210 #endif  // defined(OS_WIN)
211     return new PpapiMsg_ClearSiteData(0u, plugin_data_path, std::string(),
212                                       kClearAllData, max_age);
213   }
214
215   // Connects the client side of a newly opened plug-in channel.
216   void ConnectToChannel(const IPC::ChannelHandle& handle, bool is_ppapi) {
217     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
218
219     // If we timed out, don't bother connecting.
220     if (!is_removing_)
221       return;
222
223     DCHECK(!channel_.get());
224     channel_ = IPC::Channel::CreateClient(handle, this);
225     if (!channel_->Connect()) {
226       NOTREACHED() << "Couldn't connect to plugin";
227       SignalDone();
228       return;
229     }
230
231     uint64 max_age = begin_time_.is_null() ?
232         std::numeric_limits<uint64>::max() :
233         (base::Time::Now() - begin_time_).InSeconds();
234
235     IPC::Message* msg;
236     if (is_ppapi) {
237       msg = CreatePpapiClearSiteDataMsg(max_age);
238     } else {
239       msg = new PluginProcessMsg_ClearSiteData(
240           std::string(), kClearAllData, max_age);
241     }
242     if (!channel_->Send(msg)) {
243       NOTREACHED() << "Couldn't send ClearSiteData message";
244       SignalDone();
245       return;
246     }
247   }
248
249   // Handles the PpapiHostMsg_ClearSiteDataResult message by delegating to the
250   // PluginProcessHostMsg_ClearSiteDataResult handler.
251   void OnPpapiClearSiteDataResult(uint32 request_id, bool success) {
252     DCHECK_EQ(0u, request_id);
253     OnClearSiteDataResult(success);
254   }
255
256   // Handles the PluginProcessHostMsg_ClearSiteDataResult message.
257   void OnClearSiteDataResult(bool success) {
258     LOG_IF(ERROR, !success) << "ClearSiteData returned error";
259     UMA_HISTOGRAM_TIMES("ClearPluginData.time",
260                         base::Time::Now() - remove_start_time_);
261     SignalDone();
262   }
263
264   // Signals that we are finished with removing data (successful or not). This
265   // method is safe to call multiple times.
266   void SignalDone() {
267     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
268     if (!is_removing_)
269       return;
270     is_removing_ = false;
271     event_->Signal();
272   }
273
274   scoped_ptr<base::WaitableEvent> event_;
275   // The point in time when we start removing data.
276   base::Time remove_start_time_;
277   // The point in time from which on we remove data.
278   base::Time begin_time_;
279   bool is_removing_;
280
281   // Path for the current profile. Must be retrieved on the UI thread from the
282   // browser context when we start so we can use it later on the I/O thread.
283   base::FilePath browser_context_path_;
284
285   // The resource context for the profile. Use only on the I/O thread.
286   ResourceContext* resource_context_;
287
288   // The name of the plugin. Use only on the I/O thread.
289   std::string plugin_name_;
290
291   // The channel is NULL until we have opened a connection to the plug-in
292   // process.
293   scoped_ptr<IPC::Channel> channel_;
294 };
295
296
297 PluginDataRemoverImpl::PluginDataRemoverImpl(BrowserContext* browser_context)
298     : mime_type_(kFlashPluginSwfMimeType),
299       browser_context_(browser_context) {
300 }
301
302 PluginDataRemoverImpl::~PluginDataRemoverImpl() {
303 }
304
305 base::WaitableEvent* PluginDataRemoverImpl::StartRemoving(
306     base::Time begin_time) {
307   DCHECK(!context_.get());
308   context_ = new Context(begin_time, browser_context_);
309   context_->Init(mime_type_);
310   return context_->event();
311 }
312
313 }  // namespace content