Use views::View to represent web view.

This commit is contained in:
Cheng Zhao 2014-06-10 09:58:28 +08:00
parent 4e69bd1733
commit b0374b6a1d
2 changed files with 52 additions and 1 deletions

View file

@ -6,6 +6,8 @@
#include "content/public/browser/web_contents_view.h"
#include "ui/gfx/win/hwnd_util.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/view.h"
namespace brightray {
@ -15,6 +17,41 @@ const int kWindowInset = 100;
}
class ContainerView : public views::View {
public:
explicit ContainerView(InspectableWebContentsViewWin* web_contents_view)
: container_view_created_(false),
web_view_(new views::WebView(NULL)),
web_contents_view_(web_contents_view) {
web_view_->SetWebContents(
web_contents_view_->inspectable_web_contents()->GetWebContents());
}
private:
// views::View:
virtual void Layout() OVERRIDE {
web_view_->SetBounds(0, 0, width(), height());
}
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE {
View::ViewHierarchyChanged(details);
// We're not using child == this because a Widget may not be
// available when this is added to the hierarchy.
if (details.is_add && GetWidget() && !container_view_created_) {
container_view_created_ = true;
AddChildView(web_view_);
}
}
// True if the container view has already been created, or false otherwise.
bool container_view_created_;
views::WebView* web_view_;
InspectableWebContentsViewWin* web_contents_view_;
};
InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsImpl* inspectable_web_contents) {
return new InspectableWebContentsViewWin(inspectable_web_contents);
@ -22,7 +59,8 @@ InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsViewWin::InspectableWebContentsViewWin(
InspectableWebContentsImpl* inspectable_web_contents)
: inspectable_web_contents_(inspectable_web_contents) {
: inspectable_web_contents_(inspectable_web_contents),
container_(new ContainerView(this)) {
}
InspectableWebContentsViewWin::~InspectableWebContentsViewWin() {
@ -30,6 +68,10 @@ InspectableWebContentsViewWin::~InspectableWebContentsViewWin() {
DestroyWindow(devtools_window_->hwnd());
}
views::View* InspectableWebContentsViewWin::GetView() const {
return container_;
}
gfx::NativeView InspectableWebContentsViewWin::GetNativeView() const {
auto web_contents = inspectable_web_contents_->GetWebContents();
return web_contents->GetView()->GetNativeView();

View file

@ -6,8 +6,13 @@
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
namespace views {
class View;
}
namespace brightray {
class ContainerView;
class DevToolsWindow;
class InspectableWebContentsImpl;
@ -17,6 +22,8 @@ class InspectableWebContentsViewWin : public InspectableWebContentsView {
InspectableWebContentsImpl* inspectable_web_contents_impl);
~InspectableWebContentsViewWin();
views::View* GetView() const;
virtual gfx::NativeView GetNativeView() const OVERRIDE;
virtual void ShowDevTools() OVERRIDE;
virtual void CloseDevTools() OVERRIDE;
@ -31,6 +38,8 @@ class InspectableWebContentsViewWin : public InspectableWebContentsView {
// Owns us.
InspectableWebContentsImpl* inspectable_web_contents_;
ContainerView* container_;
base::WeakPtr<DevToolsWindow> devtools_window_;
DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsViewWin);