Enable calling WebContents::openDevTools for BrowserWindow's WebContents

This commit is contained in:
Cheng Zhao 2015-06-05 15:41:03 +08:00
parent 67144aaf2a
commit 94d69777fa
5 changed files with 78 additions and 43 deletions

View file

@ -95,17 +95,21 @@ content::ServiceWorkerContext* GetServiceWorkerContext(
if (!context || !site_instance) if (!context || !site_instance)
return nullptr; return nullptr;
content::StoragePartition* storage_partition = auto storage_partition =
content::BrowserContext::GetStoragePartition( content::BrowserContext::GetStoragePartition(context, site_instance);
context, site_instance); if (!storage_partition)
return nullptr;
DCHECK(storage_partition);
return storage_partition->GetServiceWorkerContext(); return storage_partition->GetServiceWorkerContext();
} }
} // namespace } // namespace
WebContents::WebContents(brightray::InspectableWebContents* web_contents)
: WebContents(web_contents->GetWebContents()) {
inspectable_web_contents_ = web_contents;
}
WebContents::WebContents(content::WebContents* web_contents) WebContents::WebContents(content::WebContents* web_contents)
: CommonWebContentsDelegate(false), : CommonWebContentsDelegate(false),
content::WebContentsObserver(web_contents), content::WebContentsObserver(web_contents),
@ -113,7 +117,8 @@ WebContents::WebContents(content::WebContents* web_contents)
guest_opaque_(true), guest_opaque_(true),
guest_host_(nullptr), guest_host_(nullptr),
auto_size_enabled_(false), auto_size_enabled_(false),
is_full_page_plugin_(false) { is_full_page_plugin_(false),
inspectable_web_contents_(nullptr) {
} }
WebContents::WebContents(const mate::Dictionary& options) WebContents::WebContents(const mate::Dictionary& options)
@ -136,6 +141,7 @@ WebContents::WebContents(const mate::Dictionary& options)
auto web_contents = content::WebContents::Create(params); auto web_contents = content::WebContents::Create(params);
InitWithWebContents(web_contents, GetWindowFromGuest(web_contents)); InitWithWebContents(web_contents, GetWindowFromGuest(web_contents));
inspectable_web_contents_ = managed_web_contents();
Observe(GetWebContents()); Observe(GetWebContents());
} }
@ -525,25 +531,67 @@ void WebContents::ExecuteJavaScript(const base::string16& code) {
} }
void WebContents::OpenDevTools() { void WebContents::OpenDevTools() {
if (!inspectable_web_contents())
return;
inspectable_web_contents()->SetCanDock(false); inspectable_web_contents()->SetCanDock(false);
inspectable_web_contents()->ShowDevTools(); inspectable_web_contents()->ShowDevTools();
} }
void WebContents::CloseDevTools() { void WebContents::CloseDevTools() {
if (!inspectable_web_contents())
return;
inspectable_web_contents()->CloseDevTools(); inspectable_web_contents()->CloseDevTools();
} }
bool WebContents::IsDevToolsOpened() { bool WebContents::IsDevToolsOpened() {
if (!inspectable_web_contents())
return false;
return inspectable_web_contents()->IsDevToolsViewShowing(); return inspectable_web_contents()->IsDevToolsViewShowing();
} }
void WebContents::InspectElement(int x, int y) { void WebContents::InspectElement(int x, int y) {
if (!inspectable_web_contents())
return;
OpenDevTools(); OpenDevTools();
scoped_refptr<content::DevToolsAgentHost> agent( scoped_refptr<content::DevToolsAgentHost> agent(
content::DevToolsAgentHost::GetOrCreateFor(web_contents())); content::DevToolsAgentHost::GetOrCreateFor(web_contents()));
agent->InspectElement(x, y); agent->InspectElement(x, y);
} }
void WebContents::InspectServiceWorker() {
if (!inspectable_web_contents())
return;
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
if (agent_host->GetType() ==
content::DevToolsAgentHost::TYPE_SERVICE_WORKER) {
OpenDevTools();
inspectable_web_contents()->AttachTo(agent_host);
break;
}
}
}
void WebContents::HasServiceWorker(
const base::Callback<void(bool)>& callback) {
auto context = GetServiceWorkerContext(web_contents());
if (!context)
return;
context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(),
GURL::EmptyGURL(),
callback);
}
void WebContents::UnregisterServiceWorker(
const base::Callback<void(bool)>& callback) {
auto context = GetServiceWorkerContext(web_contents());
if (!context)
return;
context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
callback);
}
void WebContents::Undo() { void WebContents::Undo() {
web_contents()->Undo(); web_contents()->Undo();
} }
@ -669,38 +717,6 @@ bool WebContents::IsGuest() const {
return is_guest(); return is_guest();
} }
void WebContents::HasServiceWorker(
const base::Callback<void(bool)>& callback) {
auto context = GetServiceWorkerContext(web_contents());
if (!context)
return;
context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(),
GURL::EmptyGURL(),
callback);
}
void WebContents::UnregisterServiceWorker(
const base::Callback<void(bool)>& callback) {
auto context = GetServiceWorkerContext(web_contents());
if (!context)
return;
context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
callback);
}
void WebContents::InspectServiceWorker() {
for (const auto& agent_host : content::DevToolsAgentHost::GetOrCreateAll()) {
if (agent_host->GetType() ==
content::DevToolsAgentHost::TYPE_SERVICE_WORKER) {
OpenDevTools();
inspectable_web_contents()->AttachTo(agent_host);
break;
}
}
}
mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
if (template_.IsEmpty()) if (template_.IsEmpty())
@ -781,6 +797,12 @@ gfx::Size WebContents::GetDefaultSize() const {
} }
} }
// static
mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents) {
return mate::CreateHandle(isolate, new WebContents(web_contents));
}
// static // static
mate::Handle<WebContents> WebContents::CreateFrom( mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents) { v8::Isolate* isolate, content::WebContents* web_contents) {

View file

@ -52,6 +52,8 @@ class WebContents : public mate::EventEmitter,
public content::GpuDataManagerObserver { public content::GpuDataManagerObserver {
public: public:
// Create from an existing WebContents. // Create from an existing WebContents.
static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents);
static mate::Handle<WebContents> CreateFrom( static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents); v8::Isolate* isolate, content::WebContents* web_contents);
@ -80,9 +82,9 @@ class WebContents : public mate::EventEmitter,
void CloseDevTools(); void CloseDevTools();
bool IsDevToolsOpened(); bool IsDevToolsOpened();
void InspectElement(int x, int y); void InspectElement(int x, int y);
void InspectServiceWorker();
void HasServiceWorker(const base::Callback<void(bool)>&); void HasServiceWorker(const base::Callback<void(bool)>&);
void UnregisterServiceWorker(const base::Callback<void(bool)>&); void UnregisterServiceWorker(const base::Callback<void(bool)>&);
void InspectServiceWorker();
// Editing commands. // Editing commands.
void Undo(); void Undo();
@ -113,11 +115,14 @@ class WebContents : public mate::EventEmitter,
// Returns whether this guest has an associated embedder. // Returns whether this guest has an associated embedder.
bool attached() const { return !!embedder_web_contents_; } bool attached() const { return !!embedder_web_contents_; }
content::WebContents* web_contents() const { // Returns the current InspectableWebContents object, nullptr will be returned
return content::WebContentsObserver::web_contents(); // if current WebContents can not beinspected, e.g. it is the devtools.
brightray::InspectableWebContents* inspectable_web_contents() const {
return inspectable_web_contents_;
} }
protected: protected:
explicit WebContents(brightray::InspectableWebContents* web_contents);
explicit WebContents(content::WebContents* web_contents); explicit WebContents(content::WebContents* web_contents);
explicit WebContents(const mate::Dictionary& options); explicit WebContents(const mate::Dictionary& options);
~WebContents(); ~WebContents();
@ -257,6 +262,10 @@ class WebContents : public mate::EventEmitter,
// Whether the guest view is inside a plugin document. // Whether the guest view is inside a plugin document.
bool is_full_page_plugin_; bool is_full_page_plugin_;
// Current InspectableWebContents object, can be nullptr for WebContents of
// devtools. It is a weak reference.
brightray::InspectableWebContents* inspectable_web_contents_;
DISALLOW_COPY_AND_ASSIGN(WebContents); DISALLOW_COPY_AND_ASSIGN(WebContents);
}; };

