Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: blimp/client/feature/navigation_feature.cc

Issue 2058263002: Tied up BlimpNavigationController to NavigationFeature (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp_core
Patch Set: Removed BlimpContentsTest.java Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "blimp/client/feature/navigation_feature.h"
6
7 #include <map>
8 #include <string>
9
10 #include "blimp/common/create_blimp_message.h"
11 #include "blimp/common/proto/blimp_message.pb.h"
12 #include "blimp/common/proto/navigation.pb.h"
13 #include "components/url_formatter/url_fixer.h"
14 #include "net/base/net_errors.h"
15 #include "url/gurl.h"
16 #include "url/url_canon.h"
17 #include "url/url_util.h"
18
19 namespace blimp {
20 namespace client {
21
22 NavigationFeature::NavigationFeature() {}
23
24 NavigationFeature::~NavigationFeature() {}
25
26 void NavigationFeature::set_outgoing_message_processor(
27 std::unique_ptr<BlimpMessageProcessor> processor) {
28 outgoing_message_processor_ = std::move(processor);
29 }
30
31 void NavigationFeature::SetDelegate(int tab_id,
32 NavigationFeatureDelegate* delegate) {
33 DCHECK(!FindDelegate(tab_id));
34 delegates_[tab_id] = delegate;
35 }
36
37 void NavigationFeature::RemoveDelegate(int tab_id) {
38 DelegateMap::iterator it = delegates_.find(tab_id);
39 if (it != delegates_.end())
40 delegates_.erase(it);
41 }
42
43 void NavigationFeature::NavigateToUrlText(int tab_id,
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
62 NavigationMessage* navigation_message;
63 std::unique_ptr<BlimpMessage> blimp_message =
64 CreateBlimpMessage(&navigation_message, tab_id);
65 navigation_message->set_type(NavigationMessage::LOAD_URL);
66 navigation_message->mutable_load_url()->set_url(url.spec());
67 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
68 net::CompletionCallback());
69 }
70
71 void NavigationFeature::Reload(int tab_id) {
72 NavigationMessage* navigation_message;
73 std::unique_ptr<BlimpMessage> blimp_message =
74 CreateBlimpMessage(&navigation_message, tab_id);
75 navigation_message->set_type(NavigationMessage::RELOAD);
76
77 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
78 net::CompletionCallback());
79 }
80
81 void NavigationFeature::GoForward(int tab_id) {
82 NavigationMessage* navigation_message;
83 std::unique_ptr<BlimpMessage> blimp_message =
84 CreateBlimpMessage(&navigation_message, tab_id);
85 navigation_message->set_type(NavigationMessage::GO_FORWARD);
86
87 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
88 net::CompletionCallback());
89 }
90
91 void NavigationFeature::GoBack(int tab_id) {
92 NavigationMessage* navigation_message;
93 std::unique_ptr<BlimpMessage> blimp_message =
94 CreateBlimpMessage(&navigation_message, tab_id);
95 navigation_message->set_type(NavigationMessage::GO_BACK);
96
97 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
98 net::CompletionCallback());
99 }
100
101 void NavigationFeature::ProcessMessage(
102 std::unique_ptr<BlimpMessage> message,
103 const net::CompletionCallback& callback) {
104 DCHECK(!callback.is_null());
105 DCHECK_EQ(BlimpMessage::kNavigation, message->feature_case());
106
107 int tab_id = message->target_tab_id();
108 const NavigationMessage& navigation_message = message->navigation();
109
110 NavigationFeatureDelegate* delegate = FindDelegate(tab_id);
111 DCHECK(delegate) << "NavigationFeatureDelegate not found for tab " << tab_id;
112 switch (navigation_message.type()) {
113 case NavigationMessage::NAVIGATION_STATE_CHANGED: {
114 const NavigationStateChangeMessage& details =
115 navigation_message.navigation_state_changed();
116 if (details.has_url())
117 delegate->OnUrlChanged(tab_id, GURL(details.url()));
118
119 if (details.has_title())
120 delegate->OnTitleChanged(tab_id, details.title());
121
122 if (details.has_loading())
123 delegate->OnLoadingChanged(tab_id, details.loading());
124
125 if (details.has_favicon()) {
126 NOTIMPLEMENTED();
127 }
128
129 if (details.has_page_load_completed()) {
130 delegate->OnPageLoadStatusUpdate(tab_id,
131 details.page_load_completed());
132 }
133 } break;
134 case NavigationMessage::LOAD_URL:
135 case NavigationMessage::GO_BACK:
136 case NavigationMessage::GO_FORWARD:
137 case NavigationMessage::RELOAD:
138 NOTREACHED() << "Client received unexpected navigation type.";
139 break;
140 case NavigationMessage::UNKNOWN:
141 NOTREACHED();
142 }
143
144 callback.Run(net::OK);
145 }
146
147 NavigationFeature::NavigationFeatureDelegate* NavigationFeature::FindDelegate(
148 const int tab_id) {
149 DelegateMap::const_iterator it = delegates_.find(tab_id);
150 if (it != delegates_.end())
151 return it->second;
152 return nullptr;
153 }
154
155 } // namespace client
156 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/client/feature/navigation_feature.h ('k') | blimp/client/feature/navigation_feature_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698