desktop_notification_handler.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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/desktop_notification_handler.h" 6 7#include "chrome/browser/browser_list.h" 8#include "chrome/browser/notifications/desktop_notification_service.h" 9#include "chrome/browser/profiles/profile.h" 10#include "chrome/browser/renderer_host/render_process_host.h" 11#include "chrome/browser/tab_contents/tab_contents.h" 12#include "chrome/common/render_messages.h" 13#include "chrome/common/render_messages_params.h" 14 15DesktopNotificationHandler::DesktopNotificationHandler( 16 TabContents* tab, RenderProcessHost* process) 17 : tab_(tab), 18 process_(process) { 19} 20 21bool DesktopNotificationHandler::OnMessageReceived( 22 const IPC::Message& message) { 23 bool handled = true; 24 25 IPC_BEGIN_MESSAGE_MAP(DesktopNotificationHandler, message) 26 IPC_MESSAGE_HANDLER(ViewHostMsg_ShowDesktopNotification, 27 OnShowDesktopNotification) 28 IPC_MESSAGE_HANDLER(ViewHostMsg_CancelDesktopNotification, 29 OnCancelDesktopNotification) 30 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestNotificationPermission, 31 OnRequestNotificationPermission) 32 IPC_MESSAGE_UNHANDLED(handled = false) 33 IPC_END_MESSAGE_MAP() 34 35 return handled; 36} 37 38void DesktopNotificationHandler::OnShowDesktopNotification( 39 const IPC::Message& message, 40 const ViewHostMsg_ShowNotification_Params& params) { 41 RenderProcessHost* process = GetRenderProcessHost(); 42 DesktopNotificationService* service = 43 process->profile()->GetDesktopNotificationService(); 44 45 service->ShowDesktopNotification( 46 params, 47 process->id(), 48 message.routing_id(), 49 DesktopNotificationService::PageNotification); 50} 51 52void DesktopNotificationHandler::OnCancelDesktopNotification( 53 const IPC::Message& message, int notification_id) { 54 RenderProcessHost* process = GetRenderProcessHost(); 55 DesktopNotificationService* service = 56 process->profile()->GetDesktopNotificationService(); 57 58 service->CancelDesktopNotification( 59 process->id(), 60 message.routing_id(), 61 notification_id); 62} 63 64void DesktopNotificationHandler::OnRequestNotificationPermission( 65 const IPC::Message& message, const GURL& source_origin, 66 int callback_context) { 67 RenderProcessHost* process = GetRenderProcessHost(); 68 Browser* browser = BrowserList::GetLastActive(); 69 // We may not have a BrowserList if the chrome browser process is launched as 70 // a ChromeFrame process in which case we attempt to use the TabContents 71 // provided by the RenderViewHostDelegate. 72 TabContents* tab = browser ? browser->GetSelectedTabContents() : tab_; 73 if (!tab) 74 return; 75 76 DesktopNotificationService* service = 77 tab->profile()->GetDesktopNotificationService(); 78 service->RequestPermission( 79 source_origin, 80 process->id(), 81 message.routing_id(), 82 callback_context, 83 tab); 84} 85 86RenderProcessHost* DesktopNotificationHandler::GetRenderProcessHost() { 87 return tab_ ? tab_->GetRenderProcessHost() : process_; 88} 89 90