| 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 #include "blimp/client/feature/navigation_feature.h" | 5 #include "blimp/client/feature/navigation_feature.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "blimp/common/create_blimp_message.h" | 10 #include "blimp/common/create_blimp_message.h" |
| 11 #include "blimp/common/proto/blimp_message.pb.h" | 11 #include "blimp/common/proto/blimp_message.pb.h" |
| 12 #include "blimp/common/proto/navigation.pb.h" | 12 #include "blimp/common/proto/navigation.pb.h" |
| 13 #include "components/url_formatter/url_fixer.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 #include "url/url_canon.h" |
| 17 #include "url/url_util.h" |
| 15 | 18 |
| 16 namespace blimp { | 19 namespace blimp { |
| 17 namespace client { | 20 namespace client { |
| 18 | 21 |
| 19 NavigationFeature::NavigationFeature() {} | 22 NavigationFeature::NavigationFeature() {} |
| 20 | 23 |
| 21 NavigationFeature::~NavigationFeature() {} | 24 NavigationFeature::~NavigationFeature() {} |
| 22 | 25 |
| 23 void NavigationFeature::set_outgoing_message_processor( | 26 void NavigationFeature::set_outgoing_message_processor( |
| 24 std::unique_ptr<BlimpMessageProcessor> processor) { | 27 std::unique_ptr<BlimpMessageProcessor> processor) { |
| 25 outgoing_message_processor_ = std::move(processor); | 28 outgoing_message_processor_ = std::move(processor); |
| 26 } | 29 } |
| 27 | 30 |
| 28 void NavigationFeature::SetDelegate(int tab_id, | 31 void NavigationFeature::SetDelegate(int tab_id, |
| 29 NavigationFeatureDelegate* delegate) { | 32 NavigationFeatureDelegate* delegate) { |
| 30 DCHECK(!FindDelegate(tab_id)); | 33 DCHECK(!FindDelegate(tab_id)); |
| 31 delegates_[tab_id] = delegate; | 34 delegates_[tab_id] = delegate; |
| 32 } | 35 } |
| 33 | 36 |
| 34 void NavigationFeature::RemoveDelegate(int tab_id) { | 37 void NavigationFeature::RemoveDelegate(int tab_id) { |
| 35 DelegateMap::iterator it = delegates_.find(tab_id); | 38 DelegateMap::iterator it = delegates_.find(tab_id); |
| 36 if (it != delegates_.end()) | 39 if (it != delegates_.end()) |
| 37 delegates_.erase(it); | 40 delegates_.erase(it); |
| 38 } | 41 } |
| 39 | 42 |
| 40 void NavigationFeature::NavigateToUrlText(int tab_id, | 43 void NavigationFeature::NavigateToUrlText(int tab_id, |
| 41 const std::string& url_text) { | 44 const std::string& url_text) { |
| 45 // Fixes up url, e.g., convert "google.com" to "http://google.com". |
| 46 // It also converts "example" to "http://example/" (a valid GURL but no |
| 47 // website). In order to use search instead in this case, we check |
| 48 // the host part of the URL to see if it contains '.', and use search |
| 49 // instead if it does not. Note that this heuristic is not correct, since |
| 50 // valid GURL such as "chrome://version/" do not have '.'. The heuristic is |
| 51 // used only for v0.5 and useful for dev&test. |
| 52 // TODO(haibinlu): Remove once omnibox is used. |
| 53 GURL url = url_formatter::FixupURL(url_text, std::string()); |
| 54 if (!url.is_valid() || url.host_piece().find('.') == std::string::npos) { |
| 55 url::RawCanonOutputT<char> buffer; |
| 56 url::EncodeURIComponent(url_text.data(), url_text.size(), &buffer); |
| 57 std::string encoded_query(buffer.data(), buffer.length()); |
| 58 url = GURL("https://www.google.com/#q=" + encoded_query); |
| 59 DCHECK(url.is_valid()); |
| 60 } |
| 61 |
| 42 NavigationMessage* navigation_message; | 62 NavigationMessage* navigation_message; |
| 43 std::unique_ptr<BlimpMessage> blimp_message = | 63 std::unique_ptr<BlimpMessage> blimp_message = |
| 44 CreateBlimpMessage(&navigation_message, tab_id); | 64 CreateBlimpMessage(&navigation_message, tab_id); |
| 45 navigation_message->set_type(NavigationMessage::LOAD_URL); | 65 navigation_message->set_type(NavigationMessage::LOAD_URL); |
| 46 navigation_message->mutable_load_url()->set_url(url_text); | 66 navigation_message->mutable_load_url()->set_url(url.spec()); |
| 47 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | 67 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| 48 net::CompletionCallback()); | 68 net::CompletionCallback()); |
| 49 } | 69 } |
| 50 | 70 |
| 51 void NavigationFeature::Reload(int tab_id) { | 71 void NavigationFeature::Reload(int tab_id) { |
| 52 NavigationMessage* navigation_message; | 72 NavigationMessage* navigation_message; |
| 53 std::unique_ptr<BlimpMessage> blimp_message = | 73 std::unique_ptr<BlimpMessage> blimp_message = |
| 54 CreateBlimpMessage(&navigation_message, tab_id); | 74 CreateBlimpMessage(&navigation_message, tab_id); |
| 55 navigation_message->set_type(NavigationMessage::RELOAD); | 75 navigation_message->set_type(NavigationMessage::RELOAD); |
| 56 | 76 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 NavigationFeature::NavigationFeatureDelegate* NavigationFeature::FindDelegate( | 148 NavigationFeature::NavigationFeatureDelegate* NavigationFeature::FindDelegate( |
| 129 const int tab_id) { | 149 const int tab_id) { |
| 130 DelegateMap::const_iterator it = delegates_.find(tab_id); | 150 DelegateMap::const_iterator it = delegates_.find(tab_id); |
| 131 if (it != delegates_.end()) | 151 if (it != delegates_.end()) |
| 132 return it->second; | 152 return it->second; |
| 133 return nullptr; | 153 return nullptr; |
| 134 } | 154 } |
| 135 | 155 |
| 136 } // namespace client | 156 } // namespace client |
| 137 } // namespace blimp | 157 } // namespace blimp |
| OLD | NEW |