12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/renderer_host/pepper/pepper_flash_browser_host.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/content_settings/cookie_settings.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_context.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ipc/ipc_message_macros.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/c/pp_errors.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/c/private/ppb_flash.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/proxy/resource_message_params.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/shared_impl/time_conversion.h"
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <windows.h>
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#elif defined(OS_MACOSX)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <CoreServices/CoreServices.h>
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::BrowserPpapiHost;
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::BrowserThread;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using content::RenderProcessHost;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace chrome {
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get the CookieSettings on the UI thread for the given render process ID.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_refptr<CookieSettings> GetCookieSettings(int render_process_id) {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
40a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RenderProcessHost* render_process_host =
41a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      RenderProcessHost::FromID(render_process_id);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (render_process_host && render_process_host->GetBrowserContext()) {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile =
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        Profile::FromBrowserContext(render_process_host->GetBrowserContext());
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return CookieSettings::Factory::GetForProfile(profile);
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperFlashBrowserHost::PepperFlashBrowserHost(BrowserPpapiHost* host,
53a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                               PP_Instance instance,
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                               PP_Resource resource)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : ResourceHost(host->GetPpapiHost(), instance, resource),
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      host_(host),
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_factory_(this) {
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int unused;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  host->GetRenderFrameIDsForInstance(instance, &render_process_id_, &unused);
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
62a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperFlashBrowserHost::~PepperFlashBrowserHost() {}
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t PepperFlashBrowserHost::OnResourceMessageReceived(
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const IPC::Message& msg,
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_BEGIN_MESSAGE_MAP(PepperFlashBrowserHost, msg)
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Flash_UpdateActivity,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        OnUpdateActivity)
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetLocalTimeZoneOffset,
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      OnGetLocalTimeZoneOffset)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        PpapiHostMsg_Flash_GetLocalDataRestrictions, OnGetLocalDataRestrictions)
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_END_MESSAGE_MAP()
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_ERROR_FAILED;
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t PepperFlashBrowserHost::OnUpdateActivity(
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* host_context) {
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN)
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Reading then writing back the same value to the screensaver timeout system
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // setting resets the countdown which prevents the screensaver from turning
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // on "for a while". As long as the plugin pings us with this message faster
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // than the screensaver timeout, it won't go on.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int value = 0;
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &value, 0))
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, value, NULL, 0);
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#elif defined(OS_MACOSX)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  UpdateSystemActivity(OverallAct);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// TODO(brettw) implement this for other platforms.
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_OK;
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t PepperFlashBrowserHost::OnGetLocalTimeZoneOffset(
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* host_context,
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::Time& t) {
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The reason for this processing being in the browser process is that on
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Linux, the localtime calls require filesystem access prohibited by the
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // sandbox.
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  host_context->reply_msg = PpapiPluginMsg_Flash_GetLocalTimeZoneOffsetReply(
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ppapi::PPGetLocalTimeZoneOffset(t));
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_OK;
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t PepperFlashBrowserHost::OnGetLocalDataRestrictions(
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Getting the Flash LSO settings requires using the CookieSettings which
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // belong to the profile which lives on the UI thread. We lazily initialize
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |cookie_settings_| by grabbing the reference from the UI thread and then
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // call |GetLocalDataRestrictions| with it.
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL document_url = host_->GetDocumentURLForInstance(pp_instance());
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GURL plugin_url = host_->GetPluginURLForInstance(pp_instance());
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (cookie_settings_.get()) {
116a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    GetLocalDataRestrictions(context->MakeReplyMessageContext(),
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                             document_url,
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                             plugin_url,
119a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                             cookie_settings_);
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
121a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    BrowserThread::PostTaskAndReplyWithResult(
122a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        BrowserThread::UI,
123a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        FROM_HERE,
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(&GetCookieSettings, render_process_id_),
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(&PepperFlashBrowserHost::GetLocalDataRestrictions,
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   weak_factory_.GetWeakPtr(),
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   context->MakeReplyMessageContext(),
128a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   document_url,
129a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   plugin_url));
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_OK_COMPLETIONPENDING;
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PepperFlashBrowserHost::GetLocalDataRestrictions(
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::ReplyMessageContext reply_context,
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& document_url,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& plugin_url,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_refptr<CookieSettings> cookie_settings) {
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Lazily initialize |cookie_settings_|. The cookie settings are thread-safe
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ref-counted so as long as we hold a reference to them we can safely access
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // them on the IO thread.
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!cookie_settings_.get()) {
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    cookie_settings_ = cookie_settings;
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(cookie_settings_.get() == cookie_settings.get());
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PP_FlashLSORestrictions restrictions = PP_FLASHLSORESTRICTIONS_NONE;
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (cookie_settings_.get() && document_url.is_valid() &&
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      plugin_url.is_valid()) {
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!cookie_settings_->IsReadingCookieAllowed(document_url, plugin_url))
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      restrictions = PP_FLASHLSORESTRICTIONS_BLOCK;
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    else if (cookie_settings_->IsCookieSessionOnly(plugin_url))
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      restrictions = PP_FLASHLSORESTRICTIONS_IN_MEMORY;
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
158a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  SendReply(reply_context,
159a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch            PpapiPluginMsg_Flash_GetLocalDataRestrictionsReply(
160a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                static_cast<int32_t>(restrictions)));
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace chrome
164