Brian Silverman | 4e662aa | 2022-05-11 23:10:19 -0700 | [diff] [blame^] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | #include "fake-chromium-header.h" |
| 10 | #include <map> |
| 11 | #include <algorithm> |
| 12 | |
| 13 | using namespace content; |
| 14 | |
| 15 | // This is all appalling. None of this is real Chromium code. |
| 16 | // It's just designed to be the bare minimum required |
| 17 | // to knock together a quick Rust-side demo. In some future realities, all |
| 18 | // this is replaced with real Chromium code. |
| 19 | |
| 20 | int latest_rfh_id = 0; |
| 21 | std::map<int, RenderFrameHost *> render_frame_hosts; |
| 22 | WebContentsImpl *the_only_web_contents; // for this daft demo |
| 23 | |
| 24 | CreateParams::CreateParams(const std::string &main_frame_name) |
| 25 | : main_frame_name_(main_frame_name) {} |
| 26 | |
| 27 | RenderFrameHost *RenderFrameHost::FromId(int, int frame_id) { |
| 28 | return render_frame_hosts.at(frame_id); |
| 29 | } |
| 30 | |
| 31 | class RenderFrameHostImpl : public RenderFrameHost { |
| 32 | public: |
| 33 | RenderFrameHostImpl(const std::string name, int routing_id) |
| 34 | : routing_id_(routing_id), name_(name) {} |
| 35 | virtual int GetRoutingID() { return routing_id_; } |
| 36 | virtual std::string GetFrameName() { return name_; } |
| 37 | |
| 38 | private: |
| 39 | int routing_id_; |
| 40 | std::string name_; |
| 41 | }; |
| 42 | |
| 43 | std::unique_ptr<WebContents> WebContents::Create(const CreateParams ¶ms) { |
| 44 | auto wc = std::make_unique<WebContentsImpl>(params); |
| 45 | the_only_web_contents = wc.get(); |
| 46 | return wc; |
| 47 | } |
| 48 | |
| 49 | WebContentsImpl::WebContentsImpl(const CreateParams ¶ms) |
| 50 | : title_(params.main_frame_name_) { |
| 51 | int id = latest_rfh_id++; |
| 52 | std::unique_ptr<RenderFrameHost> new_rfh( |
| 53 | new RenderFrameHostImpl(params.main_frame_name_, id)); |
| 54 | render_frame_hosts.insert( |
| 55 | std::pair<int, RenderFrameHost *>(id, new_rfh.get())); |
| 56 | for (auto obs : observers_) { |
| 57 | obs->RenderFrameCreated(new_rfh.get()); |
| 58 | } |
| 59 | rfhs_.push_back(std::move(new_rfh)); |
| 60 | } |
| 61 | |
| 62 | void WebContentsImpl::AddObserver(WebContentsObserver *observer) { |
| 63 | observers_.push_back(observer); |
| 64 | } |
| 65 | void WebContentsImpl::RemoveObserver(WebContentsObserver *observer) { |
| 66 | std::remove(std::begin(observers_), std::end(observers_), observer); |
| 67 | } |
| 68 | |
| 69 | void WebContentsImpl::DeleteRFH() { |
| 70 | for (auto obs : observers_) { |
| 71 | obs->RenderFrameDeleted(rfhs_[0].get()); |
| 72 | } |
| 73 | rfhs_.clear(); |
| 74 | } |
| 75 | |
| 76 | const std::string &WebContentsImpl::GetTitle() { return title_; } |
| 77 | |
| 78 | void SimulateRendererShutdown(int frame_id) { |
| 79 | render_frame_hosts.erase(frame_id); |
| 80 | the_only_web_contents->DeleteRFH(); |
| 81 | } |