Skip to content

Commit 8c01f5b

Browse files
committed
Merge branch 'master' of github.com:andrewvy/chrome-remote-interface
2 parents 26cee84 + 74ad284 commit 8c01f5b

File tree

6 files changed

+79
-62
lines changed

6 files changed

+79
-62
lines changed

lib/chrome_remote_interface.ex

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ defmodule ChromeRemoteInterface do
77

88
@protocol_env_key "CRI_PROTOCOL_VERSION"
99
@protocol_versions ["1-2", "1-3", "tot"]
10-
@protocol_version (
11-
if (vsn = System.get_env(@protocol_env_key)) in @protocol_versions do
12-
vsn
13-
else
14-
"1-3"
15-
end
10+
@protocol_version (if (vsn = System.get_env(@protocol_env_key)) in @protocol_versions do
11+
vsn
12+
else
13+
"1-3"
14+
end)
15+
IO.puts(
16+
"Compiling ChromeRemoteInterface with Chrome DevTools Protocol version: '#{@protocol_version}'"
1617
)
17-
IO.puts("Compiling ChromeRemoteInterface with Chrome DevTools Protocol version: '#{@protocol_version}'")
1818

1919
@doc """
2020
Gets the current version of the Chrome Debugger Protocol
@@ -25,12 +25,11 @@ defmodule ChromeRemoteInterface do
2525

2626
protocol =
2727
File.read!("priv/#{@protocol_version}/protocol.json")
28-
|> Poison.decode!()
29-
28+
|> Jason.decode!()
3029

3130
# Generate ChromeRemoteInterface.RPC Modules
3231

33-
Enum.each(protocol["domains"], fn(domain) ->
32+
Enum.each(protocol["domains"], fn domain ->
3433
defmodule Module.concat(ChromeRemoteInterface.RPC, domain["domain"]) do
3534
@domain domain
3635
@moduledoc domain["description"]
@@ -40,10 +39,11 @@ defmodule ChromeRemoteInterface do
4039
for command <- @domain["commands"] do
4140
name = command["name"]
4241
description = command["description"]
42+
4343
arg_doc =
4444
command["parameters"]
4545
|> List.wrap()
46-
|> Enum.map(fn(param) ->
46+
|> Enum.map(fn param ->
4747
"#{param["name"]} - <#{param["$ref"] || param["type"]}> - #{param["description"]}"
4848
end)
4949

@@ -54,21 +54,26 @@ defmodule ChromeRemoteInterface do
5454
#{arg_doc}
5555
"""
5656
def unquote(:"#{name}")(page_pid) do
57-
page_pid |> PageSession.execute_command(
57+
page_pid
58+
|> PageSession.execute_command(
5859
unquote("#{domain["domain"]}.#{name}"),
5960
%{},
6061
[]
6162
)
6263
end
64+
6365
def unquote(:"#{name}")(page_pid, parameters) do
64-
page_pid |> PageSession.execute_command(
66+
page_pid
67+
|> PageSession.execute_command(
6568
unquote("#{domain["domain"]}.#{name}"),
6669
parameters,
6770
[]
6871
)
6972
end
73+
7074
def unquote(:"#{name}")(page_pid, parameters, opts) when is_list(opts) do
71-
page_pid |> PageSession.execute_command(
75+
page_pid
76+
|> PageSession.execute_command(
7277
unquote("#{domain["domain"]}.#{name}"),
7378
parameters,
7479
opts

lib/http.ex

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ defmodule ChromeRemoteInterface.HTTP do
33
This module handles communicating with the DevTools HTTP JSON API.
44
"""
55

6-
@type success_http_response :: {:ok, Map.t}
6+
@type success_http_response :: {:ok, Map.t()}
77
@type error_http_response :: {:error, any()}
88

9-
@spec call(ChromeRemoteInterface.Server.t, String.t) :: success_http_response | error_http_response
9+
@spec call(ChromeRemoteInterface.Server.t(), String.t()) ::
10+
success_http_response | error_http_response
1011
def call(server, path) do
1112
server
1213
|> execute_request(path)
@@ -22,15 +23,15 @@ defmodule ChromeRemoteInterface.HTTP do
2223
end
2324

2425
defp execute_request(server, path) do
25-
:hackney.request(:get, http_url(server, path), [], <<>>, [path_encode_fun: &(&1)])
26+
:hackney.request(:get, http_url(server, path), [], <<>>, path_encode_fun: & &1)
2627
end
2728

2829
defp handle_response({:ok, status_code, _response_headers, client_ref}) do
2930
with true <- status_code >= 200 && status_code < 300,
30-
{:ok, body} <- :hackney.body(client_ref),
31-
{:ok, formatted_body} <- format_body(body),
32-
{:ok, json} <- decode(formatted_body) do
33-
{:ok, json}
31+
{:ok, body} <- :hackney.body(client_ref),
32+
{:ok, formatted_body} <- format_body(body),
33+
{:ok, json} <- decode(formatted_body) do
34+
{:ok, json}
3435
else
3536
error -> error
3637
end
@@ -48,7 +49,7 @@ defmodule ChromeRemoteInterface.HTTP do
4849
defp format_body(body), do: {:ok, body}
4950

5051
defp decode(body) do
51-
case Poison.decode(body) do
52+
case Jason.decode(body) do
5253
{:ok, json} -> {:ok, json}
5354
{:error, _reason} -> {:ok, body}
5455
end

lib/page_session.ex

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ defmodule ChromeRemoteInterface.PageSession do
99

1010
use GenServer
1111

12-
defstruct [
13-
url: "",
14-
socket: nil,
15-
callbacks: [],
16-
event_subscribers: %{},
17-
ref_id: 1
18-
]
12+
defstruct url: "",
13+
socket: nil,
14+
callbacks: [],
15+
event_subscribers: %{},
16+
ref_id: 1
1917

2018
# ---
2119
# Public API
@@ -25,6 +23,7 @@ defmodule ChromeRemoteInterface.PageSession do
2523
Connect to a Page's 'webSocketDebuggerUrl'.
2624
"""
2725
def start_link(%{"webSocketDebuggerUrl" => url}), do: start_link(url)
26+
2827
def start_link(url) do
2928
GenServer.start_link(__MODULE__, url)
3029
end
@@ -58,15 +57,15 @@ defmodule ChromeRemoteInterface.PageSession do
5857
"params" => %{"timestamp" => 1012329.888558}}}
5958
```
6059
"""
61-
@spec subscribe(pid(), String.t, pid()) :: any()
60+
@spec subscribe(pid(), String.t(), pid()) :: any()
6261
def subscribe(pid, event, subscriber_pid \\ self()) do
6362
GenServer.call(pid, {:subscribe, event, subscriber_pid})
6463
end
6564

6665
@doc """
6766
Unsubscribes from an event.
6867
"""
69-
@spec unsubscribe(pid(), String.t, pid()) :: any()
68+
@spec unsubscribe(pid(), String.t(), pid()) :: any()
7069
def unsubscribe(pid, event, subscriber_pid \\ self()) do
7170
GenServer.call(pid, {:unsubscribe, event, subscriber_pid})
7271
end
@@ -121,6 +120,7 @@ defmodule ChromeRemoteInterface.PageSession do
121120

122121
def init(url) do
123122
{:ok, socket} = ChromeRemoteInterface.Websocket.start_link(url)
123+
124124
state = %__MODULE__{
125125
url: url,
126126
socket: socket
@@ -156,7 +156,7 @@ defmodule ChromeRemoteInterface.PageSession do
156156
new_event_subscribers =
157157
state
158158
|> Map.get(:event_subscribers, %{})
159-
|> Map.update(event, [subscriber_pid], fn(subscriber_pids) ->
159+
|> Map.update(event, [subscriber_pid], fn subscriber_pids ->
160160
[subscriber_pid | subscriber_pids]
161161
end)
162162

@@ -169,7 +169,7 @@ defmodule ChromeRemoteInterface.PageSession do
169169
new_event_subscribers =
170170
state
171171
|> Map.get(:event_subscribers, %{})
172-
|> Map.update(event, [], fn(subscriber_pids) ->
172+
|> Map.update(event, [], fn subscriber_pids ->
173173
List.delete(subscriber_pids, subscriber_pid)
174174
end)
175175

@@ -182,7 +182,7 @@ defmodule ChromeRemoteInterface.PageSession do
182182
new_event_subscribers =
183183
state
184184
|> Map.get(:event_subscribers, %{})
185-
|> Enum.map(fn({key, subscriber_pids}) ->
185+
|> Enum.map(fn {key, subscriber_pids} ->
186186
{key, List.delete(subscriber_pids, subscriber_pid)}
187187
end)
188188
|> Enum.into(%{})
@@ -200,7 +200,7 @@ defmodule ChromeRemoteInterface.PageSession do
200200
# If the frame is an event:
201201
# - Forward the event to any subscribers.
202202
def handle_info({:message, frame_data}, state) do
203-
json = Poison.decode!(frame_data)
203+
json = Jason.decode!(frame_data)
204204
id = json["id"]
205205
method = json["method"]
206206

@@ -227,14 +227,14 @@ defmodule ChromeRemoteInterface.PageSession do
227227
"params" => params
228228
}
229229

230-
json = Poison.encode!(message)
230+
json = Jason.encode!(message)
231231
WebSockex.send_frame(socket, {:text, json})
232232
{:noreply, state}
233233
end
234234

235235
defp add_callback(state, from) do
236236
state
237-
|> Map.update(:callbacks, [{state.ref_id, from}], fn(callbacks) ->
237+
|> Map.update(:callbacks, [{state.ref_id, from}], fn callbacks ->
238238
[{state.ref_id, from} | callbacks]
239239
end)
240240
end
@@ -252,18 +252,20 @@ defmodule ChromeRemoteInterface.PageSession do
252252
defp send_rpc_response(callbacks, id, json) do
253253
error = json["error"]
254254

255-
Enum.find(callbacks, fn({ref_id, _from}) ->
255+
Enum.find(callbacks, fn {ref_id, _from} ->
256256
ref_id == id
257257
end)
258258
|> case do
259259
{_ref_id, {:cast, method, from}} = callback ->
260260
event = {:chrome_remote_interface, method, json}
261261
send(from, event)
262262
remove_callback(callbacks, callback)
263+
263264
{_ref_id, {:call, from}} = callback ->
264265
status = if error, do: :error, else: :ok
265266
GenServer.reply(from, {status, json})
266267
remove_callback(callbacks, callback)
268+
267269
_ ->
268270
callbacks
269271
end
@@ -277,7 +279,7 @@ defmodule ChromeRemoteInterface.PageSession do
277279
|> Map.get(event_name, [])
278280

279281
pids_subscribed_to_event
280-
|> Enum.each(&(send(&1, event)))
282+
|> Enum.each(&send(&1, event))
281283
end
282284

283285
def terminate(_reason, state) do

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule ChromeRemoteInterface.Mixfile do
2525
# Run "mix help deps" to learn about dependencies.
2626
defp deps do
2727
[
28-
{:poison, "~> 3.1"},
28+
{:jason, "~> 1.1"},
2929
{:hackney, "~> 1.8 or ~> 1.7 or ~> 1.6"},
3030
{:websockex, "~> 0.4.0"},
3131
{:ex_doc, "~> 0.19", only: :dev, runtime: false}

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"httpipe": {:hex, :httpipe, "0.9.0", "4db66493b0ec2a86d142ea959a62e221d6ddb23ab48a676b691be3a16c38a415", [:mix], []},
77
"httpipe_adapters_hackney": {:hex, :httpipe_adapters_hackney, "0.11.0", "35c31b96fd6fea117f9ba6ca70467ada111dffb9f2fa7fca0bfc7e12bb166e8e", [:mix], [{:hackney, "~> 1.8 or ~> 1.7 or ~> 1.6", [hex: :hackney, optional: false]}, {:httpipe, "~> 0.9.0", [hex: :httpipe, optional: false]}]},
88
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, optional: false]}]},
9+
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
910
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
1011
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
1112
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},

test/page_session_test.exs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ defmodule ChromeRemoteInterface.PageSessionTest do
1616
state = %PageSession{}
1717
state = subscribe_to_test_event(state)
1818

19-
{:reply, :ok, state} = PageSession.handle_call(
20-
{:unsubscribe, "TestEvent", self()},
21-
self(),
22-
state
23-
)
19+
{:reply, :ok, state} =
20+
PageSession.handle_call(
21+
{:unsubscribe, "TestEvent", self()},
22+
self(),
23+
state
24+
)
2425

2526
fire_test_event(state)
2627

@@ -31,11 +32,12 @@ defmodule ChromeRemoteInterface.PageSessionTest do
3132
state = %PageSession{}
3233
state = subscribe_to_test_event(state)
3334

34-
{:reply, :ok, state} = PageSession.handle_call(
35-
{:unsubscribe_all, self()},
36-
self(),
37-
state
38-
)
35+
{:reply, :ok, state} =
36+
PageSession.handle_call(
37+
{:unsubscribe_all, self()},
38+
self(),
39+
state
40+
)
3941

4042
fire_test_event(state)
4143

@@ -49,7 +51,9 @@ defmodule ChromeRemoteInterface.PageSessionTest do
4951
from = {make_ref(), self()}
5052

5153
state = %PageSession{socket: websocket}
52-
{:noreply, state} = PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)
54+
55+
{:noreply, state} =
56+
PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)
5357

5458
assert [{_ref, {:call, _from}}] = state.callbacks
5559
end
@@ -59,37 +63,41 @@ defmodule ChromeRemoteInterface.PageSessionTest do
5963
from = {self(), make_ref()}
6064

6165
state = %PageSession{socket: websocket}
62-
{:noreply, state} = PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)
66+
67+
{:noreply, state} =
68+
PageSession.handle_call({:call_command, "TestCommand", %{}}, from, state)
6369

6470
assert [{ref, {:call, _from}}] = state.callbacks
6571

66-
frame = %{"id" => ref, "result" => %{"data" => %{"foo" => "bar"}}} |> Poison.encode!()
72+
frame = %{"id" => ref, "result" => %{"data" => %{"foo" => "bar"}}} |> Jason.encode!()
6773
{:noreply, state} = PageSession.handle_info({:message, frame}, state)
6874

6975
assert [] = state.callbacks
7076
end
7177
end
7278

7379
def subscribe_to_test_event(state) do
74-
{:reply, :ok, state} = PageSession.handle_call(
75-
{:subscribe, "TestEvent", self()},
76-
self(),
77-
state
78-
)
80+
{:reply, :ok, state} =
81+
PageSession.handle_call(
82+
{:subscribe, "TestEvent", self()},
83+
self(),
84+
state
85+
)
7986

8087
state
8188
end
8289

8390
def fire_test_event(state) do
84-
json = Poison.encode!(%{
85-
method: "TestEvent"
86-
})
91+
json =
92+
Jason.encode!(%{
93+
method: "TestEvent"
94+
})
8795

8896
PageSession.handle_info({:message, json}, state)
8997
end
9098

9199
def spawn_fake_websocket() do
92-
spawn_link(fn() ->
100+
spawn_link(fn ->
93101
receive do
94102
{:"$websockex_send", from, _frame} -> GenServer.reply(from, :ok)
95103
end

0 commit comments

Comments
 (0)