atom_api_web_contents.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
  5. #define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "atom/browser/api/frame_subscriber.h"
  9. #include "atom/browser/api/save_page_handler.h"
  10. #include "atom/browser/api/trackable_object.h"
  11. #include "atom/browser/common_web_contents_delegate.h"
  12. #include "atom/browser/ui/autofill_popup.h"
  13. #include "base/observer_list.h"
  14. #include "content/common/cursors/webcursor.h"
  15. #include "content/public/browser/keyboard_event_processing_result.h"
  16. #include "content/public/browser/web_contents.h"
  17. #include "content/public/browser/web_contents_observer.h"
  18. #include "content/public/common/favicon_url.h"
  19. #include "native_mate/handle.h"
  20. #include "printing/backend/print_backend.h"
  21. #include "ui/gfx/image/image.h"
  22. namespace blink {
  23. struct WebDeviceEmulationParams;
  24. }
  25. namespace brightray {
  26. class InspectableWebContents;
  27. }
  28. namespace content {
  29. class ResourceRequestBody;
  30. }
  31. namespace mate {
  32. class Arguments;
  33. class Dictionary;
  34. } // namespace mate
  35. namespace atom {
  36. struct SetSizeParams;
  37. class AtomBrowserContext;
  38. class AtomJavaScriptDialogManager;
  39. class WebContentsZoomController;
  40. class WebViewGuestDelegate;
  41. #if defined(ENABLE_OSR)
  42. class OffScreenWebContentsView;
  43. #endif
  44. namespace api {
  45. // Certain events are only in WebContentsDelegate, provide our own Observer to
  46. // dispatch those events.
  47. class ExtendedWebContentsObserver {
  48. public:
  49. virtual void OnCloseContents() {}
  50. virtual void OnRendererResponsive() {}
  51. };
  52. // Wrapper around the content::WebContents.
  53. class WebContents : public mate::TrackableObject<WebContents>,
  54. public CommonWebContentsDelegate,
  55. public content::WebContentsObserver {
  56. public:
  57. enum Type {
  58. BACKGROUND_PAGE, // A DevTools extension background page.
  59. BROWSER_WINDOW, // Used by BrowserWindow.
  60. BROWSER_VIEW, // Used by BrowserView.
  61. REMOTE, // Thin wrap around an existing WebContents.
  62. WEB_VIEW, // Used by <webview>.
  63. OFF_SCREEN, // Used for offscreen rendering
  64. };
  65. // For node.js callback function type: function(error, buffer)
  66. using PrintToPDFCallback =
  67. base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
  68. // Create from an existing WebContents.
  69. static mate::Handle<WebContents> CreateFrom(
  70. v8::Isolate* isolate,
  71. content::WebContents* web_contents);
  72. static mate::Handle<WebContents> CreateFrom(
  73. v8::Isolate* isolate,
  74. content::WebContents* web_contents,
  75. Type type);
  76. // Create a new WebContents.
  77. static mate::Handle<WebContents> Create(v8::Isolate* isolate,
  78. const mate::Dictionary& options);
  79. static void BuildPrototype(v8::Isolate* isolate,
  80. v8::Local<v8::FunctionTemplate> prototype);
  81. static int64_t GetIDForContents(content::WebContents* web_contents);
  82. // Notifies to destroy any guest web contents before destroying self.
  83. void DestroyWebContents(bool async);
  84. int64_t GetID() const;
  85. int GetProcessID() const;
  86. base::ProcessId GetOSProcessID() const;
  87. Type GetType() const;
  88. bool Equal(const WebContents* web_contents) const;
  89. void LoadURL(const GURL& url, const mate::Dictionary& options);
  90. void DownloadURL(const GURL& url);
  91. GURL GetURL() const;
  92. base::string16 GetTitle() const;
  93. bool IsLoading() const;
  94. bool IsLoadingMainFrame() const;
  95. bool IsWaitingForResponse() const;
  96. void Stop();
  97. void ReloadIgnoringCache();
  98. void GoBack();
  99. void GoForward();
  100. void GoToOffset(int offset);
  101. const std::string GetWebRTCIPHandlingPolicy() const;
  102. void SetWebRTCIPHandlingPolicy(const std::string& webrtc_ip_handling_policy);
  103. bool IsCrashed() const;
  104. void SetUserAgent(const std::string& user_agent, mate::Arguments* args);
  105. std::string GetUserAgent();
  106. void InsertCSS(const std::string& css);
  107. bool SavePage(const base::FilePath& full_file_path,
  108. const content::SavePageType& save_type,
  109. const SavePageHandler::SavePageCallback& callback);
  110. void OpenDevTools(mate::Arguments* args);
  111. void CloseDevTools();
  112. bool IsDevToolsOpened();
  113. bool IsDevToolsFocused();
  114. void ToggleDevTools();
  115. void EnableDeviceEmulation(const blink::WebDeviceEmulationParams& params);
  116. void DisableDeviceEmulation();
  117. void InspectElement(int x, int y);
  118. void InspectServiceWorker();
  119. void HasServiceWorker(const base::Callback<void(bool)>&);
  120. void UnregisterServiceWorker(const base::Callback<void(bool)>&);
  121. void SetIgnoreMenuShortcuts(bool ignore);
  122. void SetAudioMuted(bool muted);
  123. bool IsAudioMuted();
  124. void Print(mate::Arguments* args);
  125. std::vector<printing::PrinterBasicInfo> GetPrinterList();
  126. void SetEmbedder(const WebContents* embedder);
  127. void SetDevToolsWebContents(const WebContents* devtools);
  128. v8::Local<v8::Value> GetNativeView() const;
  129. // Print current page as PDF.
  130. void PrintToPDF(const base::DictionaryValue& setting,
  131. const PrintToPDFCallback& callback);
  132. // DevTools workspace api.
  133. void AddWorkSpace(mate::Arguments* args, const base::FilePath& path);
  134. void RemoveWorkSpace(mate::Arguments* args, const base::FilePath& path);
  135. // Editing commands.
  136. void Undo();
  137. void Redo();
  138. void Cut();
  139. void Copy();
  140. void Paste();
  141. void PasteAndMatchStyle();
  142. void Delete();
  143. void SelectAll();
  144. void Unselect();
  145. void Replace(const base::string16& word);
  146. void ReplaceMisspelling(const base::string16& word);
  147. uint32_t FindInPage(mate::Arguments* args);
  148. void StopFindInPage(content::StopFindAction action);
  149. void ShowDefinitionForSelection();
  150. void CopyImageAt(int x, int y);
  151. // Focus.
  152. void Focus();
  153. bool IsFocused() const;
  154. void TabTraverse(bool reverse);
  155. // Send messages to browser.
  156. bool SendIPCMessage(bool all_frames,
  157. const base::string16& channel,
  158. const base::ListValue& args);
  159. // Send WebInputEvent to the page.
  160. void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);
  161. // Subscribe to the frame updates.
  162. void BeginFrameSubscription(mate::Arguments* args);
  163. void EndFrameSubscription();
  164. // Dragging native items.
  165. void StartDrag(const mate::Dictionary& item, mate::Arguments* args);
  166. // Captures the page with |rect|, |callback| would be called when capturing is
  167. // done.
  168. void CapturePage(mate::Arguments* args);
  169. // Methods for creating <webview>.
  170. void SetSize(const SetSizeParams& params);
  171. bool IsGuest() const;
  172. // Methods for offscreen rendering
  173. bool IsOffScreen() const;
  174. void OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap);
  175. void StartPainting();
  176. void StopPainting();
  177. bool IsPainting() const;
  178. void SetFrameRate(int frame_rate);
  179. int GetFrameRate() const;
  180. void Invalidate();
  181. gfx::Size GetSizeForNewRenderView(content::WebContents*) const override;
  182. // Methods for zoom handling.
  183. void SetZoomLevel(double level);
  184. double GetZoomLevel();
  185. void SetZoomFactor(double factor);
  186. double GetZoomFactor();
  187. // Callback triggered on permission response.
  188. void OnEnterFullscreenModeForTab(content::WebContents* source,
  189. const GURL& origin,
  190. bool allowed);
  191. // Create window with the given disposition.
  192. void OnCreateWindow(const GURL& target_url,
  193. const content::Referrer& referrer,
  194. const std::string& frame_name,
  195. WindowOpenDisposition disposition,
  196. const std::vector<std::string>& features,
  197. const scoped_refptr<content::ResourceRequestBody>& body);
  198. // Returns the web preferences of current WebContents.
  199. v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
  200. v8::Local<v8::Value> GetLastWebPreferences(v8::Isolate* isolate);
  201. // Returns the owner window.
  202. v8::Local<v8::Value> GetOwnerBrowserWindow();
  203. // Grants the child process the capability to access URLs with the origin of
  204. // the specified URL.
  205. void GrantOriginAccess(const GURL& url);
  206. // Properties.
  207. int32_t ID() const;
  208. v8::Local<v8::Value> Session(v8::Isolate* isolate);
  209. content::WebContents* HostWebContents();
  210. v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
  211. v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
  212. WebContentsZoomController* GetZoomController() { return zoom_controller_; }
  213. void AddObserver(ExtendedWebContentsObserver* obs) {
  214. observers_.AddObserver(obs);
  215. }
  216. void RemoveObserver(ExtendedWebContentsObserver* obs) {
  217. observers_.RemoveObserver(obs);
  218. }
  219. protected:
  220. WebContents(v8::Isolate* isolate,
  221. content::WebContents* web_contents,
  222. Type type);
  223. WebContents(v8::Isolate* isolate, const mate::Dictionary& options);
  224. ~WebContents() override;
  225. void InitWithSessionAndOptions(v8::Isolate* isolate,
  226. content::WebContents* web_contents,
  227. mate::Handle<class Session> session,
  228. const mate::Dictionary& options);
  229. // content::WebContentsDelegate:
  230. bool DidAddMessageToConsole(content::WebContents* source,
  231. int32_t level,
  232. const base::string16& message,
  233. int32_t line_no,
  234. const base::string16& source_id) override;
  235. void WebContentsCreated(content::WebContents* source_contents,
  236. int opener_render_process_id,
  237. int opener_render_frame_id,
  238. const std::string& frame_name,
  239. const GURL& target_url,
  240. content::WebContents* new_contents) override;
  241. void AddNewContents(content::WebContents* source,
  242. content::WebContents* new_contents,
  243. WindowOpenDisposition disposition,
  244. const gfx::Rect& initial_rect,
  245. bool user_gesture,
  246. bool* was_blocked) override;
  247. content::WebContents* OpenURLFromTab(
  248. content::WebContents* source,
  249. const content::OpenURLParams& params) override;
  250. void BeforeUnloadFired(content::WebContents* tab,
  251. bool proceed,
  252. bool* proceed_to_fire_unload) override;
  253. void MoveContents(content::WebContents* source,
  254. const gfx::Rect& pos) override;
  255. void CloseContents(content::WebContents* source) override;
  256. void ActivateContents(content::WebContents* contents) override;
  257. void UpdateTargetURL(content::WebContents* source, const GURL& url) override;
  258. bool IsPopupOrPanel(const content::WebContents* source) const override;
  259. void HandleKeyboardEvent(
  260. content::WebContents* source,
  261. const content::NativeWebKeyboardEvent& event) override;
  262. content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
  263. content::WebContents* source,
  264. const content::NativeWebKeyboardEvent& event) override;
  265. void EnterFullscreenModeForTab(content::WebContents* source,
  266. const GURL& origin) override;
  267. void ExitFullscreenModeForTab(content::WebContents* source) override;
  268. void RendererUnresponsive(
  269. content::WebContents* source,
  270. const content::WebContentsUnresponsiveState& unresponsive_state) override;
  271. void RendererResponsive(content::WebContents* source) override;
  272. bool HandleContextMenu(const content::ContextMenuParams& params) override;
  273. bool OnGoToEntryOffset(int offset) override;
  274. void FindReply(content::WebContents* web_contents,
  275. int request_id,
  276. int number_of_matches,
  277. const gfx::Rect& selection_rect,
  278. int active_match_ordinal,
  279. bool final_update) override;
  280. bool CheckMediaAccessPermission(content::WebContents* web_contents,
  281. const GURL& security_origin,
  282. content::MediaStreamType type) override;
  283. void RequestMediaAccessPermission(
  284. content::WebContents* web_contents,
  285. const content::MediaStreamRequest& request,
  286. const content::MediaResponseCallback& callback) override;
  287. void RequestToLockMouse(content::WebContents* web_contents,
  288. bool user_gesture,
  289. bool last_unlocked_by_target) override;
  290. std::unique_ptr<content::BluetoothChooser> RunBluetoothChooser(
  291. content::RenderFrameHost* frame,
  292. const content::BluetoothChooser::EventHandler& handler) override;
  293. content::JavaScriptDialogManager* GetJavaScriptDialogManager(
  294. content::WebContents* source) override;
  295. // content::WebContentsObserver:
  296. void BeforeUnloadFired(const base::TimeTicks& proceed_time) override;
  297. void RenderViewCreated(content::RenderViewHost*) override;
  298. void RenderViewDeleted(content::RenderViewHost*) override;
  299. void RenderProcessGone(base::TerminationStatus status) override;
  300. void DocumentLoadedInFrame(
  301. content::RenderFrameHost* render_frame_host) override;
  302. void DidFinishLoad(content::RenderFrameHost* render_frame_host,
  303. const GURL& validated_url) override;
  304. void DidFailLoad(content::RenderFrameHost* render_frame_host,
  305. const GURL& validated_url,
  306. int error_code,
  307. const base::string16& error_description) override;
  308. void DidStartLoading() override;
  309. void DidStopLoading() override;
  310. void DidGetResourceResponseStart(
  311. const content::ResourceRequestDetails& details) override;
  312. void DidGetRedirectForResourceRequest(
  313. const content::ResourceRedirectDetails& details) override;
  314. void DidStartNavigation(
  315. content::NavigationHandle* navigation_handle) override;
  316. void DidFinishNavigation(
  317. content::NavigationHandle* navigation_handle) override;
  318. bool OnMessageReceived(const IPC::Message& message) override;
  319. bool OnMessageReceived(const IPC::Message& message,
  320. content::RenderFrameHost* frame_host) override;
  321. void WebContentsDestroyed() override;
  322. void NavigationEntryCommitted(
  323. const content::LoadCommittedDetails& load_details) override;
  324. void TitleWasSet(content::NavigationEntry* entry) override;
  325. void DidUpdateFaviconURL(
  326. const std::vector<content::FaviconURL>& urls) override;
  327. void PluginCrashed(const base::FilePath& plugin_path,
  328. base::ProcessId plugin_pid) override;
  329. void MediaStartedPlaying(const MediaPlayerInfo& video_type,
  330. const MediaPlayerId& id) override;
  331. void MediaStoppedPlaying(const MediaPlayerInfo& video_type,
  332. const MediaPlayerId& id) override;
  333. void DidChangeThemeColor(SkColor theme_color) override;
  334. // brightray::InspectableWebContentsDelegate:
  335. void DevToolsReloadPage() override;
  336. // brightray::InspectableWebContentsViewDelegate:
  337. void DevToolsFocused() override;
  338. void DevToolsOpened() override;
  339. void DevToolsClosed() override;
  340. #if defined(TOOLKIT_VIEWS)
  341. void ShowAutofillPopup(content::RenderFrameHost* frame_host,
  342. const gfx::RectF& bounds,
  343. const std::vector<base::string16>& values,
  344. const std::vector<base::string16>& labels);
  345. #endif
  346. private:
  347. struct FrameDispatchHelper;
  348. AtomBrowserContext* GetBrowserContext() const;
  349. uint32_t GetNextRequestId() { return ++request_id_; }
  350. #if defined(ENABLE_OSR)
  351. OffScreenWebContentsView* GetOffScreenWebContentsView() const;
  352. #endif
  353. // Called when we receive a CursorChange message from chromium.
  354. void OnCursorChange(const content::WebCursor& cursor);
  355. // Called when received a message from renderer.
  356. void OnRendererMessage(content::RenderFrameHost* frame_host,
  357. const base::string16& channel,
  358. const base::ListValue& args);
  359. // Called when received a synchronous message from renderer.
  360. void OnRendererMessageSync(content::RenderFrameHost* frame_host,
  361. const base::string16& channel,
  362. const base::ListValue& args,
  363. IPC::Message* message);
  364. // Called when received a synchronous message from renderer to
  365. // set temporary zoom level.
  366. void OnSetTemporaryZoomLevel(content::RenderFrameHost* frame_host,
  367. double level,
  368. IPC::Message* reply_msg);
  369. // Called when received a synchronous message from renderer to
  370. // get the zoom level.
  371. void OnGetZoomLevel(content::RenderFrameHost* frame_host,
  372. IPC::Message* reply_msg);
  373. void InitZoomController(content::WebContents* web_contents,
  374. const mate::Dictionary& options);
  375. v8::Global<v8::Value> session_;
  376. v8::Global<v8::Value> devtools_web_contents_;
  377. v8::Global<v8::Value> debugger_;
  378. std::unique_ptr<AtomJavaScriptDialogManager> dialog_manager_;
  379. std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
  380. // The host webcontents that may contain this webcontents.
  381. WebContents* embedder_ = nullptr;
  382. // The zoom controller for this webContents.
  383. WebContentsZoomController* zoom_controller_ = nullptr;
  384. // The type of current WebContents.
  385. Type type_ = BROWSER_WINDOW;
  386. // Request id used for findInPage request.
  387. uint32_t request_id_ = 0;
  388. // Whether background throttling is disabled.
  389. bool background_throttling_ = true;
  390. // Whether to enable devtools.
  391. bool enable_devtools_ = true;
  392. // Observers of this WebContents.
  393. base::ObserverList<ExtendedWebContentsObserver> observers_;
  394. DISALLOW_COPY_AND_ASSIGN(WebContents);
  395. };
  396. } // namespace api
  397. } // namespace atom
  398. #endif // ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_