15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/notification_object_proxy.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/guid.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/desktop_notification_delegate.h"
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/render_frame_host.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/render_process_host.h"
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/web_contents.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
140529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochNotificationObjectProxy::NotificationObjectProxy(
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    content::RenderFrameHost* render_frame_host,
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    scoped_ptr<content::DesktopNotificationDelegate> delegate)
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    : render_process_id_(render_frame_host->GetProcess()->GetID()),
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      render_frame_id_(render_frame_host->GetRoutingID()),
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      delegate_(delegate.Pass()),
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      displayed_(false),
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      id_(base::GenerateGUID()) {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)NotificationObjectProxy::~NotificationObjectProxy() {}
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NotificationObjectProxy::Display() {
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This method is called each time the notification is shown to the user
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // but we only want to fire the event the first time.
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (displayed_)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  displayed_ = true;
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  delegate_->NotificationDisplayed();
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NotificationObjectProxy::Error() {
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  delegate_->NotificationError();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NotificationObjectProxy::Close(bool by_user) {
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  delegate_->NotificationClosed(by_user);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NotificationObjectProxy::Click() {
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  delegate_->NotificationClick();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string NotificationObjectProxy::id() const {
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return id_;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochcontent::WebContents* NotificationObjectProxy::GetWebContents() const {
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return content::WebContents::FromRenderFrameHost(
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      content::RenderFrameHost::FromID(render_process_id_, render_frame_id_));
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
56