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