| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 |  | 
| 3 // found in the LICENSE file. |  | 
| 4 |  | 
| 5 #include "components/html_viewer/document_resource_waiter.h" |  | 
| 6 |  | 
| 7 #include <utility> |  | 
| 8 |  | 
| 9 #include "components/html_viewer/global_state.h" |  | 
| 10 #include "components/html_viewer/html_document.h" |  | 
| 11 #include "components/html_viewer/html_frame_tree_manager.h" |  | 
| 12 #include "components/mus/public/cpp/window.h" |  | 
| 13 |  | 
| 14 using web_view::mojom::WindowConnectType; |  | 
| 15 |  | 
| 16 namespace html_viewer { |  | 
| 17 |  | 
| 18 DocumentResourceWaiter::DocumentResourceWaiter(GlobalState* global_state, |  | 
| 19                                                mojo::URLResponsePtr response, |  | 
| 20                                                HTMLDocument* document) |  | 
| 21     : global_state_(global_state), |  | 
| 22       document_(document), |  | 
| 23       response_(std::move(response)), |  | 
| 24       root_(nullptr), |  | 
| 25       change_id_(0u), |  | 
| 26       window_id_(0u), |  | 
| 27       window_connect_type_(web_view::mojom::WindowConnectType::USE_NEW), |  | 
| 28       frame_client_binding_(this), |  | 
| 29       is_ready_(false), |  | 
| 30       waiting_for_change_id_(false), |  | 
| 31       target_frame_tree_(nullptr) {} |  | 
| 32 |  | 
| 33 DocumentResourceWaiter::~DocumentResourceWaiter() { |  | 
| 34   if (root_) |  | 
| 35     root_->RemoveObserver(this); |  | 
| 36   if (target_frame_tree_) |  | 
| 37     target_frame_tree_->RemoveObserver(this); |  | 
| 38 } |  | 
| 39 |  | 
| 40 void DocumentResourceWaiter::Release( |  | 
| 41     mojo::InterfaceRequest<web_view::mojom::FrameClient>* frame_client_request, |  | 
| 42     web_view::mojom::FramePtr* frame, |  | 
| 43     mojo::Array<web_view::mojom::FrameDataPtr>* frame_data, |  | 
| 44     uint32_t* change_id, |  | 
| 45     uint32_t* window_id, |  | 
| 46     WindowConnectType* window_connect_type, |  | 
| 47     OnConnectCallback* on_connect_callback) { |  | 
| 48   DCHECK(is_ready_); |  | 
| 49   *frame_client_request = std::move(frame_client_request_); |  | 
| 50   *frame = std::move(frame_); |  | 
| 51   *frame_data = std::move(frame_data_); |  | 
| 52   *change_id = change_id_; |  | 
| 53   *window_id = window_id_; |  | 
| 54   *window_connect_type = window_connect_type_; |  | 
| 55   *on_connect_callback = on_connect_callback_; |  | 
| 56 } |  | 
| 57 |  | 
| 58 mojo::URLResponsePtr DocumentResourceWaiter::ReleaseURLResponse() { |  | 
| 59   return std::move(response_); |  | 
| 60 } |  | 
| 61 |  | 
| 62 void DocumentResourceWaiter::SetRoot(mus::Window* root) { |  | 
| 63   DCHECK(!root_); |  | 
| 64   root_ = root; |  | 
| 65   root_->AddObserver(this); |  | 
| 66   UpdateIsReady(); |  | 
| 67 } |  | 
| 68 |  | 
| 69 void DocumentResourceWaiter::Bind( |  | 
| 70     mojo::InterfaceRequest<web_view::mojom::FrameClient> request) { |  | 
| 71   if (frame_client_binding_.is_bound() || !frame_data_.is_null()) { |  | 
| 72     DVLOG(1) << "Request for FrameClient after already supplied one"; |  | 
| 73     return; |  | 
| 74   } |  | 
| 75   frame_client_binding_.Bind(std::move(request)); |  | 
| 76 } |  | 
| 77 |  | 
| 78 void DocumentResourceWaiter::UpdateIsReady() { |  | 
| 79   if (is_ready_) |  | 
| 80     return; |  | 
| 81 |  | 
| 82   // See description of |waiting_for_change_id_| for details. |  | 
| 83   if (waiting_for_change_id_) { |  | 
| 84     if (target_frame_tree_->change_id() == change_id_) { |  | 
| 85       is_ready_ = true; |  | 
| 86       waiting_for_change_id_ = false; |  | 
| 87       document_->Load(); |  | 
| 88     } |  | 
| 89     return; |  | 
| 90   } |  | 
| 91 |  | 
| 92   // The first portion of ready is when we have received OnConnect() |  | 
| 93   // (|frame_data_| is valid) and we have a window with valid metrics. The |  | 
| 94   // window is not necessary is WindowConnectType is USE_EXISTING, which means |  | 
| 95   // the |  | 
| 96   // application is not asked for a ViewTreeClient. The metrics are necessary |  | 
| 97   // to initialize ResourceBundle. If USE_EXISTING is true, it means a Window |  | 
| 98   // has already been provided to another HTMLDocument and there is no need to |  | 
| 99   // wait for metrics. |  | 
| 100   bool is_ready = |  | 
| 101       (!frame_data_.is_null() && |  | 
| 102        ((window_connect_type_ == |  | 
| 103          web_view::mojom::WindowConnectType::USE_EXISTING) || |  | 
| 104         (root_ && root_->viewport_metrics().device_pixel_ratio != 0.0f))); |  | 
| 105   if (is_ready) { |  | 
| 106     HTMLFrameTreeManager* frame_tree = |  | 
| 107         HTMLFrameTreeManager::FindFrameTreeWithRoot(frame_data_[0]->frame_id); |  | 
| 108     // Once we've received OnConnect() and the window (if necessary), we |  | 
| 109     // determine which HTMLFrameTreeManager the new frame ends up in. If there |  | 
| 110     // is an existing HTMLFrameTreeManager then we must wait for the change_id |  | 
| 111     // supplied to OnConnect() to be <= that of the HTMLFrameTreeManager's |  | 
| 112     // change_id. If we did not wait for the change id to be <= then the |  | 
| 113     // structure of the tree is not in the expected state and it's possible the |  | 
| 114     // frame communicated in OnConnect() does not exist yet. |  | 
| 115     if (frame_tree && change_id_ > frame_tree->change_id()) { |  | 
| 116       waiting_for_change_id_ = true; |  | 
| 117       target_frame_tree_ = frame_tree; |  | 
| 118       target_frame_tree_->AddObserver(this); |  | 
| 119     } else { |  | 
| 120       is_ready_ = true; |  | 
| 121       document_->Load(); |  | 
| 122     } |  | 
| 123   } |  | 
| 124 } |  | 
| 125 |  | 
| 126 void DocumentResourceWaiter::OnConnect( |  | 
| 127     web_view::mojom::FramePtr frame, |  | 
| 128     uint32_t change_id, |  | 
| 129     uint32_t window_id, |  | 
| 130     WindowConnectType window_connect_type, |  | 
| 131     mojo::Array<web_view::mojom::FrameDataPtr> frame_data, |  | 
| 132     int64_t navigation_start_time_ticks, |  | 
| 133     const OnConnectCallback& callback) { |  | 
| 134   DCHECK(frame_data_.is_null()); |  | 
| 135   change_id_ = change_id; |  | 
| 136   window_id_ = window_id; |  | 
| 137   window_connect_type_ = window_connect_type; |  | 
| 138   frame_ = std::move(frame); |  | 
| 139   frame_data_ = std::move(frame_data); |  | 
| 140   navigation_start_time_ = |  | 
| 141       base::TimeTicks::FromInternalValue(navigation_start_time_ticks); |  | 
| 142   on_connect_callback_ = callback; |  | 
| 143   CHECK(frame_data_.size() > 0u); |  | 
| 144   frame_client_request_ = frame_client_binding_.Unbind(); |  | 
| 145   UpdateIsReady(); |  | 
| 146 } |  | 
| 147 |  | 
| 148 void DocumentResourceWaiter::OnFrameAdded( |  | 
| 149     uint32_t change_id, |  | 
| 150     web_view::mojom::FrameDataPtr frame_data) { |  | 
| 151   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 152   NOTREACHED(); |  | 
| 153 } |  | 
| 154 |  | 
| 155 void DocumentResourceWaiter::OnFrameRemoved(uint32_t change_id, |  | 
| 156                                             uint32_t frame_id) { |  | 
| 157   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 158   NOTREACHED(); |  | 
| 159 } |  | 
| 160 |  | 
| 161 void DocumentResourceWaiter::OnFrameClientPropertyChanged( |  | 
| 162     uint32_t frame_id, |  | 
| 163     const mojo::String& name, |  | 
| 164     mojo::Array<uint8_t> new_value) { |  | 
| 165   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 166   NOTREACHED(); |  | 
| 167 } |  | 
| 168 |  | 
| 169 void DocumentResourceWaiter::OnPostMessageEvent( |  | 
| 170     uint32_t source_frame_id, |  | 
| 171     uint32_t target_frame_id, |  | 
| 172     web_view::mojom::HTMLMessageEventPtr event) { |  | 
| 173   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 174   NOTREACHED(); |  | 
| 175 } |  | 
| 176 |  | 
| 177 void DocumentResourceWaiter::OnWillNavigate( |  | 
| 178     const mojo::String& origin, |  | 
| 179     const OnWillNavigateCallback& callback) { |  | 
| 180   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 181   NOTREACHED(); |  | 
| 182 } |  | 
| 183 |  | 
| 184 void DocumentResourceWaiter::OnFrameLoadingStateChanged(uint32_t frame_id, |  | 
| 185                                                         bool loading) { |  | 
| 186   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 187   NOTREACHED(); |  | 
| 188 } |  | 
| 189 |  | 
| 190 void DocumentResourceWaiter::OnDispatchFrameLoadEvent(uint32_t frame_id) { |  | 
| 191   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 192   NOTREACHED(); |  | 
| 193 } |  | 
| 194 |  | 
| 195 void DocumentResourceWaiter::Find(int32_t request_id, |  | 
| 196                                   const mojo::String& search_text, |  | 
| 197                                   web_view::mojom::FindOptionsPtr options, |  | 
| 198                                   bool wrap_within_frame, |  | 
| 199                                   const FindCallback& callback) { |  | 
| 200   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 201   NOTREACHED(); |  | 
| 202 } |  | 
| 203 |  | 
| 204 void DocumentResourceWaiter::StopFinding(bool clear_selection) { |  | 
| 205   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 206   NOTREACHED(); |  | 
| 207 } |  | 
| 208 |  | 
| 209 void DocumentResourceWaiter::HighlightFindResults( |  | 
| 210     int32_t request_id, |  | 
| 211     const mojo::String& search_test, |  | 
| 212     web_view::mojom::FindOptionsPtr options, |  | 
| 213     bool reset) { |  | 
| 214   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 215   NOTREACHED(); |  | 
| 216 } |  | 
| 217 |  | 
| 218 void DocumentResourceWaiter::StopHighlightingFindResults() { |  | 
| 219   // It is assumed we receive OnConnect() (which unbinds) before anything else. |  | 
| 220   NOTREACHED(); |  | 
| 221 } |  | 
| 222 |  | 
| 223 void DocumentResourceWaiter::OnWindowViewportMetricsChanged( |  | 
| 224     mus::Window* window, |  | 
| 225     const mus::mojom::ViewportMetrics& old_metrics, |  | 
| 226     const mus::mojom::ViewportMetrics& new_metrics) { |  | 
| 227   UpdateIsReady(); |  | 
| 228 } |  | 
| 229 |  | 
| 230 void DocumentResourceWaiter::OnWindowDestroyed(mus::Window* window) { |  | 
| 231   root_->RemoveObserver(this); |  | 
| 232   root_ = nullptr; |  | 
| 233 } |  | 
| 234 |  | 
| 235 void DocumentResourceWaiter::OnHTMLFrameTreeManagerChangeIdAdvanced() { |  | 
| 236   UpdateIsReady(); |  | 
| 237 } |  | 
| 238 |  | 
| 239 void DocumentResourceWaiter::OnHTMLFrameTreeManagerDestroyed() { |  | 
| 240   document_->Destroy();  // This destroys us. |  | 
| 241 } |  | 
| 242 |  | 
| 243 }  // namespace html_viewer |  | 
| OLD | NEW | 
|---|