electron/atom/browser/api/atom_api_web_contents.h

288 lines
10 KiB
C
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2014-04-24 08:45:25 +00:00
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
2014-10-24 12:57:44 +00:00
#include <string>
#include <vector>
2014-10-24 12:57:44 +00:00
2015-06-24 06:36:05 +00:00
#include "atom/browser/api/trackable_object.h"
2015-06-05 05:49:12 +00:00
#include "atom/browser/common_web_contents_delegate.h"
#include "content/public/browser/browser_plugin_guest_delegate.h"
#include "content/public/common/favicon_url.h"
2014-04-25 03:22:51 +00:00
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/gpu_data_manager_observer.h"
2014-04-24 08:45:25 +00:00
#include "native_mate/handle.h"
#include "ui/gfx/image/image.h"
2014-04-24 08:45:25 +00:00
2014-11-02 15:32:33 +00:00
namespace brightray {
2014-11-03 11:45:09 +00:00
class InspectableWebContents;
2014-11-02 14:34:22 +00:00
}
namespace mate {
2015-06-05 09:01:17 +00:00
class Arguments;
class Dictionary;
}
2014-04-24 08:45:25 +00:00
namespace atom {
namespace api {
// A struct of parameters for SetSize(). The parameters are all declared as
// scoped pointers since they are all optional. Null pointers indicate that the
// parameter has not been provided, and the last used value should be used. Note
// that when |enable_auto_size| is true, providing |normal_size| is not
// meaningful. This is because the normal size of the guestview is overridden
// whenever autosizing occurs.
struct SetSizeParams {
SetSizeParams() {}
~SetSizeParams() {}
scoped_ptr<bool> enable_auto_size;
scoped_ptr<gfx::Size> min_size;
scoped_ptr<gfx::Size> max_size;
scoped_ptr<gfx::Size> normal_size;
};
2015-06-24 09:58:12 +00:00
class WebContents : public mate::TrackableObject<WebContents>,
public content::BrowserPluginGuestDelegate,
2015-06-05 05:49:12 +00:00
public CommonWebContentsDelegate,
public content::WebContentsObserver,
public content::GpuDataManagerObserver {
2014-04-24 08:45:25 +00:00
public:
2015-06-10 12:07:14 +00:00
// For node.js callback function type: function(error, buffer)
typedef base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>
PrintToPDFCallback;
2015-06-10 03:34:16 +00:00
// Create from an existing WebContents.
static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents);
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);
2014-04-24 08:45:25 +00:00
void Destroy();
2014-04-25 03:22:51 +00:00
bool IsAlive() const;
2015-06-23 08:16:10 +00:00
int GetID() const;
bool Equal(const WebContents* web_contents) const;
void LoadURL(const GURL& url, const mate::Dictionary& options);
2014-06-28 11:36:57 +00:00
base::string16 GetTitle() const;
bool IsLoading() const;
bool IsWaitingForResponse() const;
void Stop();
2015-04-26 13:28:30 +00:00
void ReloadIgnoringCache();
void GoBack();
void GoForward();
2015-05-11 08:44:01 +00:00
void GoToOffset(int offset);
bool IsCrashed() const;
2014-10-24 12:57:44 +00:00
void SetUserAgent(const std::string& user_agent);
2014-10-24 13:04:50 +00:00
void InsertCSS(const std::string& css);
2014-06-28 11:36:57 +00:00
void ExecuteJavaScript(const base::string16& code);
2015-06-05 09:01:17 +00:00
void OpenDevTools(mate::Arguments* args);
void CloseDevTools();
bool IsDevToolsOpened();
2015-06-05 09:01:17 +00:00
void ToggleDevTools();
2015-04-22 07:30:10 +00:00
void InspectElement(int x, int y);
void InspectServiceWorker();
v8::Local<v8::Value> Session(v8::Isolate* isolate);
void HasServiceWorker(const base::Callback<void(bool)>&);
void UnregisterServiceWorker(const base::Callback<void(bool)>&);
2015-06-15 13:40:49 +00:00
void SetAudioMuted(bool muted);
bool IsAudioMuted();
void Print(mate::Arguments* args);
2015-01-19 20:09:47 +00:00
// Print current page as PDF.
2015-06-10 03:34:16 +00:00
void PrintToPDF(const base::DictionaryValue& setting,
2015-06-13 08:02:16 +00:00
const PrintToPDFCallback& callback);
2015-01-19 20:09:47 +00:00
// Editing commands.
void Undo();
void Redo();
void Cut();
void Copy();
void Paste();
void PasteAndMatchStyle();
2015-01-19 20:09:47 +00:00
void Delete();
void SelectAll();
void Unselect();
void Replace(const base::string16& word);
void ReplaceMisspelling(const base::string16& word);
// Sending messages to browser.
2014-06-28 11:36:57 +00:00
bool SendIPCMessage(const base::string16& channel,
const base::ListValue& args);
// Used to toggle autosize mode for this GuestView, and set both the automatic
// and normal sizes.
void SetSize(const SetSizeParams& params);
2014-10-24 08:48:21 +00:00
// Sets the transparency of the guest.
void SetAllowTransparency(bool allow);
2015-06-05 07:18:15 +00:00
bool IsGuest() const;
2014-10-24 08:09:18 +00:00
// Returns whether this guest has an associated embedder.
bool attached() const { return !!embedder_web_contents_; }
// Returns the current InspectableWebContents object, nullptr will be returned
// if current WebContents can not beinspected, e.g. it is the devtools.
brightray::InspectableWebContents* inspectable_web_contents() const {
return inspectable_web_contents_;
2014-10-23 15:08:48 +00:00
}
2014-04-24 08:45:25 +00:00
protected:
explicit WebContents(brightray::InspectableWebContents* web_contents);
2014-04-24 08:45:25 +00:00
explicit WebContents(content::WebContents* web_contents);
explicit WebContents(const mate::Dictionary& options);
~WebContents();
2014-04-24 08:45:25 +00:00
// mate::Wrappable:
2014-12-16 01:15:56 +00:00
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
2014-10-23 15:08:48 +00:00
v8::Isolate* isolate) override;
2014-04-25 03:22:51 +00:00
2014-10-24 10:06:32 +00:00
// content::WebContentsDelegate:
bool AddMessageToConsole(content::WebContents* source,
int32 level,
const base::string16& message,
int32 line_no,
const base::string16& source_id) override;
2014-10-25 05:21:09 +00:00
bool ShouldCreateWebContents(
content::WebContents* web_contents,
int route_id,
2015-03-10 22:54:21 +00:00
int main_frame_route_id,
2014-10-25 05:21:09 +00:00
WindowContainerType window_container_type,
const base::string16& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override;
void CloseContents(content::WebContents* source) override;
2014-10-24 13:46:47 +00:00
content::WebContents* OpenURLFromTab(
2014-10-24 10:06:32 +00:00
content::WebContents* source,
const content::OpenURLParams& params) override;
2014-10-24 13:46:47 +00:00
void HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) override;
void EnterFullscreenModeForTab(content::WebContents* source,
const GURL& origin) override;
void ExitFullscreenModeForTab(content::WebContents* source) override;
2014-10-24 10:06:32 +00:00
// content::WebContentsObserver:
void RenderViewDeleted(content::RenderViewHost*) override;
void RenderProcessGone(base::TerminationStatus status) override;
2015-04-29 13:49:31 +00:00
void DocumentLoadedInFrame(
content::RenderFrameHost* render_frame_host) override;
void DidFinishLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url) override;
void DidFailLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) override;
2015-03-14 03:28:30 +00:00
void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description) override;
2015-05-22 07:24:34 +00:00
void DidStartLoading() override;
void DidStopLoading() override;
void DidGetResourceResponseStart(
const content::ResourceRequestDetails& details) override;
void DidGetRedirectForResourceRequest(
2015-03-10 22:54:21 +00:00
content::RenderFrameHost* render_frame_host,
2014-10-24 15:05:25 +00:00
const content::ResourceRedirectDetails& details) override;
void DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) override;
bool OnMessageReceived(const IPC::Message& message) override;
void RenderViewReady() override;
void WebContentsDestroyed() override;
void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) override;
2015-04-05 09:20:42 +00:00
void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override;
void DidUpdateFaviconURL(
const std::vector<content::FaviconURL>& urls) override;
void PluginCrashed(const base::FilePath& plugin_path,
base::ProcessId plugin_pid) override;
2014-04-24 08:45:25 +00:00
// content::BrowserPluginGuestDelegate:
void DidAttach(int guest_proxy_routing_id) final;
2015-03-13 23:33:06 +00:00
content::WebContents* GetOwnerWebContents() const final;
2015-04-21 10:56:08 +00:00
void GuestSizeChanged(const gfx::Size& new_size) final;
2015-05-22 07:24:34 +00:00
void SetGuestHost(content::GuestHost* guest_host) final;
void WillAttach(content::WebContents* embedder_web_contents,
2015-03-10 22:54:21 +00:00
int element_instance_id,
bool is_full_page_plugin) final;
// content::GpuDataManagerObserver:
void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;
2014-04-24 08:45:25 +00:00
private:
// Called when received a message from renderer.
2014-06-28 11:36:57 +00:00
void OnRendererMessage(const base::string16& channel,
const base::ListValue& args);
// Called when received a synchronous message from renderer.
2014-06-28 11:36:57 +00:00
void OnRendererMessageSync(const base::string16& channel,
const base::ListValue& args,
IPC::Message* message);
// This method is invoked when the contents auto-resized to give the container
// an opportunity to match it if it wishes.
//
// This gives the derived class an opportunity to inform its container element
// or perform other actions.
void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
const gfx::Size& new_size);
// Returns the default size of the guestview.
gfx::Size GetDefaultSize() const;
v8::Global<v8::Value> session_;
2014-10-24 08:48:21 +00:00
// Stores whether the contents of the guest can be transparent.
bool guest_opaque_;
// The WebContents that attaches this guest view.
content::WebContents* embedder_web_contents_;
// The size of the container element.
gfx::Size element_size_;
// The size of the guest content. Note: In autosize mode, the container
// element may not match the size of the guest.
gfx::Size guest_size_;
2015-05-22 07:24:34 +00:00
// A pointer to the guest_host.
content::GuestHost* guest_host_;
2015-03-13 23:33:06 +00:00
// Indicates whether autosize mode is enabled or not.
bool auto_size_enabled_;
// The maximum size constraints of the container element in autosize mode.
gfx::Size max_auto_size_;
// The minimum size constraints of the container element in autosize mode.
gfx::Size min_auto_size_;
// The size that will be used when autosize mode is disabled.
gfx::Size normal_size_;
// Whether the guest view is inside a plugin document.
bool is_full_page_plugin_;
// Current InspectableWebContents object, can be nullptr for WebContents of
// devtools. It is a weak reference.
brightray::InspectableWebContents* inspectable_web_contents_;
2014-04-24 08:45:25 +00:00
DISALLOW_COPY_AND_ASSIGN(WebContents);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_