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;
|
|
|
|
|
2013-05-23 05:49:13 +00:00
|
|
|
namespace webkit {
|
|
|
|
extern void SetGetFirstWindowContext(v8::Handle<v8::Context> (*)());
|
|
|
|
extern void SetIsValidWindowContext(bool (*)(v8::Handle<v8::Context>));
|
2013-04-20 05:05:23 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-23 05:49:13 +00:00
|
|
|
v8::Handle<v8::Context> GetFirstWindowContext() {
|
2013-04-20 05:05:23 +00:00
|
|
|
if (web_frames().size() == 0)
|
2013-05-23 05:49:13 +00:00
|
|
|
return v8::Handle<v8::Context>();
|
2013-04-20 05:05:23 +00:00
|
|
|
|
2013-05-23 05:49:13 +00:00
|
|
|
return web_frames()[0]->mainWorldScriptContext();
|
2013-04-20 05:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-05-23 05:49:13 +00:00
|
|
|
webkit::SetGetFirstWindowContext(GetFirstWindowContext);
|
|
|
|
webkit::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)
|
2013-04-23 12:57:14 +00:00
|
|
|
IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage)
|
2013-04-23 04:18:07 +00:00
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2013-04-23 12:57:14 +00:00
|
|
|
void AtomRenderViewObserver::OnBrowserMessage(const std::string& channel,
|
|
|
|
const base::ListValue& args) {
|
|
|
|
atom_bindings()->OnBrowserMessage(channel, args);
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 01:46:58 +00:00
|
|
|
} // namespace atom
|