| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 5 #ifndef HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
| 6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 6 #define HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
| 7 | 7 |
| 8 #include <string> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "headless/public/headless_export.h" | 14 #include "headless/public/headless_export.h" |
| 15 #include "net/base/ip_endpoint.h" |
| 13 | 16 |
| 14 namespace base { | 17 namespace base { |
| 18 class MessagePump; |
| 15 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
| 16 | |
| 17 namespace trace_event { | |
| 18 class TraceConfig; | |
| 19 } | |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace gfx { | 22 namespace gfx { |
| 23 class Size; | 23 class Size; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace net { | 26 namespace headless { |
| 27 class URLRequestContextGetter; | 27 class HeadlessWebContents; |
| 28 } | |
| 29 | 28 |
| 30 namespace headless { | 29 // This class represents the global headless browser instance. To get a pointer |
| 31 class WebContents; | 30 // to one, call |HeadlessBrowserMain| to initiate the browser main loop. An |
| 32 | 31 // instance of |HeadlessBrowser| will be passed to the callback given to that |
| 32 // function. |
| 33 class HEADLESS_EXPORT HeadlessBrowser { | 33 class HEADLESS_EXPORT HeadlessBrowser { |
| 34 public: | 34 public: |
| 35 static HeadlessBrowser* Get(); | |
| 36 | |
| 37 struct Options; | 35 struct Options; |
| 38 | 36 |
| 39 // Main routine for running browser. | 37 // Create a new browser tab. |size| is in physical pixels. |
| 40 // Takes command line args and callback to run as soon as browser starts. | 38 virtual scoped_ptr<HeadlessWebContents> CreateWebContents( |
| 41 static int Run( | 39 const gfx::Size& size) = 0; |
| 42 const Options& options, | |
| 43 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); | |
| 44 | 40 |
| 45 // Create a new browser tab. | 41 // Returns a task runner for submitting work to the browser main thread. |
| 46 virtual scoped_ptr<WebContents> CreateWebContents(const gfx::Size& size) = 0; | 42 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserMainThread() |
| 43 const = 0; |
| 47 | 44 |
| 48 virtual scoped_refptr<base::SingleThreadTaskRunner> BrowserMainThread() = 0; | 45 // Requests browser to stop as soon as possible. |Run| will return as soon as |
| 49 virtual scoped_refptr<base::SingleThreadTaskRunner> RendererMainThread() = 0; | 46 // browser stops. |
| 50 | 47 virtual void Shutdown() = 0; |
| 51 // Requests browser to stop as soon as possible. | |
| 52 // |Run| will return as soon as browser stops. | |
| 53 virtual void Stop() = 0; | |
| 54 | |
| 55 virtual void StartTracing(const base::trace_event::TraceConfig& trace_config, | |
| 56 const base::Closure& on_tracing_started) = 0; | |
| 57 virtual void StopTracing(const std::string& log_file_name, | |
| 58 const base::Closure& on_tracing_stopped) = 0; | |
| 59 | 48 |
| 60 protected: | 49 protected: |
| 50 HeadlessBrowser() {} |
| 61 virtual ~HeadlessBrowser() {} | 51 virtual ~HeadlessBrowser() {} |
| 62 | 52 |
| 63 private: | 53 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); | 54 DISALLOW_COPY_AND_ASSIGN(HeadlessBrowser); |
| 65 }; | 55 }; |
| 66 | 56 |
| 57 // Embedding API overrides for the headless browser. |
| 67 struct HeadlessBrowser::Options { | 58 struct HeadlessBrowser::Options { |
| 68 ~Options(); | 59 ~Options(); |
| 69 | 60 |
| 70 class Builder; | 61 class Builder; |
| 71 | 62 |
| 72 // Command line options to be passed to browser. | 63 // Command line options to be passed to browser. |
| 73 int argc; | 64 int argc; |
| 74 const char** argv; | 65 const char** argv; |
| 75 | 66 |
| 76 std::string user_agent; | 67 std::string user_agent; |
| 77 std::string navigator_platform; | 68 std::string navigator_platform; |
| 78 | 69 |
| 79 static const int kInvalidPort = -1; | 70 net::IPEndPoint devtools_endpoint; |
| 80 // If not null, create start devtools for remote debugging | |
| 81 // on specified port. | |
| 82 int devtools_http_port; | |
| 83 | 71 |
| 84 // Optional URLRequestContextGetter for customizing network stack. | 72 // Optional message pump that overrides the default. Must outlive the browser. |
| 85 // Allows overriding: | 73 base::MessagePump* message_pump; |
| 86 // - Cookie storage | |
| 87 // - HTTP cache | |
| 88 // - SSL config | |
| 89 // - Proxy service | |
| 90 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter; | |
| 91 | |
| 92 scoped_ptr<base::MessagePump> message_pump; | |
| 93 | 74 |
| 94 private: | 75 private: |
| 95 Options(int argc, const char** argv); | 76 Options(int argc, const char** argv); |
| 96 }; | 77 }; |
| 97 | 78 |
| 98 class HeadlessBrowser::Options::Builder { | 79 class HeadlessBrowser::Options::Builder { |
| 99 public: | 80 public: |
| 100 Builder(int argc, const char** argv); | 81 Builder(int argc, const char** argv); |
| 101 ~Builder(); | 82 ~Builder(); |
| 102 | 83 |
| 103 Builder& SetUserAgent(const std::string& user_agent); | 84 Builder& SetUserAgent(const std::string& user_agent); |
| 104 Builder& EnableDevToolsServer(int port); | 85 Builder& EnableDevToolsServer(const net::IPEndPoint& endpoint); |
| 105 Builder& SetURLRequestContextGetter( | 86 Builder& SetMessagePump(base::MessagePump* message_pump); |
| 106 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); | |
| 107 | 87 |
| 108 Options Build(); | 88 Options Build(); |
| 109 | 89 |
| 110 private: | 90 private: |
| 111 Options options_; | 91 Options options_; |
| 112 | 92 |
| 113 DISALLOW_COPY_AND_ASSIGN(Builder); | 93 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 114 }; | 94 }; |
| 115 | 95 |
| 96 // Main entry point for running the headless browser. This function constructs |
| 97 // the headless browser instance, passing it to the given |
| 98 // |on_browser_start_callback| callback. Note that since this function executes |
| 99 // the main loop, it will only return after HeadlessBrowser::Shutdown() is |
| 100 // called, returning the exit code for the process. |
| 101 int HeadlessBrowserMain( |
| 102 const HeadlessBrowser::Options& options, |
| 103 const base::Callback<void(HeadlessBrowser*)>& on_browser_start_callback); |
| 104 |
| 116 } // namespace headless | 105 } // namespace headless |
| 117 | 106 |
| 118 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ | 107 #endif // HEADLESS_PUBLIC_HEADLESS_BROWSER_H_ |
| OLD | NEW |