common_web_contents_delegate.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2015 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_COMMON_WEB_CONTENTS_DELEGATE_H_
  5. #define ATOM_BROWSER_COMMON_WEB_CONTENTS_DELEGATE_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "brightray/browser/devtools_file_system_indexer.h"
  10. #include "brightray/browser/inspectable_web_contents_delegate.h"
  11. #include "brightray/browser/inspectable_web_contents_impl.h"
  12. #include "brightray/browser/inspectable_web_contents_view_delegate.h"
  13. #include "content/public/browser/web_contents_delegate.h"
  14. #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
  15. #include "atom/browser/ui/autofill_popup.h"
  16. #endif
  17. using brightray::DevToolsFileSystemIndexer;
  18. namespace atom {
  19. class AtomBrowserContext;
  20. class NativeWindow;
  21. class WebDialogHelper;
  22. class CommonWebContentsDelegate
  23. : public content::WebContentsDelegate,
  24. public brightray::InspectableWebContentsDelegate,
  25. public brightray::InspectableWebContentsViewDelegate {
  26. public:
  27. CommonWebContentsDelegate();
  28. ~CommonWebContentsDelegate() override;
  29. // Creates a InspectableWebContents object and takes onwership of
  30. // |web_contents|.
  31. void InitWithWebContents(content::WebContents* web_contents,
  32. AtomBrowserContext* browser_context);
  33. // Set the window as owner window.
  34. void SetOwnerWindow(NativeWindow* owner_window);
  35. void SetOwnerWindow(content::WebContents* web_contents,
  36. NativeWindow* owner_window);
  37. // Returns the WebContents managed by this delegate.
  38. content::WebContents* GetWebContents() const;
  39. // Returns the WebContents of devtools.
  40. content::WebContents* GetDevToolsWebContents() const;
  41. brightray::InspectableWebContents* managed_web_contents() const {
  42. return web_contents_.get();
  43. }
  44. NativeWindow* owner_window() const { return owner_window_.get(); }
  45. void set_ignore_menu_shortcuts(bool ignore) {
  46. ignore_menu_shortcuts_ = ignore;
  47. }
  48. bool is_html_fullscreen() const { return html_fullscreen_; }
  49. protected:
  50. // content::WebContentsDelegate:
  51. content::WebContents* OpenURLFromTab(
  52. content::WebContents* source,
  53. const content::OpenURLParams& params) override;
  54. bool CanOverscrollContent() const override;
  55. content::ColorChooser* OpenColorChooser(
  56. content::WebContents* web_contents,
  57. SkColor color,
  58. const std::vector<content::ColorSuggestion>& suggestions) override;
  59. void RunFileChooser(content::RenderFrameHost* render_frame_host,
  60. const content::FileChooserParams& params) override;
  61. void EnumerateDirectory(content::WebContents* web_contents,
  62. int request_id,
  63. const base::FilePath& path) override;
  64. void EnterFullscreenModeForTab(content::WebContents* source,
  65. const GURL& origin) override;
  66. void ExitFullscreenModeForTab(content::WebContents* source) override;
  67. bool IsFullscreenForTabOrPending(
  68. const content::WebContents* source) const override;
  69. blink::WebSecurityStyle GetSecurityStyle(
  70. content::WebContents* web_contents,
  71. content::SecurityStyleExplanations* explanations) override;
  72. void HandleKeyboardEvent(
  73. content::WebContents* source,
  74. const content::NativeWebKeyboardEvent& event) override;
  75. // Autofill related events.
  76. #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
  77. void ShowAutofillPopup(bool offscreen,
  78. content::RenderFrameHost* frame_host,
  79. const gfx::RectF& bounds,
  80. const std::vector<base::string16>& values,
  81. const std::vector<base::string16>& labels);
  82. void HideAutofillPopup();
  83. #endif
  84. // brightray::InspectableWebContentsDelegate:
  85. void DevToolsSaveToFile(const std::string& url,
  86. const std::string& content,
  87. bool save_as) override;
  88. void DevToolsAppendToFile(const std::string& url,
  89. const std::string& content) override;
  90. void DevToolsRequestFileSystems() override;
  91. void DevToolsAddFileSystem(const base::FilePath& path) override;
  92. void DevToolsRemoveFileSystem(
  93. const base::FilePath& file_system_path) override;
  94. void DevToolsIndexPath(int request_id,
  95. const std::string& file_system_path) override;
  96. void DevToolsStopIndexing(int request_id) override;
  97. void DevToolsSearchInPath(int request_id,
  98. const std::string& file_system_path,
  99. const std::string& query) override;
  100. // brightray::InspectableWebContentsViewDelegate:
  101. #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
  102. gfx::ImageSkia GetDevToolsWindowIcon() override;
  103. #endif
  104. #if defined(USE_X11)
  105. void GetDevToolsWindowWMClass(std::string* name,
  106. std::string* class_name) override;
  107. #endif
  108. // Destroy the managed InspectableWebContents object.
  109. void ResetManagedWebContents(bool async);
  110. private:
  111. // Callback for when DevToolsSaveToFile has completed.
  112. void OnDevToolsSaveToFile(const std::string& url);
  113. // Callback for when DevToolsAppendToFile has completed.
  114. void OnDevToolsAppendToFile(const std::string& url);
  115. // DevTools index event callbacks.
  116. void OnDevToolsIndexingWorkCalculated(int request_id,
  117. const std::string& file_system_path,
  118. int total_work);
  119. void OnDevToolsIndexingWorked(int request_id,
  120. const std::string& file_system_path,
  121. int worked);
  122. void OnDevToolsIndexingDone(int request_id,
  123. const std::string& file_system_path);
  124. void OnDevToolsSearchCompleted(int request_id,
  125. const std::string& file_system_path,
  126. const std::vector<std::string>& file_paths);
  127. // Set fullscreen mode triggered by html api.
  128. void SetHtmlApiFullscreen(bool enter_fullscreen);
  129. // The window that this WebContents belongs to.
  130. base::WeakPtr<NativeWindow> owner_window_;
  131. bool offscreen_ = false;
  132. bool ignore_menu_shortcuts_ = false;
  133. // Whether window is fullscreened by HTML5 api.
  134. bool html_fullscreen_ = false;
  135. // Whether window is fullscreened by window api.
  136. bool native_fullscreen_ = false;
  137. // UI related helper classes.
  138. #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
  139. std::unique_ptr<AutofillPopup> autofill_popup_;
  140. #endif
  141. std::unique_ptr<WebDialogHelper> web_dialog_helper_;
  142. scoped_refptr<DevToolsFileSystemIndexer> devtools_file_system_indexer_;
  143. // Make sure BrowserContext is alwasys destroyed after WebContents.
  144. scoped_refptr<AtomBrowserContext> browser_context_;
  145. // The stored InspectableWebContents object.
  146. // Notice that web_contents_ must be placed after dialog_manager_, so we can
  147. // make sure web_contents_ is destroyed before dialog_manager_, otherwise a
  148. // crash would happen.
  149. std::unique_ptr<brightray::InspectableWebContents> web_contents_;
  150. // Maps url to file path, used by the file requests sent from devtools.
  151. typedef std::map<std::string, base::FilePath> PathsMap;
  152. PathsMap saved_files_;
  153. // Map id to index job, used for file system indexing requests from devtools.
  154. typedef std::
  155. map<int, scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob>>
  156. DevToolsIndexingJobsMap;
  157. DevToolsIndexingJobsMap devtools_indexing_jobs_;
  158. DISALLOW_COPY_AND_ASSIGN(CommonWebContentsDelegate);
  159. };
  160. } // namespace atom
  161. #endif // ATOM_BROWSER_COMMON_WEB_CONTENTS_DELEGATE_H_