ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / renderer_host / pepper / pepper_flash_browser_host.cc
blob7fbf70ab550560cd2d464945c974025d91fb8d47
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.
5 #include "chrome/browser/renderer_host/pepper/pepper_flash_browser_host.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/content_settings/cookie_settings_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "components/content_settings/core/browser/cookie_settings.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_ppapi_host.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "ipc/ipc_message_macros.h"
16 #include "ppapi/c/pp_errors.h"
17 #include "ppapi/c/private/ppb_flash.h"
18 #include "ppapi/host/dispatch_host_message.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "ppapi/proxy/resource_message_params.h"
21 #include "ppapi/shared_impl/time_conversion.h"
22 #include "url/gurl.h"
24 #if defined(OS_WIN)
25 #include <windows.h>
26 #elif defined(OS_MACOSX)
27 #include <CoreServices/CoreServices.h>
28 #endif
30 using content::BrowserPpapiHost;
31 using content::BrowserThread;
32 using content::RenderProcessHost;
34 namespace chrome {
36 namespace {
38 // Get the CookieSettings on the UI thread for the given render process ID.
39 scoped_refptr<content_settings::CookieSettings> GetCookieSettings(
40 int render_process_id) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 RenderProcessHost* render_process_host =
43 RenderProcessHost::FromID(render_process_id);
44 if (render_process_host && render_process_host->GetBrowserContext()) {
45 Profile* profile =
46 Profile::FromBrowserContext(render_process_host->GetBrowserContext());
47 return CookieSettingsFactory::GetForProfile(profile);
49 return NULL;
52 } // namespace
54 PepperFlashBrowserHost::PepperFlashBrowserHost(BrowserPpapiHost* host,
55 PP_Instance instance,
56 PP_Resource resource)
57 : ResourceHost(host->GetPpapiHost(), instance, resource),
58 host_(host),
59 weak_factory_(this) {
60 int unused;
61 host->GetRenderFrameIDsForInstance(instance, &render_process_id_, &unused);
64 PepperFlashBrowserHost::~PepperFlashBrowserHost() {}
66 int32_t PepperFlashBrowserHost::OnResourceMessageReceived(
67 const IPC::Message& msg,
68 ppapi::host::HostMessageContext* context) {
69 PPAPI_BEGIN_MESSAGE_MAP(PepperFlashBrowserHost, msg)
70 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Flash_UpdateActivity,
71 OnUpdateActivity)
72 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetLocalTimeZoneOffset,
73 OnGetLocalTimeZoneOffset)
74 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
75 PpapiHostMsg_Flash_GetLocalDataRestrictions, OnGetLocalDataRestrictions)
76 PPAPI_END_MESSAGE_MAP()
77 return PP_ERROR_FAILED;
80 int32_t PepperFlashBrowserHost::OnUpdateActivity(
81 ppapi::host::HostMessageContext* host_context) {
82 #if defined(OS_WIN)
83 // Reading then writing back the same value to the screensaver timeout system
84 // setting resets the countdown which prevents the screensaver from turning
85 // on "for a while". As long as the plugin pings us with this message faster
86 // than the screensaver timeout, it won't go on.
87 int value = 0;
88 if (SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &value, 0))
89 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, value, NULL, 0);
90 #elif defined(OS_MACOSX)
91 UpdateSystemActivity(OverallAct);
92 #else
93 // TODO(brettw) implement this for other platforms.
94 #endif
95 return PP_OK;
98 int32_t PepperFlashBrowserHost::OnGetLocalTimeZoneOffset(
99 ppapi::host::HostMessageContext* host_context,
100 const base::Time& t) {
101 // The reason for this processing being in the browser process is that on
102 // Linux, the localtime calls require filesystem access prohibited by the
103 // sandbox.
104 host_context->reply_msg = PpapiPluginMsg_Flash_GetLocalTimeZoneOffsetReply(
105 ppapi::PPGetLocalTimeZoneOffset(t));
106 return PP_OK;
109 int32_t PepperFlashBrowserHost::OnGetLocalDataRestrictions(
110 ppapi::host::HostMessageContext* context) {
111 // Getting the Flash LSO settings requires using the CookieSettings which
112 // belong to the profile which lives on the UI thread. We lazily initialize
113 // |cookie_settings_| by grabbing the reference from the UI thread and then
114 // call |GetLocalDataRestrictions| with it.
115 GURL document_url = host_->GetDocumentURLForInstance(pp_instance());
116 GURL plugin_url = host_->GetPluginURLForInstance(pp_instance());
117 if (cookie_settings_.get()) {
118 GetLocalDataRestrictions(context->MakeReplyMessageContext(),
119 document_url,
120 plugin_url,
121 cookie_settings_);
122 } else {
123 BrowserThread::PostTaskAndReplyWithResult(
124 BrowserThread::UI,
125 FROM_HERE,
126 base::Bind(&GetCookieSettings, render_process_id_),
127 base::Bind(&PepperFlashBrowserHost::GetLocalDataRestrictions,
128 weak_factory_.GetWeakPtr(),
129 context->MakeReplyMessageContext(),
130 document_url,
131 plugin_url));
133 return PP_OK_COMPLETIONPENDING;
136 void PepperFlashBrowserHost::GetLocalDataRestrictions(
137 ppapi::host::ReplyMessageContext reply_context,
138 const GURL& document_url,
139 const GURL& plugin_url,
140 scoped_refptr<content_settings::CookieSettings> cookie_settings) {
141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
143 // Lazily initialize |cookie_settings_|. The cookie settings are thread-safe
144 // ref-counted so as long as we hold a reference to them we can safely access
145 // them on the IO thread.
146 if (!cookie_settings_.get()) {
147 cookie_settings_ = cookie_settings;
148 } else {
149 DCHECK(cookie_settings_.get() == cookie_settings.get());
152 PP_FlashLSORestrictions restrictions = PP_FLASHLSORESTRICTIONS_NONE;
153 if (cookie_settings_.get() && document_url.is_valid() &&
154 plugin_url.is_valid()) {
155 if (!cookie_settings_->IsReadingCookieAllowed(document_url, plugin_url))
156 restrictions = PP_FLASHLSORESTRICTIONS_BLOCK;
157 else if (cookie_settings_->IsCookieSessionOnly(plugin_url))
158 restrictions = PP_FLASHLSORESTRICTIONS_IN_MEMORY;
160 SendReply(reply_context,
161 PpapiPluginMsg_Flash_GetLocalDataRestrictionsReply(
162 static_cast<int32_t>(restrictions)));
165 } // namespace chrome