#include "atom/browser/api/atom_api_web_contents.h"
+#include "atom/browser/atom_browser_context.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
+#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
+#include "atom/common/node_includes.h"
+
namespace atom {
namespace api {
} // namespace
WebContents::WebContents(content::WebContents* web_contents)
- : content::WebContentsObserver(web_contents),
- web_contents_(web_contents) {
+ : content::WebContentsObserver(web_contents) {
+}
+
+WebContents::WebContents(const mate::Dictionary& options) {
+ content::WebContents::CreateParams params(AtomBrowserContext::Get());
+ bool is_guest;
+ if (options.Get("isGuest", &is_guest) && is_guest)
+ params.guest_delegate = this;
+
+ storage_.reset(content::WebContents::Create(params));
+ Observe(storage_.get());
+}
+
+WebContents::~WebContents() {
}
void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {
void WebContents::WebContentsDestroyed() {
// The RenderViewDeleted was not called when the WebContents is destroyed.
- RenderViewDeleted(web_contents_->GetRenderViewHost());
+ RenderViewDeleted(web_contents()->GetRenderViewHost());
Emit("destroyed");
}
+void WebContents::Destroy() {
+ if (storage_) {
+ Observe(nullptr);
+ storage_.reset();
+ }
+}
+
bool WebContents::IsAlive() const {
return web_contents() != NULL;
}
v8::Isolate* isolate) {
if (template_.IsEmpty())
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
+ .SetMethod("destroy", &WebContents::Destroy)
.SetMethod("isAlive", &WebContents::IsAlive)
.SetMethod("loadUrl", &WebContents::LoadURL)
.SetMethod("getUrl", &WebContents::GetURL)
}
// static
-mate::Handle<WebContents> WebContents::Create(
+mate::Handle<WebContents> WebContents::CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents) {
return mate::CreateHandle(isolate, new WebContents(web_contents));
}
+// static
+mate::Handle<WebContents> WebContents::Create(
+ v8::Isolate* isolate, const mate::Dictionary& options) {
+ return mate::CreateHandle(isolate, new WebContents(options));
+}
+
} // namespace api
} // namespace atom
+
+
+namespace {
+
+void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
+ v8::Handle<v8::Context> context, void* priv) {
+ v8::Isolate* isolate = context->GetIsolate();
+ mate::Dictionary dict(isolate, exports);
+ dict.SetMethod("create", &atom::api::WebContents::Create);
+}
+
+} // namespace
+
+NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_web_contents, Initialize)
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
#include "atom/browser/api/event_emitter.h"
+#include "content/public/browser/browser_plugin_guest_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "native_mate/handle.h"
+namespace mate {
+class Dictionary;
+}
+
namespace atom {
namespace api {
class WebContents : public mate::EventEmitter,
+ public content::BrowserPluginGuestDelegate,
public content::WebContentsObserver {
public:
- static mate::Handle<WebContents> Create(v8::Isolate* isolate,
- content::WebContents* web_contents);
+ // Create from an existing WebContents.
+ static mate::Handle<WebContents> CreateFrom(
+ v8::Isolate* isolate, content::WebContents* web_contents);
+
+ // Create a new WebContents.
+ static mate::Handle<WebContents> Create(
+ v8::Isolate* isolate, const mate::Dictionary& options);
+ void Destroy();
bool IsAlive() const;
void LoadURL(const GURL& url);
GURL GetURL() const;
protected:
explicit WebContents(content::WebContents* web_contents);
+ explicit WebContents(const mate::Dictionary& options);
+ ~WebContents();
// mate::Wrappable implementations:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
const base::ListValue& args,
IPC::Message* message);
- content::WebContents* web_contents_; // Weak.
+ // Stores the WebContents that managed by this class.
+ scoped_ptr<content::WebContents> storage_;
DISALLOW_COPY_AND_ASSIGN(WebContents);
};