View file

@ -473,7 +473,7 @@ bool Window::IsVisibleOnAllWorkspaces() {
} }
mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const { mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const {
return WebContents::CreateFrom(isolate, window_->GetWebContents()); return WebContents::CreateFrom(isolate, window_->managed_web_contents());
} }
mate::Handle<WebContents> Window::GetDevToolsWebContents( mate::Handle<WebContents> Window::GetDevToolsWebContents(

View file

@ -38,7 +38,7 @@ class CommonWebContentsDelegate
// Returns the WebContents of devtools. // Returns the WebContents of devtools.
content::WebContents* GetDevToolsWebContents() const; content::WebContents* GetDevToolsWebContents() const;
brightray::InspectableWebContents* inspectable_web_contents() const { brightray::InspectableWebContents* managed_web_contents() const {
return web_contents_.get(); return web_contents_.get();
} }

View file

@ -212,6 +212,10 @@ class NativeWindow : public CommonWebContentsDelegate,
observers_.RemoveObserver(obs); observers_.RemoveObserver(obs);
} }
brightray::InspectableWebContents* inspectable_web_contents() const {
return managed_web_contents();
}
bool has_frame() const { return has_frame_; } bool has_frame() const { return has_frame_; }
bool is_html_api_fullscreen() const { return html_fullscreen_; } bool is_html_api_fullscreen() const { return html_fullscreen_; }