Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Side by Side Diff: chrome/renderer/notification_provider.cc

Issue 6151011: Introduce RenderView::Observer interface so that RenderView doesn't have to k... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/notification_provider.h" 5 #include "chrome/renderer/notification_provider.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/task.h" 8 #include "base/task.h"
9 #include "chrome/common/render_messages.h" 9 #include "chrome/common/render_messages.h"
10 #include "chrome/common/render_messages_params.h" 10 #include "chrome/common/render_messages_params.h"
11 #include "chrome/common/url_constants.h" 11 #include "chrome/common/url_constants.h"
12 #include "chrome/renderer/render_thread.h" 12 #include "chrome/renderer/render_thread.h"
13 #include "chrome/renderer/render_view.h"
14 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" 13 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
15 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" 14 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
16 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPermissionCal lback.h" 15 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPermissionCal lback.h"
17 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" 16 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h"
18 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" 17 #include "third_party/WebKit/WebKit/chromium/public/WebView.h"
19 18
20 using WebKit::WebDocument; 19 using WebKit::WebDocument;
21 using WebKit::WebNotification; 20 using WebKit::WebNotification;
22 using WebKit::WebNotificationPresenter; 21 using WebKit::WebNotificationPresenter;
23 using WebKit::WebNotificationPermissionCallback; 22 using WebKit::WebNotificationPermissionCallback;
24 using WebKit::WebSecurityOrigin; 23 using WebKit::WebSecurityOrigin;
25 using WebKit::WebString; 24 using WebKit::WebString;
26 using WebKit::WebURL; 25 using WebKit::WebURL;
27 26
28 NotificationProvider::NotificationProvider(RenderView* view) 27 NotificationProvider::NotificationProvider() {
29 : view_(view) {
30 } 28 }
31 29
32 NotificationProvider::~NotificationProvider() { 30 NotificationProvider::~NotificationProvider() {
33 manager_.DetachAll(); 31 manager_.DetachAll();
34 } 32 }
35 33
36 bool NotificationProvider::show(const WebNotification& notification) { 34 bool NotificationProvider::show(const WebNotification& notification) {
37 int notification_id = manager_.RegisterNotification(notification); 35 int notification_id = manager_.RegisterNotification(notification);
38 if (notification.isHTML()) 36 if (notification.isHTML())
39 return ShowHTML(notification, notification_id); 37 return ShowHTML(notification, notification_id);
40 else 38 else
41 return ShowText(notification, notification_id); 39 return ShowText(notification, notification_id);
42 } 40 }
43 41
44 void NotificationProvider::cancel(const WebNotification& notification) { 42 void NotificationProvider::cancel(const WebNotification& notification) {
45 int id; 43 int id;
46 bool id_found = manager_.GetId(notification, id); 44 bool id_found = manager_.GetId(notification, id);
47 // Won't be found if the notification has already been closed by the user. 45 // Won't be found if the notification has already been closed by the user.
48 if (id_found) 46 if (id_found)
49 Send(new ViewHostMsg_CancelDesktopNotification(view_->routing_id(), id)); 47 Send(new ViewHostMsg_CancelDesktopNotification(routing_id(), id));
50 } 48 }
51 49
52 void NotificationProvider::objectDestroyed( 50 void NotificationProvider::objectDestroyed(
53 const WebNotification& notification) { 51 const WebNotification& notification) {
54 int id; 52 int id;
55 bool id_found = manager_.GetId(notification, id); 53 bool id_found = manager_.GetId(notification, id);
56 // Won't be found if the notification has already been closed by the user. 54 // Won't be found if the notification has already been closed by the user.
57 if (id_found) 55 if (id_found)
58 manager_.UnregisterNotification(id); 56 manager_.UnregisterNotification(id);
59 } 57 }
60 58
61 WebNotificationPresenter::Permission NotificationProvider::checkPermission( 59 WebNotificationPresenter::Permission NotificationProvider::checkPermission(
62 const WebURL& url) { 60 const WebURL& url) {
63 int permission; 61 int permission;
64 Send(new ViewHostMsg_CheckNotificationPermission( 62 Send(new ViewHostMsg_CheckNotificationPermission(
65 view_->routing_id(), 63 routing_id(),
66 url, 64 url,
67 &permission)); 65 &permission));
68 return static_cast<WebNotificationPresenter::Permission>(permission); 66 return static_cast<WebNotificationPresenter::Permission>(permission);
69 } 67 }
70 68
71 void NotificationProvider::requestPermission( 69 void NotificationProvider::requestPermission(
72 const WebSecurityOrigin& origin, 70 const WebSecurityOrigin& origin,
73 WebNotificationPermissionCallback* callback) { 71 WebNotificationPermissionCallback* callback) {
74 // We only request permission in response to a user gesture. 72 // We only request permission in response to a user gesture.
75 if (!view_->webview()->mainFrame()->isProcessingUserGesture()) 73 if (!render_view()->webview()->mainFrame()->isProcessingUserGesture())
76 return; 74 return;
77 75
78 int id = manager_.RegisterPermissionRequest(callback); 76 int id = manager_.RegisterPermissionRequest(callback);
79 77
80 Send(new ViewHostMsg_RequestNotificationPermission(view_->routing_id(), 78 Send(new ViewHostMsg_RequestNotificationPermission(routing_id(),
81 GURL(origin.toString()), 79 GURL(origin.toString()),
82 id)); 80 id));
83 } 81 }
84 82
85 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) { 83 bool NotificationProvider::OnMessageReceived(const IPC::Message& message) {
86 bool handled = true; 84 bool handled = true;
87 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message) 85 IPC_BEGIN_MESSAGE_MAP(NotificationProvider, message)
88 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay); 86 IPC_MESSAGE_HANDLER(ViewMsg_PostDisplayToNotificationObject, OnDisplay);
89 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError); 87 IPC_MESSAGE_HANDLER(ViewMsg_PostErrorToNotificationObject, OnError);
90 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose); 88 IPC_MESSAGE_HANDLER(ViewMsg_PostCloseToNotificationObject, OnClose);
91 IPC_MESSAGE_HANDLER(ViewMsg_PostClickToNotificationObject, OnClick); 89 IPC_MESSAGE_HANDLER(ViewMsg_PostClickToNotificationObject, OnClick);
92 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone, 90 IPC_MESSAGE_HANDLER(ViewMsg_PermissionRequestDone,
93 OnPermissionRequestComplete); 91 OnPermissionRequestComplete);
94 IPC_MESSAGE_UNHANDLED(handled = false) 92 IPC_MESSAGE_UNHANDLED(handled = false)
95 IPC_END_MESSAGE_MAP() 93 IPC_END_MESSAGE_MAP()
94
95 if (message.type() == ViewMsg_Navigate::ID)
96 OnNavigate(); // Don't want to swallow the message.
97
96 return handled; 98 return handled;
97 } 99 }
98 100
99 void NotificationProvider::OnNavigate() {
100 manager_.Clear();
101 }
102
103 bool NotificationProvider::ShowHTML(const WebNotification& notification, 101 bool NotificationProvider::ShowHTML(const WebNotification& notification,
104 int id) { 102 int id) {
105 // Disallow HTML notifications from unwanted schemes. javascript: 103 // Disallow HTML notifications from unwanted schemes. javascript:
106 // in particular allows unwanted cross-domain access. 104 // in particular allows unwanted cross-domain access.
107 GURL url = notification.url(); 105 GURL url = notification.url();
108 if (!url.SchemeIs(chrome::kHttpScheme) && 106 if (!url.SchemeIs(chrome::kHttpScheme) &&
109 !url.SchemeIs(chrome::kHttpsScheme) && 107 !url.SchemeIs(chrome::kHttpsScheme) &&
110 !url.SchemeIs(chrome::kExtensionScheme) && 108 !url.SchemeIs(chrome::kExtensionScheme) &&
111 !url.SchemeIs(chrome::kDataScheme)) 109 !url.SchemeIs(chrome::kDataScheme))
112 return false; 110 return false;
113 111
114 DCHECK(notification.isHTML()); 112 DCHECK(notification.isHTML());
115 ViewHostMsg_ShowNotification_Params params; 113 ViewHostMsg_ShowNotification_Params params;
116 params.origin = GURL(view_->webview()->mainFrame()->url()).GetOrigin(); 114 params.origin =
115 GURL(render_view()->webview()->mainFrame()->url()).GetOrigin();
117 params.is_html = true; 116 params.is_html = true;
118 params.contents_url = notification.url(); 117 params.contents_url = notification.url();
119 params.notification_id = id; 118 params.notification_id = id;
120 params.replace_id = notification.replaceId(); 119 params.replace_id = notification.replaceId();
121 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), 120 return Send(new ViewHostMsg_ShowDesktopNotification(routing_id(), params));
122 params));
123 } 121 }
124 122
125 bool NotificationProvider::ShowText(const WebNotification& notification, 123 bool NotificationProvider::ShowText(const WebNotification& notification,
126 int id) { 124 int id) {
127 DCHECK(!notification.isHTML()); 125 DCHECK(!notification.isHTML());
128 ViewHostMsg_ShowNotification_Params params; 126 ViewHostMsg_ShowNotification_Params params;
129 params.is_html = false; 127 params.is_html = false;
130 params.origin = GURL(view_->webview()->mainFrame()->url()).GetOrigin(); 128 params.origin = GURL(
129 render_view()->webview()->mainFrame()->url()).GetOrigin();
131 params.icon_url = notification.iconURL(); 130 params.icon_url = notification.iconURL();
132 params.title = notification.title(); 131 params.title = notification.title();
133 params.body = notification.body(); 132 params.body = notification.body();
134 params.direction = notification.direction(); 133 params.direction = notification.direction();
135 params.notification_id = id; 134 params.notification_id = id;
136 params.replace_id = notification.replaceId(); 135 params.replace_id = notification.replaceId();
137 136
138 return Send(new ViewHostMsg_ShowDesktopNotification(view_->routing_id(), 137 return Send(new ViewHostMsg_ShowDesktopNotification(routing_id(), params));
139 params));
140 } 138 }
141 139
142 void NotificationProvider::OnDisplay(int id) { 140 void NotificationProvider::OnDisplay(int id) {
143 WebNotification notification; 141 WebNotification notification;
144 bool found = manager_.GetNotification(id, &notification); 142 bool found = manager_.GetNotification(id, &notification);
145 // |found| may be false if the WebNotification went out of scope in 143 // |found| may be false if the WebNotification went out of scope in
146 // the page before it was actually displayed to the user. 144 // the page before it was actually displayed to the user.
147 if (found) 145 if (found)
148 notification.dispatchDisplayEvent(); 146 notification.dispatchDisplayEvent();
149 } 147 }
(...skipping 27 matching lines...) Expand all
177 notification.dispatchClickEvent(); 175 notification.dispatchClickEvent();
178 } 176 }
179 177
180 void NotificationProvider::OnPermissionRequestComplete(int id) { 178 void NotificationProvider::OnPermissionRequestComplete(int id) {
181 WebNotificationPermissionCallback* callback = manager_.GetCallback(id); 179 WebNotificationPermissionCallback* callback = manager_.GetCallback(id);
182 DCHECK(callback); 180 DCHECK(callback);
183 callback->permissionRequestComplete(); 181 callback->permissionRequestComplete();
184 manager_.OnPermissionRequestComplete(id); 182 manager_.OnPermissionRequestComplete(id);
185 } 183 }
186 184
187 bool NotificationProvider::Send(IPC::Message* message) { 185 void NotificationProvider::OnNavigate() {
188 return RenderThread::current()->Send(message); 186 manager_.Clear();
189 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698