OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/views/notifications/balloon_view_host.h" | 5 #include "chrome/browser/views/notifications/balloon_view_host.h" |
6 | 6 |
7 #include "base/string_util.h" | |
8 #include "chrome/browser/browser_list.h" | |
9 #include "chrome/browser/extensions/extension_process_manager.h" | |
10 #include "chrome/browser/in_process_webkit/dom_storage_context.h" | |
11 #include "chrome/browser/in_process_webkit/webkit_context.h" | |
12 #include "chrome/browser/notifications/balloon.h" | 7 #include "chrome/browser/notifications/balloon.h" |
13 #include "chrome/browser/notifications/notification.h" | |
14 #include "chrome/browser/profile.h" | |
15 #include "chrome/browser/renderer_host/render_view_host.h" | 8 #include "chrome/browser/renderer_host/render_view_host.h" |
16 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 9 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
17 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
18 #include "chrome/browser/renderer_host/render_widget_host_view_win.h" | 11 #include "chrome/browser/renderer_host/render_widget_host_view_win.h" |
19 #endif | 12 #endif |
20 #if defined(OS_LINUX) | 13 #if defined(OS_LINUX) |
21 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" | 14 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" |
22 #endif | 15 #endif |
23 #include "chrome/browser/renderer_host/site_instance.h" | |
24 #include "chrome/common/bindings_policy.h" | |
25 #include "chrome/common/notification_service.h" | |
26 #include "chrome/common/notification_type.h" | |
27 #include "chrome/common/render_messages.h" | |
28 #include "chrome/common/renderer_preferences.h" | |
29 #include "chrome/common/url_constants.h" | |
30 #include "views/widget/widget.h" | 16 #include "views/widget/widget.h" |
31 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
32 #include "views/widget/widget_win.h" | 18 #include "views/widget/widget_win.h" |
33 #endif | 19 #endif |
34 #if defined(OS_LINUX) | 20 #if defined(OS_LINUX) |
35 #include "views/widget/widget_gtk.h" | 21 #include "views/widget/widget_gtk.h" |
36 #endif | 22 #endif |
37 | 23 |
| 24 class BalloonViewHostView : public views::NativeViewHost { |
| 25 public: |
| 26 explicit BalloonViewHostView(BalloonViewHost* host) |
| 27 : host_(host), |
| 28 initialized_(false) { |
| 29 } |
| 30 |
| 31 virtual void ViewHierarchyChanged(bool is_add, |
| 32 views::View* parent, |
| 33 views::View* child) { |
| 34 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); |
| 35 if (is_add && GetWidget() && !initialized_) |
| 36 host_->Init(GetWidget()->GetNativeView()); |
| 37 initialized_ = true; |
| 38 } |
| 39 |
| 40 private: |
| 41 // The host owns this object. |
| 42 BalloonViewHost* host_; |
| 43 |
| 44 bool initialized_; |
| 45 }; |
| 46 |
38 BalloonViewHost::BalloonViewHost(Balloon* balloon) | 47 BalloonViewHost::BalloonViewHost(Balloon* balloon) |
39 : initialized_(false), | 48 : BalloonHost(balloon) { |
40 balloon_(balloon), | 49 native_host_.reset(new BalloonViewHostView(this)); |
41 render_view_host_(NULL), | |
42 should_notify_on_disconnect_(false), | |
43 is_extension_page_(false) { | |
44 DCHECK(balloon_); | |
45 | |
46 // If the notification is for an extension URL, make sure to use the extension | |
47 // process to render it, so that it can communicate with other views in the | |
48 // extension. | |
49 const GURL& balloon_url = balloon_->notification().content_url(); | |
50 if (balloon_url.SchemeIs(chrome::kExtensionScheme)) { | |
51 is_extension_page_ = true; | |
52 site_instance_ = | |
53 balloon_->profile()->GetExtensionProcessManager()->GetSiteInstanceForURL( | |
54 balloon_url); | |
55 } else { | |
56 site_instance_ = SiteInstance::CreateSiteInstance(balloon_->profile()); | |
57 } | |
58 } | 50 } |
59 | 51 |
60 void BalloonViewHost::Shutdown() { | 52 void BalloonViewHost::Init(gfx::NativeView parent_native_view) { |
61 if (render_view_host_) { | 53 parent_native_view_ = parent_native_view; |
62 render_view_host_->Shutdown(); | 54 BalloonHost::Init(); |
63 render_view_host_ = NULL; | |
64 } | |
65 } | 55 } |
66 | 56 |
67 WebPreferences BalloonViewHost::GetWebkitPrefs() { | 57 void BalloonViewHost::InitRenderWidgetHostView() { |
68 WebPreferences prefs; | 58 DCHECK(render_view_host_); |
69 prefs.allow_scripts_to_close_windows = true; | |
70 return prefs; | |
71 } | |
72 | 59 |
73 void BalloonViewHost::Close(RenderViewHost* render_view_host) { | 60 render_widget_host_view_ = |
74 balloon_->CloseByScript(); | 61 RenderWidgetHostView::CreateViewForWidget(render_view_host_); |
75 } | |
76 | |
77 void BalloonViewHost::RenderViewCreated(RenderViewHost* render_view_host) { | |
78 render_view_host->Send(new ViewMsg_EnablePreferredSizeChangedMode( | |
79 render_view_host->routing_id())); | |
80 } | |
81 | |
82 void BalloonViewHost::RendererReady(RenderViewHost* /* render_view_host */) { | |
83 should_notify_on_disconnect_ = true; | |
84 NotificationService::current()->Notify( | |
85 NotificationType::NOTIFY_BALLOON_CONNECTED, | |
86 Source<Balloon>(balloon_), NotificationService::NoDetails()); | |
87 } | |
88 | |
89 void BalloonViewHost::RendererGone(RenderViewHost* /* render_view_host */) { | |
90 if (!should_notify_on_disconnect_) | |
91 return; | |
92 | |
93 should_notify_on_disconnect_ = false; | |
94 NotificationService::current()->Notify( | |
95 NotificationType::NOTIFY_BALLOON_DISCONNECTED, | |
96 Source<Balloon>(balloon_), NotificationService::NoDetails()); | |
97 } | |
98 | |
99 // RenderViewHostDelegate::View methods implemented to allow links to | |
100 // open pages in new tabs. | |
101 void BalloonViewHost::CreateNewWindow(int route_id) { | |
102 delegate_view_helper_.CreateNewWindow( | |
103 route_id, balloon_->profile(), site_instance_.get(), | |
104 DOMUIFactory::GetDOMUIType(balloon_->notification().content_url()), NULL); | |
105 } | |
106 | |
107 void BalloonViewHost::ShowCreatedWindow(int route_id, | |
108 WindowOpenDisposition disposition, | |
109 const gfx::Rect& initial_pos, | |
110 bool user_gesture) { | |
111 // Don't allow pop-ups from notifications. | |
112 if (disposition == NEW_POPUP) | |
113 return; | |
114 | |
115 TabContents* contents = delegate_view_helper_.GetCreatedWindow(route_id); | |
116 if (contents) { | |
117 Browser* browser = BrowserList::GetLastActive(); | |
118 browser->AddTabContents(contents, disposition, initial_pos, user_gesture); | |
119 } | |
120 } | |
121 | |
122 void BalloonViewHost::UpdatePreferredSize(const gfx::Size& new_size) { | |
123 balloon_->SetContentPreferredSize(new_size); | |
124 } | |
125 | |
126 void BalloonViewHost::Init(gfx::NativeView parent_hwnd) { | |
127 DCHECK(!render_view_host_) << "BalloonViewHost already initialized."; | |
128 int64 session_storage_namespace_id = balloon_->profile()->GetWebKitContext()-> | |
129 dom_storage_context()->AllocateSessionStorageNamespaceId(); | |
130 RenderViewHost* rvh = new RenderViewHost(site_instance_.get(), | |
131 this, MSG_ROUTING_NONE, | |
132 session_storage_namespace_id); | |
133 render_view_host_ = rvh; | |
134 | |
135 if (is_extension_page_) { | |
136 rvh->AllowBindings(BindingsPolicy::EXTENSION); | |
137 } | |
138 | |
139 // Pointer is owned by the RVH. | |
140 RenderWidgetHostView* view = RenderWidgetHostView::CreateViewForWidget(rvh); | |
141 rvh->set_view(view); | |
142 | 62 |
143 // TODO(johnnyg): http://crbug.com/23954. Need a cross-platform solution. | 63 // TODO(johnnyg): http://crbug.com/23954. Need a cross-platform solution. |
144 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
145 RenderWidgetHostViewWin* view_win = | 65 RenderWidgetHostViewWin* view_win = |
146 static_cast<RenderWidgetHostViewWin*>(view); | 66 static_cast<RenderWidgetHostViewWin*>(render_widget_host_view_); |
147 | 67 |
148 // Create the HWND. | 68 // Create the HWND. |
149 HWND hwnd = view_win->Create(parent_hwnd); | 69 HWND hwnd = view_win->Create(parent_native_view_); |
150 view_win->ShowWindow(SW_SHOW); | 70 view_win->ShowWindow(SW_SHOW); |
151 Attach(hwnd); | 71 native_host_->Attach(hwnd); |
152 #elif defined(OS_LINUX) | 72 #elif defined(OS_LINUX) |
153 RenderWidgetHostViewGtk* view_gtk = | 73 RenderWidgetHostViewGtk* view_gtk = |
154 static_cast<RenderWidgetHostViewGtk*>(view); | 74 static_cast<RenderWidgetHostViewGtk*>(render_widget_host_view_); |
155 view_gtk->InitAsChild(); | 75 view_gtk->InitAsChild(); |
156 Attach(view_gtk->native_view()); | 76 native_host_->Attach(view_gtk->native_view()); |
157 #else | 77 #else |
158 NOTIMPLEMENTED(); | 78 NOTIMPLEMENTED(); |
159 #endif | 79 #endif |
160 | |
161 // Start up the renderer and point it at the balloon contents URL. | |
162 rvh->CreateRenderView(GetProfile()->GetRequestContext()); | |
163 rvh->NavigateToURL(balloon_->notification().content_url()); | |
164 initialized_ = true; | |
165 } | 80 } |
166 | |
167 void BalloonViewHost::ViewHierarchyChanged(bool is_add, views::View* parent, | |
168 views::View* child) { | |
169 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); | |
170 if (is_add && GetWidget() && !initialized_) | |
171 Init(GetWidget()->GetNativeView()); | |
172 } | |
OLD | NEW |