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

Side by Side Diff: chrome/browser/extensions/extension_view.cc

Issue 92043: Refactor ExtensionView to support a UI-less extension instance.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 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) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-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/browser/extensions/extension_view.h" 5 #include "chrome/browser/extensions/extension_view.h"
6 6
7 #include "base/command_line.h" 7 #include "chrome/browser/extensions/extension_host.h"
8 #include "chrome/browser/browser.h"
9 #include "chrome/browser/character_encoding.h"
10 #include "chrome/browser/extensions/extension.h"
11 #include "chrome/browser/extensions/extension_message_service.h"
12 #include "chrome/browser/jsmessage_box_handler.h"
13 #include "chrome/browser/profile.h"
14 #include "chrome/browser/renderer_host/render_view_host.h" 8 #include "chrome/browser/renderer_host/render_view_host.h"
15 #include "chrome/browser/renderer_host/render_process_host.h"
16 #include "chrome/browser/renderer_host/render_widget_host.h"
17 #include "chrome/browser/renderer_host/render_widget_host_view.h" 9 #include "chrome/browser/renderer_host/render_widget_host_view.h"
18 #include "chrome/browser/tab_contents/site_instance.h" 10 #include "chrome/views/widget/widget.h"
19 #include "chrome/browser/tab_contents/web_contents.h"
20 #include "chrome/browser/tab_contents/tab_contents_view.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/pref_service.h"
24 #include "chrome/common/resource_bundle.h"
25 11
26 #include "grit/browser_resources.h" 12 #if defined(OS_WIN)
27 #include "grit/generated_resources.h" 13 #include "chrome/browser/renderer_host/render_widget_host_view_win.h"
14 #endif
28 15
29 #include "webkit/glue/context_menu.h" 16 ExtensionView::ExtensionView(ExtensionHost* host, Browser* browser,
30 17 const GURL& content_url)
31 ExtensionView::ExtensionView(Extension* extension, 18 : host_(host), browser_(browser), content_url_(content_url),
32 const GURL& url, 19 initialized_(false) {
33 SiteInstance* instance, 20 host_->set_view(this);
34 Browser* browser)
35 : HWNDHtmlView(url, this, false, instance),
36 extension_(extension),
37 browser_(browser),
38 did_stop_loading_(false),
39 pending_preferred_width_(0) {
40 } 21 }
41 22
42 ExtensionFunctionDispatcher* ExtensionView:: 23 ExtensionView::~ExtensionView() {
43 CreateExtensionFunctionDispatcher(RenderViewHost *render_view_host, 24 if (GetHWND())
44 const std::string& extension_id) { 25 Detach();
45 return new ExtensionFunctionDispatcher(render_view_host, browser_, 26 }
46 extension_id); 27
28 void ExtensionView::SetVisible(bool is_visible) {
29 HWNDView::SetVisible(is_visible);
30
31 // Also tell RenderWidgetHostView the new visibility. Despite its name, it is
32 // not part of the View heirarchy and does not know about the change unless we
33 // tell it.
34 if (render_view_host()->view()) {
35 if (is_visible)
36 render_view_host()->view()->Show();
37 else
38 render_view_host()->view()->Hide();
39 }
40 }
41
42 void ExtensionView::DidChangeBounds(const gfx::Rect& previous,
43 const gfx::Rect& current) {
44 // Propagate the new size to RenderWidgetHostView.
45 // We can't send size zero because RenderWidget DCHECKs that.
46 if (render_view_host()->view() && !current.IsEmpty())
47 render_view_host()->view()->SetSize(gfx::Size(width(), height()));
47 } 48 }
48 49
49 void ExtensionView::ShowIfCompletelyLoaded() { 50 void ExtensionView::ShowIfCompletelyLoaded() {
50 // We wait to show the ExtensionView until it has loaded and our parent has 51 // We wait to show the ExtensionView until it has loaded and our parent has
51 // given us a background. These can happen in different orders. 52 // given us a background. These can happen in different orders.
52 if (did_stop_loading_ && !render_view_host()->view()->background().empty()) { 53 if (host_->did_stop_loading() && render_view_host()->view() &&
54 !render_view_host()->view()->background().empty()) {
53 SetVisible(true); 55 SetVisible(true);
54 DidContentsPreferredWidthChange(pending_preferred_width_); 56 DidContentsPreferredWidthChange(pending_preferred_width_);
55 } 57 }
56 } 58 }
57 59
58 void ExtensionView::SetBackground(const SkBitmap& background) { 60 void ExtensionView::SetBackground(const SkBitmap& background) {
59 HWNDHtmlView::SetBackground(background); 61 if (initialized_ && render_view_host()->view()) {
60 ShowIfCompletelyLoaded(); 62 render_view_host()->view()->SetBackground(background);
61 } 63 } else {
62 64 pending_background_ = background;
63 void ExtensionView::DidStopLoading(RenderViewHost* render_view_host) { 65 }
64 render_view_host->WasResized();
65 did_stop_loading_ = true;
66 ShowIfCompletelyLoaded(); 66 ShowIfCompletelyLoaded();
67 } 67 }
68 68
69 void ExtensionView::DidContentsPreferredWidthChange(const int pref_width) { 69 void ExtensionView::DidContentsPreferredWidthChange(const int pref_width) {
70 // Don't actually do anything with this information until we have been shown. 70 // Don't actually do anything with this information until we have been shown.
71 // Size changes will not be honored by lower layers while we are hidden. 71 // Size changes will not be honored by lower layers while we are hidden.
72 if (!IsVisible()) { 72 if (!IsVisible()) {
73 pending_preferred_width_ = pref_width; 73 pending_preferred_width_ = pref_width;
74 } else if (pref_width > 0) { 74 } else if (pref_width > 0) {
75 set_preferred_size(gfx::Size(pref_width, height())); 75 set_preferred_size(gfx::Size(pref_width, height()));
76 SizeToPreferredSize(); 76 SizeToPreferredSize();
77 77
78 // TODO(rafaelw): This assumes that the extension view is a child of an 78 // TODO(rafaelw): This assumes that the extension view is a child of an
79 // ExtensionToolstrip, which is a child of the BookmarkBarView. There should 79 // ExtensionToolstrip, which is a child of the BookmarkBarView. There should
80 // be a way to do this where the ExtensionView doesn't have to know it's 80 // be a way to do this where the ExtensionView doesn't have to know it's
81 // containment hierarchy. 81 // containment hierarchy.
82 if (GetParent() != NULL && GetParent()->GetParent() != NULL) { 82 if (GetParent() != NULL && GetParent()->GetParent() != NULL) {
83 GetParent()->GetParent()->Layout(); 83 GetParent()->GetParent()->Layout();
84 } 84 }
85 85
86 SchedulePaint(); 86 SchedulePaint();
87 } 87 }
88 } 88 }
89 89
90 void ExtensionView::CreatingRenderer() { 90 void ExtensionView::ViewHierarchyChanged(bool is_add,
91 render_view_host()->AllowExtensionBindings(); 91 views::View *parent,
92 SetVisible(false); 92 views::View *child) {
93 } 93 if (is_add && GetWidget() && !initialized_) {
94 initialized_ = true;
94 95
95 void ExtensionView::RenderViewCreated(RenderViewHost* rvh) { 96 RenderWidgetHostView* view = RenderWidgetHostView::CreateViewForWidget(
96 URLRequestContext* context = rvh->process()->profile()->GetRequestContext(); 97 render_view_host());
97 ExtensionMessageService::GetInstance(context)->RegisterExtension(
98 extension_->id(), render_view_host()->process()->pid());
99 }
100 98
101 WebPreferences ExtensionView::GetWebkitPrefs() { 99 // TODO(mpcomplete): RWHV needs a cross-platform Init function.
102 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs(); 100 #if defined(OS_WIN)
103 bool isDomUI = true; 101 // Create the HWND. Note:
104 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, isDomUI); 102 // RenderWidgetHostHWND supports windowed plugins, but if we ever also
105 } 103 // wanted to support constrained windows with this, we would need an
104 // additional HWND to parent off of because windowed plugin HWNDs cannot
105 // exist in the same z-order as constrained windows.
106 RenderWidgetHostViewWin* view_win =
107 static_cast<RenderWidgetHostViewWin*>(view);
108 HWND hwnd = view_win->Create(GetWidget()->GetNativeView());
109 view_win->ShowWindow(SW_SHOW);
110 Attach(hwnd);
111 #else
112 NOTIMPLEMENTED();
113 #endif
106 114
107 void ExtensionView::RunJavaScriptMessage( 115 host_->CreateRenderView(content_url_, view);
108 const std::wstring& message, 116 SetVisible(false);
109 const std::wstring& default_prompt,
110 const GURL& frame_url,
111 const int flags,
112 IPC::Message* reply_msg,
113 bool* did_suppress_message) {
114 // Automatically cancel the javascript alert (otherwise the renderer hangs
115 // indefinitely).
116 *did_suppress_message = true;
117 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true, L"");
118 }
119 117
120 void ExtensionView::DidStartLoading(RenderViewHost* render_view_host) { 118 if (!pending_background_.empty()) {
121 static const StringPiece toolstrip_css( 119 render_view_host()->view()->SetBackground(pending_background_);
122 ResourceBundle::GetSharedInstance().GetRawDataResource( 120 pending_background_.reset();
123 IDR_EXTENSIONS_TOOLSTRIP_CSS)); 121 }
124 render_view_host->InsertCSSInWebFrame(L"", toolstrip_css.as_string());
125 }
126
127 RenderViewHostDelegate::View* ExtensionView::GetViewDelegate() const {
128 // TODO(erikkay) this is unfortunate. The interface declares that this method
129 // must be const (no good reason for it as far as I can tell) which means you
130 // can't return self without doing this const_cast. Either we need to change
131 // the interface, or we need to split out the view delegate into another
132 // object (which is how WebContents works).
133 return const_cast<ExtensionView*>(this);
134 }
135
136 void ExtensionView::CreateNewWindow(int route_id,
137 base::WaitableEvent* modal_dialog_event) {
138 delegate_view_helper_.CreateNewWindow(route_id, modal_dialog_event,
139 browser_->profile(), site_instance());
140 }
141
142 void ExtensionView::CreateNewWidget(int route_id, bool activatable) {
143 delegate_view_helper_.CreateNewWidget(route_id, activatable,
144 site_instance()->GetProcess());
145 }
146
147 void ExtensionView::ShowCreatedWindow(int route_id,
148 WindowOpenDisposition disposition,
149 const gfx::Rect& initial_pos,
150 bool user_gesture) {
151 WebContents* contents = delegate_view_helper_.GetCreatedWindow(route_id);
152 if (contents) {
153 browser_->AddTabContents(contents, disposition, initial_pos, user_gesture);
154 } 122 }
155 } 123 }
156
157 void ExtensionView::ShowCreatedWidget(int route_id,
158 const gfx::Rect& initial_pos) {
159 RenderWidgetHostView* widget_host_view =
160 delegate_view_helper_.GetCreatedWidget(route_id);
161 browser_->BrowserRenderWidgetShowing();
162 // TODO(erikkay): These two lines could be refactored with TabContentsView.
163 widget_host_view->InitAsPopup(render_view_host()->view(),
164 initial_pos);
165 widget_host_view->GetRenderWidgetHost()->Init();
166 }
167
168 void ExtensionView::ShowContextMenu(const ContextMenuParams& params) {
169 // TODO(erikkay) - This is a temporary hack. Show a menu here instead.
170 render_view_host()->InspectElementAt(params.x, params.y);
171 }
172
173 void ExtensionView::StartDragging(const WebDropData& drop_data) {
174 }
175
176 void ExtensionView::UpdateDragCursor(bool is_drop_target) {
177 }
178
179 void ExtensionView::TakeFocus(bool reverse) {
180 }
181
182 void ExtensionView::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
183 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_view.h ('k') | chrome/browser/extensions/extension_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698