chrome_render_message_filter.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 "chrome/browser/renderer_host/chrome_render_message_filter.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/metrics/histogram.h"
12#include "base/strings/utf_string_conversions.h"
13#include "chrome/browser/automation/automation_resource_message_filter.h"
14#include "chrome/browser/browser_process.h"
15#include "chrome/browser/chrome_notification_types.h"
16#include "chrome/browser/content_settings/cookie_settings.h"
17#include "chrome/browser/content_settings/tab_specific_content_settings.h"
18#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
19#include "chrome/browser/extensions/activity_log/activity_actions.h"
20#include "chrome/browser/extensions/activity_log/activity_log.h"
21#include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
22#include "chrome/browser/extensions/api/messaging/message_service.h"
23#include "chrome/browser/extensions/event_router.h"
24#include "chrome/browser/extensions/extension_process_manager.h"
25#include "chrome/browser/extensions/extension_system.h"
26#include "chrome/browser/net/chrome_url_request_context.h"
27#include "chrome/browser/net/predictor.h"
28#include "chrome/browser/profiles/profile_manager.h"
29#include "chrome/browser/task_manager/task_manager.h"
30#include "chrome/common/extensions/api/i18n/default_locale_handler.h"
31#include "chrome/common/extensions/extension_file_util.h"
32#include "chrome/common/extensions/extension_messages.h"
33#include "chrome/common/extensions/message_bundle.h"
34#include "chrome/common/render_messages.h"
35#include "content/public/browser/notification_service.h"
36#include "content/public/browser/render_process_host.h"
37#include "content/public/browser/resource_dispatcher_host.h"
38#include "extensions/common/constants.h"
39
40#if defined(USE_TCMALLOC)
41#include "chrome/browser/browser_about_handler.h"
42#endif
43
44using content::BrowserThread;
45using extensions::APIPermission;
46using WebKit::WebCache;
47
48namespace {
49
50// Logs an action to the extension activity log for the specified profile.  Can
51// be called from any thread.
52void AddActionToExtensionActivityLog(
53    Profile* profile,
54    scoped_refptr<extensions::Action> action) {
55#if defined(ENABLE_EXTENSIONS)
56  // The ActivityLog can only be accessed from the main (UI) thread.  If we're
57  // running on the wrong thread, re-dispatch from the main thread.
58  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
59    BrowserThread::PostTask(
60        BrowserThread::UI, FROM_HERE,
61        base::Bind(&AddActionToExtensionActivityLog, profile, action));
62  } else {
63    if (!g_browser_process->profile_manager()->IsValidProfile(profile))
64      return;
65    // If the action included a URL, check whether it is for an incognito
66    // profile.  The check is performed here so that it can safely be done from
67    // the UI thread.
68    if (action->page_url().is_valid() || !action->page_title().empty())
69      action->set_page_incognito(profile->IsOffTheRecord());
70    extensions::ActivityLog* activity_log =
71        extensions::ActivityLog::GetInstance(profile);
72    activity_log->LogAction(action);
73  }
74#endif
75}
76
77} // namespace
78
79ChromeRenderMessageFilter::ChromeRenderMessageFilter(
80    int render_process_id,
81    Profile* profile,
82    net::URLRequestContextGetter* request_context)
83    : render_process_id_(render_process_id),
84      profile_(profile),
85      off_the_record_(profile_->IsOffTheRecord()),
86      request_context_(request_context),
87      extension_info_map_(
88          extensions::ExtensionSystem::Get(profile)->info_map()),
89      cookie_settings_(CookieSettings::Factory::GetForProfile(profile)),
90      weak_ptr_factory_(this) {
91}
92
93ChromeRenderMessageFilter::~ChromeRenderMessageFilter() {
94}
95
96bool ChromeRenderMessageFilter::OnMessageReceived(const IPC::Message& message,
97                                                  bool* message_was_ok) {
98  bool handled = true;
99  IPC_BEGIN_MESSAGE_MAP_EX(ChromeRenderMessageFilter, message, *message_was_ok)
100    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DnsPrefetch, OnDnsPrefetch)
101    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_Preconnect, OnPreconnect)
102    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ResourceTypeStats,
103                        OnResourceTypeStats)
104    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_UpdatedCacheStats,
105                        OnUpdatedCacheStats)
106    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FPS, OnFPS)
107    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_V8HeapStats, OnV8HeapStats)
108    IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension,
109                        OnOpenChannelToExtension)
110    IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToTab, OnOpenChannelToTab)
111    IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToNativeApp,
112                        OnOpenChannelToNativeApp)
113    IPC_MESSAGE_HANDLER_DELAY_REPLY(ExtensionHostMsg_GetMessageBundle,
114                                    OnGetExtensionMessageBundle)
115    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener, OnExtensionAddListener)
116    IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveListener,
117                        OnExtensionRemoveListener)
118    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyListener,
119                        OnExtensionAddLazyListener)
120    IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyListener,
121                        OnExtensionRemoveLazyListener)
122    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddFilteredListener,
123                        OnExtensionAddFilteredListener)
124    IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveFilteredListener,
125                        OnExtensionRemoveFilteredListener)
126    IPC_MESSAGE_HANDLER(ExtensionHostMsg_CloseChannel, OnExtensionCloseChannel)
127    IPC_MESSAGE_HANDLER(ExtensionHostMsg_RequestForIOThread,
128                        OnExtensionRequestForIOThread)
129    IPC_MESSAGE_HANDLER(ExtensionHostMsg_ShouldSuspendAck,
130                        OnExtensionShouldSuspendAck)
131    IPC_MESSAGE_HANDLER(ExtensionHostMsg_GenerateUniqueID,
132                        OnExtensionGenerateUniqueID)
133    IPC_MESSAGE_HANDLER(ExtensionHostMsg_SuspendAck, OnExtensionSuspendAck)
134    IPC_MESSAGE_HANDLER(ExtensionHostMsg_ResumeRequests,
135                        OnExtensionResumeRequests);
136    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddAPIActionToActivityLog,
137                        OnAddAPIActionToExtensionActivityLog);
138    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddDOMActionToActivityLog,
139                        OnAddDOMActionToExtensionActivityLog);
140    IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddEventToActivityLog,
141                        OnAddEventToExtensionActivityLog);
142    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowDatabase, OnAllowDatabase)
143    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowDOMStorage, OnAllowDOMStorage)
144    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowFileSystem, OnAllowFileSystem)
145    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowIndexedDB, OnAllowIndexedDB)
146    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CanTriggerClipboardRead,
147                        OnCanTriggerClipboardRead)
148    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CanTriggerClipboardWrite,
149                        OnCanTriggerClipboardWrite)
150    IPC_MESSAGE_UNHANDLED(handled = false)
151  IPC_END_MESSAGE_MAP()
152
153#if defined(ENABLE_AUTOMATION)
154  if ((message.type() == ChromeViewHostMsg_GetCookies::ID ||
155       message.type() == ChromeViewHostMsg_SetCookie::ID) &&
156    AutomationResourceMessageFilter::ShouldFilterCookieMessages(
157        render_process_id_, message.routing_id())) {
158    // ChromeFrame then we need to get/set cookies from the external host.
159    IPC_BEGIN_MESSAGE_MAP_EX(ChromeRenderMessageFilter, message,
160                             *message_was_ok)
161      IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_GetCookies,
162                                      OnGetCookies)
163      IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetCookie, OnSetCookie)
164    IPC_END_MESSAGE_MAP()
165    handled = true;
166  }
167#endif
168
169  return handled;
170}
171
172void ChromeRenderMessageFilter::OverrideThreadForMessage(
173    const IPC::Message& message, BrowserThread::ID* thread) {
174  switch (message.type()) {
175    case ChromeViewHostMsg_ResourceTypeStats::ID:
176    case ExtensionHostMsg_AddListener::ID:
177    case ExtensionHostMsg_RemoveListener::ID:
178    case ExtensionHostMsg_AddLazyListener::ID:
179    case ExtensionHostMsg_RemoveLazyListener::ID:
180    case ExtensionHostMsg_AddFilteredListener::ID:
181    case ExtensionHostMsg_RemoveFilteredListener::ID:
182    case ExtensionHostMsg_CloseChannel::ID:
183    case ExtensionHostMsg_ShouldSuspendAck::ID:
184    case ExtensionHostMsg_SuspendAck::ID:
185    case ChromeViewHostMsg_UpdatedCacheStats::ID:
186      *thread = BrowserThread::UI;
187      break;
188    default:
189      break;
190  }
191}
192
193net::HostResolver* ChromeRenderMessageFilter::GetHostResolver() {
194  return request_context_->GetURLRequestContext()->host_resolver();
195}
196
197void ChromeRenderMessageFilter::OnDnsPrefetch(
198    const std::vector<std::string>& hostnames) {
199  if (profile_->GetNetworkPredictor())
200    profile_->GetNetworkPredictor()->DnsPrefetchList(hostnames);
201}
202
203void ChromeRenderMessageFilter::OnPreconnect(const GURL& url) {
204  if (profile_->GetNetworkPredictor())
205    profile_->GetNetworkPredictor()->PreconnectUrl(
206        url, GURL(), chrome_browser_net::UrlInfo::MOUSE_OVER_MOTIVATED, 1);
207}
208
209void ChromeRenderMessageFilter::OnResourceTypeStats(
210    const WebCache::ResourceTypeStats& stats) {
211  HISTOGRAM_COUNTS("WebCoreCache.ImagesSizeKB",
212                   static_cast<int>(stats.images.size / 1024));
213  HISTOGRAM_COUNTS("WebCoreCache.CSSStylesheetsSizeKB",
214                   static_cast<int>(stats.cssStyleSheets.size / 1024));
215  HISTOGRAM_COUNTS("WebCoreCache.ScriptsSizeKB",
216                   static_cast<int>(stats.scripts.size / 1024));
217  HISTOGRAM_COUNTS("WebCoreCache.XSLStylesheetsSizeKB",
218                   static_cast<int>(stats.xslStyleSheets.size / 1024));
219  HISTOGRAM_COUNTS("WebCoreCache.FontsSizeKB",
220                   static_cast<int>(stats.fonts.size / 1024));
221
222  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
223#if defined(ENABLE_TASK_MANAGER)
224  TaskManager::GetInstance()->model()->NotifyResourceTypeStats(peer_pid(),
225                                                               stats);
226#endif  // defined(ENABLE_TASK_MANAGER)
227}
228
229void ChromeRenderMessageFilter::OnUpdatedCacheStats(
230    const WebCache::UsageStats& stats) {
231  WebCacheManager::GetInstance()->ObserveStats(render_process_id_, stats);
232}
233
234void ChromeRenderMessageFilter::OnFPS(int routing_id, float fps) {
235  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
236    BrowserThread::PostTask(
237        BrowserThread::UI, FROM_HERE,
238        base::Bind(
239            &ChromeRenderMessageFilter::OnFPS, this,
240            routing_id, fps));
241    return;
242  }
243
244  base::ProcessId renderer_id = peer_pid();
245
246#if defined(ENABLE_TASK_MANAGER)
247  TaskManager::GetInstance()->model()->NotifyFPS(
248      renderer_id, routing_id, fps);
249#endif  // defined(ENABLE_TASK_MANAGER)
250
251  FPSDetails details(routing_id, fps);
252  content::NotificationService::current()->Notify(
253      chrome::NOTIFICATION_RENDERER_FPS_COMPUTED,
254      content::Source<const base::ProcessId>(&renderer_id),
255      content::Details<const FPSDetails>(&details));
256}
257
258void ChromeRenderMessageFilter::OnV8HeapStats(int v8_memory_allocated,
259                                              int v8_memory_used) {
260  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
261    BrowserThread::PostTask(
262        BrowserThread::UI, FROM_HERE,
263        base::Bind(&ChromeRenderMessageFilter::OnV8HeapStats, this,
264                   v8_memory_allocated, v8_memory_used));
265    return;
266  }
267
268  base::ProcessId renderer_id = peer_pid();
269
270#if defined(ENABLE_TASK_MANAGER)
271  TaskManager::GetInstance()->model()->NotifyV8HeapStats(
272      renderer_id,
273      static_cast<size_t>(v8_memory_allocated),
274      static_cast<size_t>(v8_memory_used));
275#endif  // defined(ENABLE_TASK_MANAGER)
276
277  V8HeapStatsDetails details(v8_memory_allocated, v8_memory_used);
278  content::NotificationService::current()->Notify(
279      chrome::NOTIFICATION_RENDERER_V8_HEAP_STATS_COMPUTED,
280      content::Source<const base::ProcessId>(&renderer_id),
281      content::Details<const V8HeapStatsDetails>(&details));
282}
283
284void ChromeRenderMessageFilter::OnOpenChannelToExtension(
285    int routing_id,
286    const ExtensionMsg_ExternalConnectionInfo& info,
287    const std::string& channel_name,
288    bool include_tls_channel_id,
289    int* port_id) {
290  int port2_id;
291  extensions::MessageService::AllocatePortIdPair(port_id, &port2_id);
292
293  BrowserThread::PostTask(
294      BrowserThread::UI, FROM_HERE,
295      base::Bind(&ChromeRenderMessageFilter::OpenChannelToExtensionOnUIThread,
296                 this, render_process_id_, routing_id, port2_id, info,
297                 channel_name, include_tls_channel_id));
298}
299
300void ChromeRenderMessageFilter::OpenChannelToExtensionOnUIThread(
301    int source_process_id, int source_routing_id,
302    int receiver_port_id,
303    const ExtensionMsg_ExternalConnectionInfo& info,
304    const std::string& channel_name,
305    bool include_tls_channel_id) {
306  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
307  extensions::MessageService::Get(profile_)->OpenChannelToExtension(
308      source_process_id, source_routing_id, receiver_port_id,
309      info.source_id, info.target_id, info.source_url, channel_name,
310      include_tls_channel_id);
311}
312
313void ChromeRenderMessageFilter::OnOpenChannelToNativeApp(
314    int routing_id,
315    const std::string& source_extension_id,
316    const std::string& native_app_name,
317    int* port_id) {
318  int port2_id;
319  extensions::MessageService::AllocatePortIdPair(port_id, &port2_id);
320
321  BrowserThread::PostTask(
322      BrowserThread::UI, FROM_HERE,
323      base::Bind(&ChromeRenderMessageFilter::OpenChannelToNativeAppOnUIThread,
324                 this, routing_id, port2_id, source_extension_id,
325                 native_app_name));
326}
327
328void ChromeRenderMessageFilter::OpenChannelToNativeAppOnUIThread(
329    int source_routing_id,
330    int receiver_port_id,
331    const std::string& source_extension_id,
332    const std::string& native_app_name) {
333  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
334  extensions::MessageService::Get(profile_)->OpenChannelToNativeApp(
335      render_process_id_, source_routing_id, receiver_port_id,
336      source_extension_id, native_app_name);
337}
338
339void ChromeRenderMessageFilter::OnOpenChannelToTab(
340    int routing_id, int tab_id, const std::string& extension_id,
341    const std::string& channel_name, int* port_id) {
342  int port2_id;
343  extensions::MessageService::AllocatePortIdPair(port_id, &port2_id);
344
345  BrowserThread::PostTask(
346      BrowserThread::UI, FROM_HERE,
347      base::Bind(&ChromeRenderMessageFilter::OpenChannelToTabOnUIThread, this,
348                 render_process_id_, routing_id, port2_id, tab_id, extension_id,
349                 channel_name));
350}
351
352void ChromeRenderMessageFilter::OpenChannelToTabOnUIThread(
353    int source_process_id, int source_routing_id,
354    int receiver_port_id,
355    int tab_id,
356    const std::string& extension_id,
357    const std::string& channel_name) {
358  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
359  extensions::MessageService::Get(profile_)->OpenChannelToTab(
360      source_process_id, source_routing_id, receiver_port_id,
361      tab_id, extension_id, channel_name);
362}
363
364void ChromeRenderMessageFilter::OnGetExtensionMessageBundle(
365    const std::string& extension_id, IPC::Message* reply_msg) {
366  const extensions::Extension* extension =
367      extension_info_map_->extensions().GetByID(extension_id);
368  base::FilePath extension_path;
369  std::string default_locale;
370  if (extension) {
371    extension_path = extension->path();
372    default_locale = extensions::LocaleInfo::GetDefaultLocale(extension);
373  }
374
375  BrowserThread::PostTask(
376      BrowserThread::FILE, FROM_HERE,
377      base::Bind(
378          &ChromeRenderMessageFilter::OnGetExtensionMessageBundleOnFileThread,
379          this, extension_path, extension_id, default_locale, reply_msg));
380}
381
382void ChromeRenderMessageFilter::OnGetExtensionMessageBundleOnFileThread(
383    const base::FilePath& extension_path,
384    const std::string& extension_id,
385    const std::string& default_locale,
386    IPC::Message* reply_msg) {
387  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
388
389  scoped_ptr<extensions::MessageBundle::SubstitutionMap> dictionary_map(
390      extension_file_util::LoadMessageBundleSubstitutionMap(
391          extension_path, extension_id, default_locale));
392
393  ExtensionHostMsg_GetMessageBundle::WriteReplyParams(reply_msg,
394                                                      *dictionary_map);
395  Send(reply_msg);
396}
397
398void ChromeRenderMessageFilter::OnExtensionAddListener(
399    const std::string& extension_id,
400    const std::string& event_name) {
401  content::RenderProcessHost* process =
402      content::RenderProcessHost::FromID(render_process_id_);
403  if (!process || !extensions::ExtensionSystem::Get(profile_)->event_router())
404    return;
405
406  extensions::ExtensionSystem::Get(profile_)->event_router()->AddEventListener(
407      event_name, process, extension_id);
408}
409
410void ChromeRenderMessageFilter::OnExtensionRemoveListener(
411    const std::string& extension_id,
412    const std::string& event_name) {
413  content::RenderProcessHost* process =
414      content::RenderProcessHost::FromID(render_process_id_);
415  if (!process || !extensions::ExtensionSystem::Get(profile_)->event_router())
416    return;
417
418  extensions::ExtensionSystem::Get(profile_)->event_router()->
419      RemoveEventListener(event_name, process, extension_id);
420}
421
422void ChromeRenderMessageFilter::OnExtensionAddLazyListener(
423    const std::string& extension_id, const std::string& event_name) {
424  if (extensions::ExtensionSystem::Get(profile_)->event_router()) {
425    extensions::ExtensionSystem::Get(profile_)->event_router()->
426        AddLazyEventListener(event_name, extension_id);
427  }
428}
429
430void ChromeRenderMessageFilter::OnExtensionRemoveLazyListener(
431    const std::string& extension_id, const std::string& event_name) {
432  if (extensions::ExtensionSystem::Get(profile_)->event_router()) {
433    extensions::ExtensionSystem::Get(profile_)->event_router()->
434        RemoveLazyEventListener(event_name, extension_id);
435  }
436}
437
438void ChromeRenderMessageFilter::OnExtensionAddFilteredListener(
439    const std::string& extension_id,
440    const std::string& event_name,
441    const base::DictionaryValue& filter,
442    bool lazy) {
443  content::RenderProcessHost* process =
444      content::RenderProcessHost::FromID(render_process_id_);
445  if (!process || !extensions::ExtensionSystem::Get(profile_)->event_router())
446    return;
447
448  extensions::ExtensionSystem::Get(profile_)->event_router()->
449      AddFilteredEventListener(event_name, process, extension_id, filter, lazy);
450}
451
452void ChromeRenderMessageFilter::OnExtensionRemoveFilteredListener(
453    const std::string& extension_id,
454    const std::string& event_name,
455    const base::DictionaryValue& filter,
456    bool lazy) {
457  content::RenderProcessHost* process =
458      content::RenderProcessHost::FromID(render_process_id_);
459  if (!process || !extensions::ExtensionSystem::Get(profile_)->event_router())
460    return;
461
462  extensions::ExtensionSystem::Get(profile_)->event_router()->
463      RemoveFilteredEventListener(event_name, process, extension_id, filter,
464                                  lazy);
465}
466
467void ChromeRenderMessageFilter::OnExtensionCloseChannel(
468    int port_id,
469    const std::string& error_message) {
470  if (!content::RenderProcessHost::FromID(render_process_id_))
471    return;  // To guard against crash in browser_tests shutdown.
472
473  extensions::MessageService* message_service =
474      extensions::MessageService::Get(profile_);
475  if (message_service)
476    message_service->CloseChannel(port_id, error_message);
477}
478
479void ChromeRenderMessageFilter::OnExtensionRequestForIOThread(
480    int routing_id,
481    const ExtensionHostMsg_Request_Params& params) {
482  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
483
484  ExtensionFunctionDispatcher::DispatchOnIOThread(
485      extension_info_map_.get(), profile_, render_process_id_,
486      weak_ptr_factory_.GetWeakPtr(), routing_id, params);
487}
488
489void ChromeRenderMessageFilter::OnExtensionShouldSuspendAck(
490     const std::string& extension_id, int sequence_id) {
491  if (extensions::ExtensionSystem::Get(profile_)->process_manager()) {
492    extensions::ExtensionSystem::Get(profile_)->process_manager()->
493        OnShouldSuspendAck(extension_id, sequence_id);
494  }
495}
496
497void ChromeRenderMessageFilter::OnExtensionSuspendAck(
498     const std::string& extension_id) {
499  if (extensions::ExtensionSystem::Get(profile_)->process_manager()) {
500    extensions::ExtensionSystem::Get(profile_)->process_manager()->
501        OnSuspendAck(extension_id);
502  }
503}
504
505void ChromeRenderMessageFilter::OnExtensionGenerateUniqueID(int* unique_id) {
506  static int next_unique_id = 0;
507  *unique_id = ++next_unique_id;
508}
509
510void ChromeRenderMessageFilter::OnExtensionResumeRequests(int route_id) {
511  content::ResourceDispatcherHost::Get()->ResumeBlockedRequestsForRoute(
512      render_process_id_, route_id);
513}
514
515void ChromeRenderMessageFilter::OnAddAPIActionToExtensionActivityLog(
516    const std::string& extension_id,
517    const ExtensionHostMsg_APIActionOrEvent_Params& params) {
518  scoped_refptr<extensions::Action> action = new extensions::Action(
519      extension_id, base::Time::Now(), extensions::Action::ACTION_API_CALL,
520      params.api_call);
521  action->set_args(make_scoped_ptr(params.arguments.DeepCopy()));
522  if (!params.extra.empty()) {
523    action->mutable_other()->SetString(
524        activity_log_constants::kActionExtra, params.extra);
525  }
526  AddActionToExtensionActivityLog(profile_, action);
527}
528
529void ChromeRenderMessageFilter::OnAddDOMActionToExtensionActivityLog(
530    const std::string& extension_id,
531    const ExtensionHostMsg_DOMAction_Params& params) {
532  scoped_refptr<extensions::Action> action = new extensions::Action(
533      extension_id, base::Time::Now(), extensions::Action::ACTION_DOM_ACCESS,
534      params.api_call);
535  action->set_args(make_scoped_ptr(params.arguments.DeepCopy()));
536  action->set_page_url(params.url);
537  action->set_page_title(base::UTF16ToUTF8(params.url_title));
538  action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
539                                      params.call_type);
540  AddActionToExtensionActivityLog(profile_, action);
541}
542
543void ChromeRenderMessageFilter::OnAddEventToExtensionActivityLog(
544    const std::string& extension_id,
545    const ExtensionHostMsg_APIActionOrEvent_Params& params) {
546  scoped_refptr<extensions::Action> action = new extensions::Action(
547      extension_id, base::Time::Now(), extensions::Action::ACTION_API_EVENT,
548      params.api_call);
549  action->set_args(make_scoped_ptr(params.arguments.DeepCopy()));
550  if (!params.extra.empty()) {
551    action->mutable_other()->SetString(activity_log_constants::kActionExtra,
552                                       params.extra);
553  }
554  AddActionToExtensionActivityLog(profile_, action);
555}
556
557void ChromeRenderMessageFilter::OnAllowDatabase(int render_view_id,
558                                                const GURL& origin_url,
559                                                const GURL& top_origin_url,
560                                                const string16& name,
561                                                const string16& display_name,
562                                                bool* allowed) {
563  *allowed =
564      cookie_settings_->IsSettingCookieAllowed(origin_url, top_origin_url);
565  BrowserThread::PostTask(
566      BrowserThread::UI, FROM_HERE,
567      base::Bind(&TabSpecificContentSettings::WebDatabaseAccessed,
568                 render_process_id_, render_view_id, origin_url, name,
569                 display_name, !*allowed));
570}
571
572void ChromeRenderMessageFilter::OnAllowDOMStorage(int render_view_id,
573                                                  const GURL& origin_url,
574                                                  const GURL& top_origin_url,
575                                                  bool local,
576                                                  bool* allowed) {
577  *allowed =
578      cookie_settings_->IsSettingCookieAllowed(origin_url, top_origin_url);
579  // Record access to DOM storage for potential display in UI.
580  BrowserThread::PostTask(
581      BrowserThread::UI, FROM_HERE,
582      base::Bind(&TabSpecificContentSettings::DOMStorageAccessed,
583                 render_process_id_, render_view_id, origin_url, local,
584                 !*allowed));
585}
586
587void ChromeRenderMessageFilter::OnAllowFileSystem(int render_view_id,
588                                                  const GURL& origin_url,
589                                                  const GURL& top_origin_url,
590                                                  bool* allowed) {
591  *allowed =
592      cookie_settings_->IsSettingCookieAllowed(origin_url, top_origin_url);
593  // Record access to file system for potential display in UI.
594  BrowserThread::PostTask(
595      BrowserThread::UI, FROM_HERE,
596      base::Bind(&TabSpecificContentSettings::FileSystemAccessed,
597                 render_process_id_, render_view_id, origin_url, !*allowed));
598}
599
600void ChromeRenderMessageFilter::OnAllowIndexedDB(int render_view_id,
601                                                 const GURL& origin_url,
602                                                 const GURL& top_origin_url,
603                                                 const string16& name,
604                                                 bool* allowed) {
605  *allowed =
606      cookie_settings_->IsSettingCookieAllowed(origin_url, top_origin_url);
607  BrowserThread::PostTask(
608      BrowserThread::UI, FROM_HERE,
609      base::Bind(&TabSpecificContentSettings::IndexedDBAccessed,
610                 render_process_id_, render_view_id, origin_url, name,
611                 !*allowed));
612}
613
614void ChromeRenderMessageFilter::OnCanTriggerClipboardRead(
615    const GURL& origin, bool* allowed) {
616  *allowed = extension_info_map_->SecurityOriginHasAPIPermission(
617      origin, render_process_id_, APIPermission::kClipboardRead);
618}
619
620void ChromeRenderMessageFilter::OnCanTriggerClipboardWrite(
621    const GURL& origin, bool* allowed) {
622  // Since all extensions could historically write to the clipboard, preserve it
623  // for compatibility.
624  *allowed = (origin.SchemeIs(extensions::kExtensionScheme) ||
625      extension_info_map_->SecurityOriginHasAPIPermission(
626          origin, render_process_id_, APIPermission::kClipboardWrite));
627}
628
629void ChromeRenderMessageFilter::OnGetCookies(
630    const GURL& url,
631    const GURL& first_party_for_cookies,
632    IPC::Message* reply_msg) {
633#if defined(ENABLE_AUTOMATION)
634  AutomationResourceMessageFilter::GetCookiesForUrl(
635      this, request_context_->GetURLRequestContext(), render_process_id_,
636      reply_msg, url);
637#endif
638}
639
640void ChromeRenderMessageFilter::OnSetCookie(const IPC::Message& message,
641                                            const GURL& url,
642                                            const GURL& first_party_for_cookies,
643                                            const std::string& cookie) {
644#if defined(ENABLE_AUTOMATION)
645  AutomationResourceMessageFilter::SetCookiesForUrl(
646      render_process_id_, message.routing_id(), url, cookie);
647#endif
648}
649