pepper_flash_browser_host.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam// Use of this source code is governed by a BSD-style license that can be
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam// found in the LICENSE file.
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include "chrome/browser/renderer_host/pepper/pepper_flash_browser_host.h"
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include "base/time/time.h"
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "chrome/browser/content_settings/cookie_settings.h"
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom#include "chrome/browser/profiles/profile.h"
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "content/public/browser/browser_context.h"
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "content/public/browser/browser_ppapi_host.h"
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "content/public/browser/browser_thread.h"
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "content/public/browser/render_process_host.h"
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include "ipc/ipc_message_macros.h"
15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include "ppapi/c/pp_errors.h"
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "ppapi/c/private/ppb_flash.h"
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "ppapi/host/dispatch_host_message.h"
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "ppapi/proxy/ppapi_messages.h"
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "ppapi/proxy/resource_message_params.h"
204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom#include "ppapi/shared_impl/time_conversion.h"
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#include "url/gurl.h"
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#if defined(OS_WIN)
24c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include <windows.h>
25c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#elif defined(OS_MACOSX)
26c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#include <CoreServices/CoreServices.h>
27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom#endif
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamusing content::BrowserPpapiHost;
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamusing content::BrowserThread;
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamusing content::RenderProcessHost;
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamnamespace chrome {
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamnamespace {
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam// Get the CookieSettings on the UI thread for the given render process ID.
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamscoped_refptr<CookieSettings> GetCookieSettings(int render_process_id) {
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
404c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  RenderProcessHost* render_process_host = RenderProcessHost::FromID(
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam      render_process_id);
424c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  if (render_process_host && render_process_host->GetBrowserContext()) {
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    Profile* profile =
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Profile::FromBrowserContext(render_process_host->GetBrowserContext());
454c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    return CookieSettings::Factory::GetForProfile(profile);
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  }
47c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  return NULL;
48c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom}
49c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}  // namespace
51c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
52c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian CarlstromPepperFlashBrowserHost::PepperFlashBrowserHost(
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    BrowserPpapiHost* host,
54c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    PP_Instance instance,
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    PP_Resource resource)
56c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    : ResourceHost(host->GetPpapiHost(), instance, resource),
57c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom      host_(host),
5870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom      weak_factory_(this) {
59c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  int unused;
60c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  host->GetRenderFrameIDsForInstance(instance, &render_process_id_, &unused);
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
634c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian CarlstromPepperFlashBrowserHost::~PepperFlashBrowserHost() {
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom}
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromint32_t PepperFlashBrowserHost::OnResourceMessageReceived(
67c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    const IPC::Message& msg,
6870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    ppapi::host::HostMessageContext* context) {
69c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  IPC_BEGIN_MESSAGE_MAP(PepperFlashBrowserHost, msg)
70c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Flash_UpdateActivity,
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                                        OnUpdateActivity);
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetLocalTimeZoneOffset,
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                                      OnGetLocalTimeZoneOffset);
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        PpapiHostMsg_Flash_GetLocalDataRestrictions,
76c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        OnGetLocalDataRestrictions);
77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  IPC_END_MESSAGE_MAP()
78c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom  return PP_ERROR_FAILED;
795db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root}
805db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root
815db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Rootint32_t PepperFlashBrowserHost::OnUpdateActivity(
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    ppapi::host::HostMessageContext* host_context) {
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam#if defined(OS_WIN)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  // Reading then writing back the same value to the screensaver timeout system
856e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom  // setting resets the countdown which prevents the screensaver from turning
866e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom  // on "for a while". As long as the plugin pings us with this message faster
8770c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  // than the screensaver timeout, it won't go on.
8870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  int value = 0;
8970c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  if (SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &value, 0))
9070c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, value, NULL, 0);
9170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom#elif defined(OS_MACOSX)
9270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  UpdateSystemActivity(OverallAct);
9370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom#else
9470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  // TODO(brettw) implement this for other platforms.
9570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom#endif
9670c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  return PP_OK;
9770c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom}
9870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
9970c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstromint32_t PepperFlashBrowserHost::OnGetLocalTimeZoneOffset(
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    ppapi::host::HostMessageContext* host_context,
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    const base::Time& t) {
1024c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  // The reason for this processing being in the browser process is that on
10370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom  // Linux, the localtime calls require filesystem access prohibited by the
1044c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  // sandbox.
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  host_context->reply_msg = PpapiPluginMsg_Flash_GetLocalTimeZoneOffsetReply(
1064c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom      ppapi::PPGetLocalTimeZoneOffset(t));
1074c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  return PP_OK;
1084c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom}
1094c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamint32_t PepperFlashBrowserHost::OnGetLocalDataRestrictions(
1114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ppapi::host::HostMessageContext* context) {
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  // Getting the Flash LSO settings requires using the CookieSettings which
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  // belong to the profile which lives on the UI thread. We lazily initialize
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  // |cookie_settings_| by grabbing the reference from the UI thread and then
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  // call |GetLocalDataRestrictions| with it.
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  GURL document_url = host_->GetDocumentURLForInstance(pp_instance());
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  GURL plugin_url = host_->GetPluginURLForInstance(pp_instance());
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  if (cookie_settings_.get()) {
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    GetLocalDataRestrictions(context->MakeReplyMessageContext(), document_url,
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                             plugin_url, cookie_settings_);
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  } else {
1224c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE,
1234c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        base::Bind(&GetCookieSettings, render_process_id_),
124c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom        base::Bind(&PepperFlashBrowserHost::GetLocalDataRestrictions,
125c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom                   weak_factory_.GetWeakPtr(),
126c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom                   context->MakeReplyMessageContext(),
127c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom                   document_url, plugin_url));
128c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  }
1294c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  return PP_OK_COMPLETIONPENDING;
130c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom}
131c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom
1324c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromvoid PepperFlashBrowserHost::GetLocalDataRestrictions(
1334c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ppapi::host::ReplyMessageContext reply_context,
1344c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    const GURL& document_url,
135c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom    const GURL& plugin_url,
136c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom    scoped_refptr<CookieSettings> cookie_settings) {
137c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1384c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
1394c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  // Lazily initialize |cookie_settings_|. The cookie settings are thread-safe
140c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  // ref-counted so as long as we hold a reference to them we can safely access
141c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  // them on the IO thread.
142c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  if (!cookie_settings_.get()) {
1434c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    cookie_settings_ = cookie_settings;
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  } else {
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DCHECK(cookie_settings_.get() == cookie_settings.get());
146c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  }
147c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom
148c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom  PP_FlashLSORestrictions restrictions = PP_FLASHLSORESTRICTIONS_NONE;
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam  if (cookie_settings_.get() && document_url.is_valid() &&
1504c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom      plugin_url.is_valid()) {
151c4fa740cf84a54fceb87964ca8ea666fd41b5b8fBrian Carlstrom    if (!cookie_settings_->IsReadingCookieAllowed(document_url, plugin_url))
1524c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom      restrictions = PP_FLASHLSORESTRICTIONS_BLOCK;
153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    else if (cookie_settings_->IsCookieSessionOnly(plugin_url))
1544c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom      restrictions = PP_FLASHLSORESTRICTIONS_IN_MEMORY;
1554c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  }
1564c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom  SendReply(reply_context, PpapiPluginMsg_Flash_GetLocalDataRestrictionsReply(
157c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom      static_cast<int32_t>(restrictions)));
158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom}
159c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
1604c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom}  // namespace chrome
1614c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom