2013-04-12 01:46:58 +00:00
|
|
|
// Copyright (c) 2013 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 "renderer/atom_render_view_observer.h"
|
|
|
|
|
2013-04-20 05:05:23 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
#include "common/api/api_messages.h"
|
2013-04-20 03:13:06 +00:00
|
|
|
#include "common/node_bindings.h"
|
2013-04-23 04:18:07 +00:00
|
|
|
#include "ipc/ipc_message_macros.h"
|
2013-04-22 08:27:34 +00:00
|
|
|
#include "renderer/api/atom_renderer_bindings.h"
|
2013-04-20 03:13:06 +00:00
|
|
|
#include "renderer/atom_renderer_client.h"
|
2013-04-20 05:05:23 +00:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
2013-04-12 01:46:58 +00:00
|
|
|
#include "v8/include/v8.h"
|
|
|
|
|
2013-04-20 05:05:23 +00:00
|
|
|
using WebKit::WebFrame;
|
|
|
|
|
|
|
|
namespace webkit_atom {
|
|
|
|
extern void SetNodeContext(v8::Persistent<v8::Context> context);
|
|
|
|
extern void SetEnterFirstWindowContext(bool (*func)());
|
|
|
|
extern void SetIsValidWindowContext(bool (*func)(v8::Handle<v8::Context>));
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2013-04-20 05:05:23 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::vector<WebFrame*>& web_frames() {
|
|
|
|
CR_DEFINE_STATIC_LOCAL(std::vector<WebFrame*>, frames, ());
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnterFirstWindowContext() {
|
|
|
|
if (web_frames().size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DCHECK(!web_frames()[0]->mainWorldScriptContext().IsEmpty());
|
|
|
|
web_frames()[0]->mainWorldScriptContext()->Enter();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValidWindowContext(v8::Handle<v8::Context> context) {
|
|
|
|
size_t size = web_frames().size();
|
2013-04-20 06:03:55 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
2013-04-20 05:05:23 +00:00
|
|
|
if (web_frames()[i]->mainWorldScriptContext() == context)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-20 06:03:55 +00:00
|
|
|
} // namespace
|
2013-04-20 05:05:23 +00:00
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
AtomRenderViewObserver::AtomRenderViewObserver(
|
2013-04-20 03:13:06 +00:00
|
|
|
content::RenderView* render_view,
|
|
|
|
AtomRendererClient* renderer_client)
|
|
|
|
: content::RenderViewObserver(render_view),
|
2013-04-22 13:32:48 +00:00
|
|
|
atom_bindings_(new AtomRendererBindings(render_view)),
|
2013-04-20 03:13:06 +00:00
|
|
|
renderer_client_(renderer_client) {
|
2013-04-20 05:05:23 +00:00
|
|
|
// Interact with dirty workarounds of extra node context in WebKit.
|
|
|
|
webkit_atom::SetEnterFirstWindowContext(EnterFirstWindowContext);
|
|
|
|
webkit_atom::SetIsValidWindowContext(IsValidWindowContext);
|
2013-04-12 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AtomRenderViewObserver::~AtomRenderViewObserver() {
|
|
|
|
}
|
|
|
|
|
2013-04-20 05:05:23 +00:00
|
|
|
void AtomRenderViewObserver::DidClearWindowObject(WebFrame* frame) {
|
|
|
|
// Remember the web frame.
|
|
|
|
web_frames().push_back(frame);
|
|
|
|
|
2013-04-20 03:13:06 +00:00
|
|
|
renderer_client_->node_bindings()->BindTo(frame);
|
2013-04-22 13:32:48 +00:00
|
|
|
atom_bindings()->BindToFrame(frame);
|
2013-04-12 01:46:58 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 05:05:23 +00:00
|
|
|
void AtomRenderViewObserver::FrameWillClose(WebFrame* frame) {
|
|
|
|
std::vector<WebFrame*>& vec = web_frames();
|
|
|
|
vec.erase(std::remove(vec.begin(), vec.end(), frame), vec.end());
|
|
|
|
}
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
|
|
|
|
bool handled = true;
|
|
|
|
IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message)
|
|
|
|
IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnRendererMessage)
|
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomRenderViewObserver::OnRendererMessage(const std::string& channel,
|
|
|
|
const base::ListValue& args) {
|
|
|
|
atom_bindings()->OnRendererMessage(channel, args);
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
} // namespace atom
|