electron/atom/browser/api/atom_api_web_contents.h

304 lines
11 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
#include "atom/browser/api/frame_subscriber.h"
2015-10-14 04:41:31 +00:00
#include "atom/browser/api/save_page_handler.h"
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"
2014-04-25 03:22:51 +00:00
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/favicon_url.h"
#include "content/common/cursors/webcursor.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
namespace blink {
struct WebDeviceEmulationParams;
}
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 {
struct SetSizeParams;
2015-07-14 19:13:25 +00:00
class AtomBrowserContext;
class WebViewGuestDelegate;
2014-04-24 08:45:25 +00:00
namespace api {
2015-06-24 09:58:12 +00:00
class WebContents : public mate::TrackableObject<WebContents>,
2015-06-05 05:49:12 +00:00
public CommonWebContentsDelegate,
2015-06-25 12:28:07 +00:00
public content::WebContentsObserver {
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)
using PrintToPDFCallback =
base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
2015-06-10 03:34:16 +00:00
// 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);
2014-04-24 08:45:25 +00:00
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);
void DownloadURL(const GURL& url);
GURL GetURL() const;
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);
2015-07-22 04:25:10 +00:00
std::string GetUserAgent();
2014-10-24 13:04:50 +00:00
void InsertCSS(const std::string& css);
2015-10-14 04:41:31 +00:00
bool SavePage(const base::FilePath& full_file_path,
const content::SavePageType& save_type,
const SavePageHandler::SavePageCallback& callback);
2015-06-05 09:01:17 +00:00
void OpenDevTools(mate::Arguments* args);
void CloseDevTools();
bool IsDevToolsOpened();
bool IsDevToolsFocused();
2015-06-05 09:01:17 +00:00
void ToggleDevTools();
void EnableDeviceEmulation(const blink::WebDeviceEmulationParams& params);
2015-08-31 09:19:19 +00:00
void DisableDeviceEmulation();
2015-04-22 07:30:10 +00:00
void InspectElement(int x, int y);
void InspectServiceWorker();
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);
// DevTools workspace api.
2015-09-07 08:12:31 +00:00
void AddWorkSpace(mate::Arguments* args, const base::FilePath& path);
void RemoveWorkSpace(mate::Arguments* args, const base::FilePath& path);
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);
2015-12-17 23:10:42 +00:00
uint32 FindInPage(mate::Arguments* args);
2015-12-17 17:27:56 +00:00
void StopFindInPage(content::StopFindAction action);
2015-01-19 20:09:47 +00:00
2015-07-24 04:58:28 +00:00
// Focus.
void Focus();
void TabTraverse(bool reverse);
2015-09-18 06:20:31 +00:00
// Send messages to browser.
2014-06-28 11:36:57 +00:00
bool SendIPCMessage(const base::string16& channel,
const base::ListValue& args);
2015-09-18 06:20:31 +00:00
// Send WebInputEvent to the page.
void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);
// Subscribe to the frame updates.
void BeginFrameSubscription(
const FrameSubscriber::FrameCaptureCallback& callback);
void EndFrameSubscription();
// Methods for creating <webview>.
void SetSize(const SetSizeParams& params);
2014-10-24 08:48:21 +00:00
void SetAllowTransparency(bool allow);
2015-06-05 07:18:15 +00:00
bool IsGuest() const;
2014-10-24 08:09:18 +00:00
2016-02-01 10:03:38 +00:00
// Callback triggered on permission response.
void OnEnterFullscreenModeForTab(content::WebContents* source,
const GURL& origin,
bool allowed);
// Returns the web preferences of current WebContents.
v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
2015-10-01 05:45:59 +00:00
// Returns the owner window.
v8::Local<v8::Value> GetOwnerBrowserWindow();
2015-10-01 03:14:19 +00:00
// Properties.
v8::Local<v8::Value> Session(v8::Isolate* isolate);
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
2016-01-21 18:22:23 +00:00
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
2015-10-01 03:14:19 +00:00
// mate::TrackableObject:
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype);
2014-04-24 08:45:25 +00:00
protected:
explicit WebContents(content::WebContents* web_contents);
2015-09-04 16:44:22 +00:00
WebContents(v8::Isolate* isolate, const mate::Dictionary& options);
~WebContents();
2014-04-24 08:45:25 +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,
2015-09-02 07:16:49 +00:00
const std::string& frame_name,
2014-10-25 05:21:09 +00:00
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) 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;
void BeforeUnloadFired(content::WebContents* tab,
bool proceed,
bool* proceed_to_fire_unload) override;
void MoveContents(content::WebContents* source,
const gfx::Rect& pos) override;
void CloseContents(content::WebContents* source) override;
void ActivateContents(content::WebContents* contents) override;
2015-06-24 14:23:38 +00:00
bool IsPopupOrPanel(const content::WebContents* source) const 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;
void RendererUnresponsive(content::WebContents* source) override;
void RendererResponsive(content::WebContents* source) override;
2015-10-31 13:39:07 +00:00
bool HandleContextMenu(const content::ContextMenuParams& params) override;
bool OnGoToEntryOffset(int offset) override;
2015-12-17 17:27:56 +00:00
void FindReply(content::WebContents* web_contents,
int request_id,
int number_of_matches,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) override;
2016-01-23 13:29:47 +00:00
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) override;
2016-02-01 09:43:49 +00:00
void RequestToLockMouse(
content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) override;
2014-10-24 10:06:32 +00:00
// content::WebContentsObserver:
void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;
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,
2015-09-02 07:16:49 +00:00
const base::string16& error_description,
bool was_ignored_by_handler) override;
2015-03-14 03:28:30 +00:00
void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
2015-09-02 07:16:49 +00:00
const base::string16& error_description,
bool was_ignored_by_handler) 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 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;
2015-12-20 03:16:22 +00:00
void MediaStartedPlaying() override;
void MediaPaused() override;
void DidChangeThemeColor(SkColor theme_color) override;
2014-04-24 08:45:25 +00:00
2015-10-01 03:14:19 +00:00
// brightray::InspectableWebContentsViewDelegate:
void DevToolsFocused() override;
void DevToolsOpened() override;
void DevToolsClosed() override;
2014-04-24 08:45:25 +00:00
private:
2015-06-24 13:44:27 +00:00
enum Type {
BROWSER_WINDOW, // Used by BrowserWindow.
WEB_VIEW, // Used by <webview>.
REMOTE, // Thin wrap around an existing WebContents.
};
2015-07-14 19:13:25 +00:00
AtomBrowserContext* GetBrowserContext() const;
2015-12-17 23:10:42 +00:00
uint32 GetNextRequestId() {
return ++request_id_;
}
// Called when we receive a CursorChange message from chromium.
void OnCursorChange(const content::WebCursor& cursor);
// 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);
v8::Global<v8::Value> session_;
2015-10-01 03:14:19 +00:00
v8::Global<v8::Value> devtools_web_contents_;
2016-01-21 18:22:23 +00:00
v8::Global<v8::Value> debugger_;
scoped_ptr<WebViewGuestDelegate> guest_delegate_;
2015-06-24 13:44:27 +00:00
// The type of current WebContents.
Type type_;
2015-12-17 23:10:42 +00:00
// Request id used for findInPage request.
uint32 request_id_;
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_