| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "services/navigation/public/cpp/view.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "services/navigation/public/cpp/view_delegate.h" | |
| 11 #include "services/navigation/public/cpp/view_observer.h" | |
| 12 #include "ui/aura/mus/window_port_mus.h" | |
| 13 #include "ui/aura/window.h" | |
| 14 | |
| 15 namespace navigation { | |
| 16 namespace { | |
| 17 | |
| 18 // Callback with result of Embed(). | |
| 19 void EmbedCallback(bool result) {} | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 //////////////////////////////////////////////////////////////////////////////// | |
| 24 // View, public: | |
| 25 | |
| 26 View::View(mojom::ViewFactoryPtr factory) : binding_(this) { | |
| 27 mojom::ViewClientPtr client; | |
| 28 binding_.Bind(MakeRequest(&client)); | |
| 29 factory->CreateView(std::move(client), MakeRequest(&view_)); | |
| 30 } | |
| 31 | |
| 32 View::View(mojom::ViewPtr view, mojom::ViewClientRequest request) | |
| 33 : view_(std::move(view)), binding_(this, std::move(request)) {} | |
| 34 | |
| 35 View::~View() {} | |
| 36 | |
| 37 void View::AddObserver(ViewObserver* observer) { | |
| 38 observers_.AddObserver(observer); | |
| 39 } | |
| 40 | |
| 41 void View::RemoveObserver(ViewObserver* observer) { | |
| 42 observers_.RemoveObserver(observer); | |
| 43 } | |
| 44 | |
| 45 void View::NavigateToURL(const GURL& url) { | |
| 46 view_->NavigateTo(url); | |
| 47 } | |
| 48 | |
| 49 void View::NavigateToOffset(int offset) { | |
| 50 view_->NavigateToOffset(offset); | |
| 51 } | |
| 52 | |
| 53 void View::GoBack() { | |
| 54 if (can_go_back_) | |
| 55 view_->GoBack(); | |
| 56 } | |
| 57 | |
| 58 void View::GoForward() { | |
| 59 if (can_go_forward_) | |
| 60 view_->GoForward(); | |
| 61 } | |
| 62 | |
| 63 void View::GetBackMenuItems(std::vector<NavigationListItem>* items) { | |
| 64 DCHECK(items); | |
| 65 for (int i = navigation_list_cursor_ - 1, offset = -1; i >= 0; | |
| 66 --i, --offset) { | |
| 67 items->push_back(NavigationListItem( | |
| 68 base::UTF8ToUTF16(navigation_list_[i]->title), offset)); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void View::GetForwardMenuItems(std::vector<NavigationListItem>* items) { | |
| 73 DCHECK(items); | |
| 74 for (int i = navigation_list_cursor_ + 1, offset = 1; | |
| 75 i < static_cast<int>(navigation_list_.size()); ++i, ++offset) { | |
| 76 items->push_back(NavigationListItem( | |
| 77 base::UTF8ToUTF16(navigation_list_[i]->title), offset)); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void View::Reload(bool bypass_cache) { | |
| 82 view_->Reload(bypass_cache); | |
| 83 } | |
| 84 | |
| 85 void View::Stop() { | |
| 86 view_->Stop(); | |
| 87 } | |
| 88 | |
| 89 void View::ShowInterstitial(const std::string& html) { | |
| 90 view_->ShowInterstitial(html); | |
| 91 } | |
| 92 | |
| 93 void View::HideInterstitial() { | |
| 94 view_->HideInterstitial(); | |
| 95 } | |
| 96 | |
| 97 void View::EmbedInWindow(aura::Window* parent) { | |
| 98 ui::mojom::WindowTreeClientPtr client; | |
| 99 view_->GetWindowTreeClient(MakeRequest(&client)); | |
| 100 const uint32_t embed_flags = 0u; // Nothing special. | |
| 101 aura::WindowPortMus::Get(parent)->Embed(std::move(client), embed_flags, | |
| 102 base::Bind(&EmbedCallback)); | |
| 103 } | |
| 104 | |
| 105 //////////////////////////////////////////////////////////////////////////////// | |
| 106 // View, mojom::ViewClient implementation: | |
| 107 | |
| 108 void View::OpenURL(mojom::OpenURLParamsPtr params) { | |
| 109 if (delegate_) | |
| 110 delegate_->OpenURL(this, std::move(params)); | |
| 111 } | |
| 112 | |
| 113 void View::LoadingStateChanged(bool is_loading) { | |
| 114 is_loading_ = is_loading; | |
| 115 for (auto& observer : observers_) | |
| 116 observer.LoadingStateChanged(this); | |
| 117 } | |
| 118 | |
| 119 void View::NavigationStateChanged(const GURL& url, | |
| 120 const std::string& title, | |
| 121 bool can_go_back, | |
| 122 bool can_go_forward) { | |
| 123 url_ = url; | |
| 124 title_ = base::UTF8ToUTF16(title); | |
| 125 can_go_back_ = can_go_back; | |
| 126 can_go_forward_ = can_go_forward; | |
| 127 for (auto& observer : observers_) | |
| 128 observer.NavigationStateChanged(this); | |
| 129 } | |
| 130 | |
| 131 void View::LoadProgressChanged(double progress) { | |
| 132 for (auto& observer : observers_) | |
| 133 observer.LoadProgressChanged(this, progress); | |
| 134 } | |
| 135 | |
| 136 void View::UpdateHoverURL(const GURL& url) { | |
| 137 for (auto& observer : observers_) | |
| 138 observer.HoverTargetURLChanged(this, url); | |
| 139 } | |
| 140 | |
| 141 void View::ViewCreated(mojom::ViewPtr view, | |
| 142 mojom::ViewClientRequest request, | |
| 143 bool is_popup, | |
| 144 const gfx::Rect& initial_bounds, | |
| 145 bool user_gesture) { | |
| 146 if (delegate_) { | |
| 147 delegate_->ViewCreated( | |
| 148 this, base::WrapUnique(new View(std::move(view), std::move(request))), | |
| 149 is_popup, initial_bounds, user_gesture); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void View::Close() { | |
| 154 if (delegate_) | |
| 155 delegate_->Close(this); | |
| 156 } | |
| 157 | |
| 158 void View::NavigationPending(mojom::NavigationEntryPtr entry) { | |
| 159 pending_navigation_ = std::move(entry); | |
| 160 } | |
| 161 | |
| 162 void View::NavigationCommitted(mojom::NavigationCommittedDetailsPtr details, | |
| 163 int current_index) { | |
| 164 switch (details->type) { | |
| 165 case mojom::NavigationType::NEW_PAGE: | |
| 166 navigation_list_.push_back(std::move(pending_navigation_)); | |
| 167 navigation_list_cursor_ = current_index; | |
| 168 break; | |
| 169 case mojom::NavigationType::EXISTING_PAGE: | |
| 170 navigation_list_cursor_ = current_index; | |
| 171 break; | |
| 172 default: | |
| 173 break; | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void View::NavigationEntryChanged(mojom::NavigationEntryPtr entry, | |
| 178 int entry_index) { | |
| 179 navigation_list_[entry_index] = std::move(entry); | |
| 180 } | |
| 181 | |
| 182 void View::NavigationListPruned(bool from_front, int count) { | |
| 183 DCHECK(count < static_cast<int>(navigation_list_.size())); | |
| 184 if (from_front) { | |
| 185 auto it = navigation_list_.begin() + count; | |
| 186 navigation_list_.erase(navigation_list_.begin(), it); | |
| 187 } else { | |
| 188 auto it = navigation_list_.end() - count; | |
| 189 navigation_list_.erase(it, navigation_list_.end()); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 } // namespace navigation | |
| OLD | NEW |