2013-03-27 14:33:00 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
#include "browser/inspectable_web_contents_impl.h"
|
|
|
|
|
2013-03-27 14:33:00 +00:00
|
|
|
#include "browser/browser_client.h"
|
2013-04-08 17:53:53 +00:00
|
|
|
#include "browser/browser_context.h"
|
2013-03-27 14:33:00 +00:00
|
|
|
#include "browser/browser_main_parts.h"
|
2014-03-04 08:12:02 +00:00
|
|
|
#include "browser/inspectable_web_contents_delegate.h"
|
2013-03-14 13:03:50 +00:00
|
|
|
#include "browser/inspectable_web_contents_view.h"
|
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
#include "base/prefs/pref_registry_simple.h"
|
|
|
|
#include "base/prefs/pref_service.h"
|
2013-10-07 19:16:49 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2013-08-23 20:56:30 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2013-03-27 14:33:00 +00:00
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
|
|
|
#include "content/public/browser/devtools_client_host.h"
|
|
|
|
#include "content/public/browser/devtools_http_handler.h"
|
|
|
|
#include "content/public/browser/devtools_manager.h"
|
2013-03-27 14:59:40 +00:00
|
|
|
#include "content/public/browser/web_contents_view.h"
|
2013-03-27 15:19:15 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2013-03-27 14:33:00 +00:00
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-05-14 06:45:34 +00:00
|
|
|
const char kChromeUIDevToolsURL[] = "chrome-devtools://devtools/devtools.html";
|
2013-04-08 17:53:53 +00:00
|
|
|
const char kDockSidePref[] = "brightray.devtools.dockside";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
// Implemented separately on each platform.
|
2013-11-17 23:08:34 +00:00
|
|
|
InspectableWebContentsView* CreateInspectableContentsView(
|
|
|
|
InspectableWebContentsImpl* inspectable_web_contents_impl);
|
2013-03-14 13:03:50 +00:00
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) {
|
|
|
|
registry->RegisterStringPref(kDockSidePref, "bottom");
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
InspectableWebContentsImpl::InspectableWebContentsImpl(
|
|
|
|
content::WebContents* web_contents)
|
2014-03-04 08:12:02 +00:00
|
|
|
: web_contents_(web_contents),
|
|
|
|
delegate_(nullptr) {
|
2013-11-17 23:08:34 +00:00
|
|
|
auto context = static_cast<BrowserContext*>(
|
|
|
|
web_contents_->GetBrowserContext());
|
2013-04-08 17:53:53 +00:00
|
|
|
dock_side_ = context->prefs()->GetString(kDockSidePref);
|
|
|
|
|
2013-03-14 13:03:50 +00:00
|
|
|
view_.reset(CreateInspectableContentsView(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
InspectableWebContentsImpl::~InspectableWebContentsImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
InspectableWebContentsView* InspectableWebContentsImpl::GetView() const {
|
|
|
|
return view_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
content::WebContents* InspectableWebContentsImpl::GetWebContents() const {
|
|
|
|
return web_contents_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::ShowDevTools() {
|
2013-03-27 14:33:00 +00:00
|
|
|
if (!devtools_web_contents_) {
|
2013-12-02 17:38:24 +00:00
|
|
|
embedder_message_dispatcher_.reset(
|
|
|
|
new DevToolsEmbedderMessageDispatcher(this));
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
auto create_params = content::WebContents::CreateParams(
|
|
|
|
web_contents_->GetBrowserContext());
|
|
|
|
devtools_web_contents_.reset(content::WebContents::Create(create_params));
|
2013-10-10 18:07:20 +00:00
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// Work around http://crbug.com/279472.
|
|
|
|
devtools_web_contents_->GetView()->SetAllowOverlappingViews(true);
|
|
|
|
#endif
|
|
|
|
|
2013-03-27 14:33:00 +00:00
|
|
|
Observe(devtools_web_contents_.get());
|
|
|
|
devtools_web_contents_->SetDelegate(this);
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
agent_host_ = content::DevToolsAgentHost::GetOrCreateFor(
|
|
|
|
web_contents_->GetRenderViewHost());
|
|
|
|
frontend_host_.reset(
|
|
|
|
content::DevToolsClientHost::CreateDevToolsFrontendHost(
|
|
|
|
devtools_web_contents_.get(), this));
|
|
|
|
content::DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
|
|
|
|
agent_host_, frontend_host_.get());
|
2013-03-27 14:33:00 +00:00
|
|
|
|
2013-05-14 07:36:40 +00:00
|
|
|
GURL devtools_url(kChromeUIDevToolsURL);
|
2013-11-17 23:08:34 +00:00
|
|
|
devtools_web_contents_->GetController().LoadURL(
|
|
|
|
devtools_url,
|
|
|
|
content::Referrer(),
|
|
|
|
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
|
|
|
|
std::string());
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 08:12:02 +00:00
|
|
|
if (delegate_ && delegate_->DevToolsShow(dock_side_))
|
|
|
|
return;
|
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
view_->SetDockSide(dock_side_);
|
2013-03-14 13:03:50 +00:00
|
|
|
view_->ShowDevTools();
|
|
|
|
}
|
|
|
|
|
2013-12-09 12:34:44 +00:00
|
|
|
bool InspectableWebContentsImpl::IsDevToolsViewShowing() {
|
|
|
|
return devtools_web_contents_ && view_->IsDevToolsViewShowing();
|
2013-11-05 02:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
void InspectableWebContentsImpl::UpdateFrontendDockSide() {
|
2013-11-17 23:08:34 +00:00
|
|
|
auto javascript = base::StringPrintf(
|
|
|
|
"InspectorFrontendAPI.setDockSide(\"%s\")", dock_side_.c_str());
|
|
|
|
devtools_web_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
|
|
|
|
string16(), ASCIIToUTF16(javascript));
|
2013-04-08 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 14:33:00 +00:00
|
|
|
void InspectableWebContentsImpl::ActivateWindow() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::CloseWindow() {
|
2013-03-27 14:59:40 +00:00
|
|
|
view_->CloseDevTools();
|
|
|
|
devtools_web_contents_.reset();
|
|
|
|
web_contents_->GetView()->Focus();
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::MoveWindow(int x, int y) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::SetDockSide(const std::string& side) {
|
2014-03-04 08:12:02 +00:00
|
|
|
bool succeed = true;
|
|
|
|
if (delegate_ && delegate_->DevToolsSetDockSide(side, &succeed)) {
|
|
|
|
if (!succeed) // delegate failed to set dock side.
|
|
|
|
return;
|
|
|
|
} else if (!view_->SetDockSide(side)) {
|
2013-03-27 15:19:15 +00:00
|
|
|
return;
|
2014-03-04 08:12:02 +00:00
|
|
|
}
|
2013-03-27 15:19:15 +00:00
|
|
|
|
2013-04-08 17:53:53 +00:00
|
|
|
dock_side_ = side;
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
auto context = static_cast<BrowserContext*>(
|
|
|
|
web_contents_->GetBrowserContext());
|
2013-04-08 17:53:53 +00:00
|
|
|
context->prefs()->SetString(kDockSidePref, side);
|
|
|
|
|
|
|
|
UpdateFrontendDockSide();
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::OpenInNewTab(const std::string& url) {
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::SaveToFile(
|
|
|
|
const std::string& url, const std::string& content, bool save_as) {
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::AppendToFile(
|
|
|
|
const std::string& url, const std::string& content) {
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::RequestFileSystems() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::AddFileSystem() {
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::RemoveFileSystem(
|
|
|
|
const std::string& file_system_path) {
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::IndexPath(
|
|
|
|
int request_id, const std::string& file_system_path) {
|
2013-10-07 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsImpl::StopIndexing(int request_id) {
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::SearchInPath(
|
|
|
|
int request_id,
|
|
|
|
const std::string& file_system_path,
|
|
|
|
const std::string& query) {
|
2013-10-07 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 17:38:24 +00:00
|
|
|
void InspectableWebContentsImpl::DispatchOnEmbedder(
|
|
|
|
const std::string& message) {
|
|
|
|
embedder_message_dispatcher_->Dispatch(message);
|
|
|
|
}
|
|
|
|
|
2013-03-27 14:33:00 +00:00
|
|
|
void InspectableWebContentsImpl::InspectedContentsClosing() {
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::AboutToNavigateRenderView(
|
|
|
|
content::RenderViewHost* render_view_host) {
|
|
|
|
content::DevToolsClientHost::SetupDevToolsFrontendClient(
|
|
|
|
web_contents()->GetRenderViewHost());
|
2013-03-27 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::DidFinishLoad(int64 frame_id,
|
|
|
|
const GURL& validated_url,
|
|
|
|
bool is_main_frame,
|
|
|
|
content::RenderViewHost*) {
|
2013-04-08 17:53:53 +00:00
|
|
|
if (!is_main_frame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UpdateFrontendDockSide();
|
|
|
|
}
|
|
|
|
|
2013-03-27 14:33:00 +00:00
|
|
|
void InspectableWebContentsImpl::WebContentsDestroyed(content::WebContents*) {
|
2013-11-17 23:08:34 +00:00
|
|
|
content::DevToolsManager::GetInstance()->ClientHostClosing(
|
|
|
|
frontend_host_.get());
|
2013-03-27 14:33:00 +00:00
|
|
|
Observe(nullptr);
|
|
|
|
agent_host_ = nullptr;
|
|
|
|
frontend_host_.reset();
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
void InspectableWebContentsImpl::HandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
2013-03-27 14:33:00 +00:00
|
|
|
auto delegate = web_contents_->GetDelegate();
|
|
|
|
if (delegate)
|
|
|
|
delegate->HandleKeyboardEvent(source, event);
|
|
|
|
}
|
|
|
|
|
2013-11-17 23:08:34 +00:00
|
|
|
} // namespace brightray
|