Merge pull request #225 from atom/devtools-menu

Enable context menu in devtools
This commit is contained in:
Cheng Zhao 2014-04-05 03:30:47 +00:00
Родитель 663ab8e0bf 2369f6cc41
Коммит 839f875045
18 изменённых файлов: 319 добавлений и 46 удалений

Просмотреть файл

@ -32,6 +32,7 @@
'atom/common/api/lib/shell.coffee',
'atom/common/lib/init.coffee',
'atom/renderer/lib/init.coffee',
'atom/renderer/lib/inspector.coffee',
'atom/renderer/lib/override.coffee',
'atom/renderer/api/lib/ipc.coffee',
'atom/renderer/api/lib/remote.coffee',
@ -93,6 +94,8 @@
'atom/browser/browser_observer.h',
'atom/browser/devtools_delegate.cc',
'atom/browser/devtools_delegate.h',
'atom/browser/devtools_web_contents_observer.cc',
'atom/browser/devtools_web_contents_observer.h',
'atom/browser/native_window.cc',
'atom/browser/native_window.h',
'atom/browser/native_window_gtk.cc',

Просмотреть файл

@ -4,10 +4,10 @@
#include "atom/browser/api/atom_api_event.h"
#include "atom/browser/native_window.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/v8/node_common.h"
#include "atom/common/v8/native_type_conversions.h"
#include "content/public/browser/web_contents.h"
namespace atom {
@ -22,8 +22,6 @@ Event::Event()
}
Event::~Event() {
if (sender_ != NULL)
sender_->RemoveObserver(this);
}
// static
@ -44,16 +42,17 @@ v8::Handle<v8::Object> Event::CreateV8Object() {
return t->NewInstance(0, NULL);
}
void Event::SetSenderAndMessage(NativeWindow* sender, IPC::Message* message) {
void Event::SetSenderAndMessage(content::WebContents* sender,
IPC::Message* message) {
DCHECK(!sender_);
DCHECK(!message_);
sender_ = sender;
message_ = message;
sender_->AddObserver(this);
Observe(sender);
}
void Event::OnWindowClosed() {
void Event::WebContentsDestroyed(content::WebContents* web_contents) {
sender_ = NULL;
message_ = NULL;
}

Просмотреть файл

@ -5,11 +5,11 @@
#ifndef ATOM_BROWSER_API_ATOM_API_EVENT_H_
#define ATOM_BROWSER_API_ATOM_API_EVENT_H_
#include "atom/common/v8/scoped_persistent.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/strings/string16.h"
#include "atom/browser/native_window_observer.h"
#include "atom/common/v8/scoped_persistent.h"
#include "content/public/browser/web_contents_observer.h"
#include "vendor/node/src/node_object_wrap.h"
namespace IPC {
@ -18,12 +18,10 @@ class Message;
namespace atom {
class NativeWindow;
namespace api {
class Event : public node::ObjectWrap,
public NativeWindowObserver {
public content::WebContentsObserver {
public:
virtual ~Event();
@ -31,7 +29,7 @@ class Event : public node::ObjectWrap,
static v8::Handle<v8::Object> CreateV8Object();
// Pass the sender and message to be replied.
void SetSenderAndMessage(NativeWindow* sender, IPC::Message* message);
void SetSenderAndMessage(content::WebContents* sender, IPC::Message* message);
// Whether event.preventDefault() is called.
bool prevent_default() const { return prevent_default_; }
@ -39,8 +37,8 @@ class Event : public node::ObjectWrap,
protected:
Event();
// NativeWindowObserver implementations:
virtual void OnWindowClosed() OVERRIDE;
// content::WebContentsObserver implementations:
virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
private:
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
@ -52,7 +50,7 @@ class Event : public node::ObjectWrap,
static ScopedPersistent<v8::Function> constructor_template_;
// Replyer for the synchronous messages.
NativeWindow* sender_;
content::WebContents* sender_;
IPC::Message* message_;
bool prevent_default_;

Просмотреть файл

@ -503,6 +503,19 @@ void Window::IsCrashed(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(self->window_->GetWebContents()->IsCrashed());
}
// static
void Window::GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_WINDOW_AND_CHECK;
content::WebContents* web_contents = self->window_->GetDevToolsWebContents();
v8::Local<v8::Object> devtools = v8::Object::New();
devtools->Set(ToV8Value("processId"),
ToV8Value(web_contents->GetRenderProcessHost()->GetID()));
devtools->Set(ToV8Value("routingId"),
ToV8Value(web_contents->GetRoutingID()));
args.GetReturnValue().Set(devtools);
}
// static
void Window::LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_WINDOW_AND_CHECK;
@ -686,6 +699,8 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "getProcessId", GetProcessID);
NODE_SET_PROTOTYPE_METHOD(t, "isCrashed", IsCrashed);
NODE_SET_PROTOTYPE_METHOD(t, "getDevTools", GetDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "loadUrl", LoadURL);
NODE_SET_PROTOTYPE_METHOD(t, "getUrl", GetURL);
NODE_SET_PROTOTYPE_METHOD(t, "canGoBack", CanGoBack);

Просмотреть файл

@ -103,6 +103,9 @@ class Window : public EventEmitter,
static void GetProcessID(const v8::FunctionCallbackInfo<v8::Value>& args);
static void IsCrashed(const v8::FunctionCallbackInfo<v8::Value>& args);
// APIs for devtools.
static void GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
// APIs for NavigationController.
static void LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetURL(const v8::FunctionCallbackInfo<v8::Value>& args);

Просмотреть файл

@ -57,7 +57,7 @@ void AtomBrowserBindings::OnRendererMessageSync(
int routing_id,
const string16& channel,
const base::ListValue& args,
NativeWindow* sender,
content::WebContents* sender,
IPC::Message* message) {
v8::Locker locker(node_isolate);
v8::HandleScope handle_scope(node_isolate);

Просмотреть файл

@ -13,14 +13,16 @@ namespace base {
class ListValue;
}
namespace content {
class WebContents;
}
namespace IPC {
class Message;
}
namespace atom {
class NativeWindow;
class AtomBrowserBindings : public AtomBindings {
public:
AtomBrowserBindings();
@ -37,7 +39,7 @@ class AtomBrowserBindings : public AtomBindings {
int routing_id,
const string16& channel,
const base::ListValue& args,
NativeWindow* sender,
content::WebContents* sender,
IPC::Message* message);
private:

Просмотреть файл

@ -55,4 +55,11 @@ BrowserWindow.fromProcessIdAndRoutingId = (processId, routingId) ->
return window for window in windows when window.getProcessId() == processId and
window.getRoutingId() == routingId
BrowserWindow.fromDevTools = (processId, routingId) ->
windows = BrowserWindow.getAllWindows()
for window in windows
devtools = window.getDevTools()
return window if devtools.processId == processId and
devtools.routingId == routingId
module.exports = BrowserWindow

Просмотреть файл

@ -0,0 +1,64 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "atom/browser/devtools_web_contents_observer.h"
#include "atom/browser/api/atom_browser_bindings.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/native_window.h"
#include "atom/common/api/api_messages.h"
#include "base/logging.h"
#include "content/public/browser/render_process_host.h"
#include "ipc/ipc_message_macros.h"
namespace atom {
DevToolsWebContentsObserver::DevToolsWebContentsObserver(
NativeWindow* native_window,
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
inspected_window_(native_window) {
DCHECK(native_window);
}
DevToolsWebContentsObserver::~DevToolsWebContentsObserver() {
}
bool DevToolsWebContentsObserver::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(DevToolsWebContentsObserver, message)
IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage)
IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync,
OnRendererMessageSync)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void DevToolsWebContentsObserver::OnRendererMessage(
const string16& channel,
const base::ListValue& args) {
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
web_contents()->GetRenderProcessHost()->GetID(),
web_contents()->GetRoutingID(),
channel,
args);
}
void DevToolsWebContentsObserver::OnRendererMessageSync(
const string16& channel,
const base::ListValue& args,
IPC::Message* reply_msg) {
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessageSync(
web_contents()->GetRenderProcessHost()->GetID(),
web_contents()->GetRoutingID(),
channel,
args,
web_contents(),
reply_msg);
}
} // namespace atom

Просмотреть файл

@ -0,0 +1,41 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_DEVTOOLS_WEB_CONTENTS_OBSERVER_H_
#define ATOM_BROWSER_DEVTOOLS_WEB_CONTENTS_OBSERVER_H_
#include "content/public/browser/web_contents_observer.h"
namespace base {
class ListValue;
}
namespace atom {
class NativeWindow;
class DevToolsWebContentsObserver : public content::WebContentsObserver {
public:
DevToolsWebContentsObserver(NativeWindow* native_window,
content::WebContents* web_contents);
virtual ~DevToolsWebContentsObserver();
protected:
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
void OnRendererMessage(const string16& channel,
const base::ListValue& args);
void OnRendererMessageSync(const string16& channel,
const base::ListValue& args,
IPC::Message* reply_msg);
private:
NativeWindow* inspected_window_;
DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
};
} // namespace atom
#endif // ATOM_BROWSER_DEVTOOLS_WEB_CONTENTS_OBSERVER_H_

Просмотреть файл

@ -98,6 +98,7 @@ ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event, processId, routingId) ->
try
BrowserWindow = require 'browser-window'
window = BrowserWindow.fromProcessIdAndRoutingId processId, routingId
window = BrowserWindow.fromDevTools processId, routingId unless window?
event.returnValue = valueToMeta processId, routingId, window
catch e
event.returnValue = errorToMeta e

Просмотреть файл

@ -8,20 +8,26 @@
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/prefs/pref_service.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "atom/browser/api/atom_browser_bindings.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_javascript_dialog_manager.h"
#include "atom/browser/browser.h"
#include "atom/browser/devtools_delegate.h"
#include "atom/browser/devtools_web_contents_observer.h"
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/window_list.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/atom_version.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/json/json_writer.h"
#include "base/prefs/pref_service.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/navigation_entry.h"
@ -33,9 +39,6 @@
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/renderer_preferences.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/atom_version.h"
#include "atom/common/options_switches.h"
#include "ipc/ipc_message_macros.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/point.h"
@ -194,8 +197,12 @@ bool NativeWindow::HasModalDialog() {
void NativeWindow::OpenDevTools() {
if (devtools_window_) {
devtools_window_->Focus(true);
devtools_web_contents_observer_.reset(new DevToolsWebContentsObserver(
this, devtools_window_->GetWebContents()));
} else {
inspectable_web_contents()->ShowDevTools();
devtools_web_contents_observer_.reset(new DevToolsWebContentsObserver(
this, GetDevToolsWebContents()));
#if defined(OS_MACOSX)
// Temporary fix for flashing devtools, try removing this after upgraded to
// Chrome 32.
@ -514,6 +521,41 @@ bool NativeWindow::DevToolsShow(std::string* dock_side) {
return false;
}
void NativeWindow::DevToolsSaveToFile(const std::string& url,
const std::string& content,
bool save_as) {
base::FilePath path;
PathsMap::iterator it = saved_files_.find(url);
if (it != saved_files_.end() && !save_as) {
path = it->second;
} else {
if (!file_dialog::ShowSaveDialog(this, url, base::FilePath(url), &path))
return;
}
saved_files_[url] = path;
file_util::WriteFile(path, content.data(), content.size());
// Notify devtools.
base::StringValue url_value(url);
CallDevToolsFunction("InspectorFrontendAPI.savedURL", &url_value);
// TODO(zcbenz): In later Chrome we need to call canceledSaveURL when the save
// failed.
}
void NativeWindow::DevToolsAppendToFile(const std::string& url,
const std::string& content) {
PathsMap::iterator it = saved_files_.find(url);
if (it == saved_files_.end())
return;
file_util::AppendToFile(it->second, content.data(), content.size());
// Notify devtools.
base::StringValue url_value(url);
CallDevToolsFunction("InspectorFrontendAPI.appendedToURL", &url_value);
}
void NativeWindow::ScheduleUnresponsiveEvent(int ms) {
window_unresposive_closure_.Reset(
base::Bind(&NativeWindow::NotifyWindowUnresponsive,
@ -542,6 +584,30 @@ void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
callback.Run(data);
}
void NativeWindow::CallDevToolsFunction(const std::string& function_name,
const base::Value* arg1,
const base::Value* arg2,
const base::Value* arg3) {
std::string params;
if (arg1) {
std::string json;
base::JSONWriter::Write(arg1, &json);
params.append(json);
if (arg2) {
base::JSONWriter::Write(arg2, &json);
params.append(", " + json);
if (arg3) {
base::JSONWriter::Write(arg3, &json);
params.append(", " + json);
}
}
}
base::string16 javascript =
base::ASCIIToUTF16(function_name + "(" + params + ");");
GetDevToolsWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
string16(), javascript);
}
void NativeWindow::OnRendererMessage(const string16& channel,
const base::ListValue& args) {
AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
@ -559,7 +625,7 @@ void NativeWindow::OnRendererMessageSync(const string16& channel,
GetWebContents()->GetRoutingID(),
channel,
args,
this,
GetWebContents(),
reply_msg);
}

Просмотреть файл

@ -5,6 +5,7 @@
#ifndef ATOM_BROWSER_NATIVE_WINDOW_H_
#define ATOM_BROWSER_NATIVE_WINDOW_H_
#include <map>
#include <string>
#include <vector>
@ -41,14 +42,11 @@ class Rect;
class Size;
}
namespace IPC {
class Message;
}
namespace atom {
class AtomJavaScriptDialogManager;
class DevToolsDelegate;
class DevToolsWebContentsObserver;
struct DraggableRegion;
class NativeWindow : public brightray::DefaultWebContentsDelegate,
@ -240,6 +238,11 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
virtual bool DevToolsSetDockSide(const std::string& dock_side,
bool* succeed) OVERRIDE;
virtual bool DevToolsShow(std::string* dock_side) OVERRIDE;
virtual void DevToolsSaveToFile(const std::string& url,
const std::string& content,
bool save_as) OVERRIDE;
virtual void DevToolsAppendToFile(const std::string& url,
const std::string& content) OVERRIDE;
// Whether window has standard frame.
bool has_frame_;
@ -254,6 +257,12 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
// Dispatch unresponsive event to observers.
void NotifyWindowUnresponsive();
// Call a function in devtools.
void CallDevToolsFunction(const std::string& function_name,
const base::Value* arg1 = NULL,
const base::Value* arg2 = NULL,
const base::Value* arg3 = NULL);
// Called when CapturePage has done.
void OnCapturePageDone(const CapturePageCallback& callback,
bool succeed,
@ -288,11 +297,18 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
base::WeakPtrFactory<NativeWindow> weak_factory_;
base::WeakPtr<NativeWindow> devtools_window_;
scoped_ptr<DevToolsDelegate> devtools_delegate_;
// WebContentsObserver for the WebContents of devtools.
scoped_ptr<DevToolsWebContentsObserver> devtools_web_contents_observer_;
scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
// Maps url to file path, used by the file requests sent from devtools.
typedef std::map<std::string, base::FilePath> PathsMap;
PathsMap saved_files_;
DISALLOW_COPY_AND_ASSIGN(NativeWindow);
};

Просмотреть файл

@ -28,9 +28,6 @@ const char* kSecurityManualEnableIframe = "manual-enable-iframe";
const char* kSecurityDisable = "disable";
const char* kSecurityEnableNodeIntegration = "enable-node-integration";
// Scheme used by devtools
const char* kChromeDevToolsScheme = "chrome-devtools";
} // namespace
AtomRendererClient::AtomRendererClient()
@ -159,10 +156,6 @@ bool AtomRendererClient::ShouldFork(WebKit::WebFrame* frame,
bool AtomRendererClient::IsNodeBindingEnabled(WebKit::WebFrame* frame) {
if (node_integration_ == DISABLE)
return false;
// Do not pollute devtools.
else if (frame != NULL &&
GURL(frame->document().url()).SchemeIs(kChromeDevToolsScheme))
return false;
// Node integration is enabled in main frame unless explictly disabled.
else if (frame == main_frame_)
return true;

Просмотреть файл

@ -1,4 +1,5 @@
path = require 'path'
url = require 'url'
Module = require 'module'
# Expose information of current process.
@ -42,5 +43,9 @@ else
global.__filename = __filename
global.__dirname = __dirname
# Override default web functions.
require path.join(__dirname, 'override')
if location.protocol is 'chrome-devtools:'
# Override some inspector APIs.
require path.join(__dirname, 'inspector')
else
# Override default web functions.
require path.join(__dirname, 'override')

Просмотреть файл

@ -0,0 +1,61 @@
window.onload = ->
# Use menu API to show context menu.
WebInspector.ContextMenu.prototype.show = ->
menuObject = @_buildDescriptor()
if menuObject.length
WebInspector._contextMenu = this
createMenu(menuObject, @_event)
@_event.consume()
# Use dialog API to override file chooser dialog.
WebInspector.createFileSelectorElement = (callback) ->
fileSelectorElement = document.createElement 'span'
fileSelectorElement.style.display = 'none'
fileSelectorElement.click = showFileChooserDialog.bind this, callback
return fileSelectorElement
convertToMenuTemplate = (items) ->
template = []
for item in items
do (item) ->
transformed =
if item.type is 'subMenu'
type: 'submenu'
label: item.label
enabled: item.enabled
submenu: convertToMenuTemplate item.subItems
else if item.type is 'separator'
type: 'separator'
else if item.type is 'checkbox'
type: 'checkbox'
label: item.label
enabled: item.enabled
checked: item.checked
else
type: 'normal'
label: item.label
enabled: item.enabled
if item.id?
transformed.click = -> WebInspector.contextMenuItemSelected item.id
template.push transformed
template
createMenu = (items, event) ->
remote = require 'remote'
Menu = remote.require 'menu'
menu = Menu.buildFromTemplate convertToMenuTemplate(items)
menu.popup remote.getCurrentWindow()
event.consume true
showFileChooserDialog = (callback) ->
remote = require 'remote'
dialog = remote.require 'dialog'
dialog.showOpenDialog remote.getCurrentWindow(), null, (files) ->
callback pathToHtml5FileObject(files[0]) if files?
pathToHtml5FileObject = (path) ->
fs = require 'fs'
blob = new Blob([fs.readFileSync(path)])
blob.name = path
blob

Просмотреть файл

@ -27,7 +27,6 @@ def main():
run_script('coffeelint.py')
run_script('build.py')
run_script('test.py', ['--ci'])
run_script('create-dist.py')
def run_script(script, args=[]):

2
vendor/brightray поставляемый

@ -1 +1 @@
Subproject commit 794cc6b6a6d269eacce0bbaf930d10f642e7a907
Subproject commit dab731f2329285a63c0c2cff30793a34fde91c39