REVIEW: Inject devtools extensions API via browser

Behavior was changed in https://crbug.com/706169
This commit is contained in:
deepak1556 2017-10-30 17:06:13 +05:30 committed by Cheng Zhao
parent 531472bad4
commit 7a9892f151
4 changed files with 59 additions and 10 deletions

View file

@ -7,11 +7,13 @@
#include "brightray/browser/inspectable_web_contents_impl.h"
#include "base/guid.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
@ -26,6 +28,7 @@
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/user_agent.h"
@ -597,6 +600,12 @@ void InspectableWebContentsImpl::ClearPreferences() {
update.Get()->Clear();
}
void InspectableWebContentsImpl::RegisterExtensionsAPI(
const std::string& origin,
const std::string& script) {
extensions_api_[origin + "/"] = script;
}
void InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend(
const std::string& message) {
std::string method;
@ -745,13 +754,43 @@ void InspectableWebContentsImpl::OnWebContentsFocused(
#endif
}
void InspectableWebContentsImpl::DidStartNavigationToPendingEntry(
const GURL& url,
content::ReloadType reload_type) {
frontend_host_.reset(content::DevToolsFrontendHost::Create(
web_contents()->GetMainFrame(),
base::Bind(&InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend,
base::Unretained(this))));
void InspectableWebContentsImpl::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
if (navigation_handle->IsInMainFrame()) {
if (navigation_handle->GetRenderFrameHost() ==
devtools_web_contents_->GetMainFrame() &&
frontend_host_) {
return;
}
frontend_host_.reset(content::DevToolsFrontendHost::Create(
web_contents()->GetMainFrame(),
base::Bind(
&InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend,
base::Unretained(this))));
return;
}
}
void InspectableWebContentsImpl::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (navigation_handle->IsInMainFrame() ||
!navigation_handle->GetURL().SchemeIs("chrome-extension") ||
!navigation_handle->HasCommitted())
return;
content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost();
auto origin = navigation_handle->GetURL().GetOrigin().spec();
auto it = extensions_api_.find(origin);
if (it == extensions_api_.end())
return;
// Injected Script from devtools frontend doesn't expose chrome,
// most likely bug in chromium.
base::ReplaceFirstSubstringAfterOffset(&it->second, 0, "var chrome",
"var chrome = window.chrome ");
auto script = base::StringPrintf("%s(\"%s\")", it->second.c_str(),
base::GenerateGUID().c_str());
// Invoking content::DevToolsFrontendHost::SetupExtensionsAPI(frame, script);
// should be enough, but it seems to be a noop currently.
frame->ExecuteJavaScriptForTests(base::UTF8ToUTF16(script));
}
void InspectableWebContentsImpl::OnURLFetchComplete(