OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/tab_contents/core_tab_helper.h" | 5 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_command_controller.h" | 16 #include "chrome/browser/ui/browser_command_controller.h" |
17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
20 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
21 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
22 #include "components/search_engines/template_url_service.h" | 22 #include "components/search_engines/template_url_service.h" |
23 #include "components/web_cache/browser/web_cache_manager.h" | 23 #include "components/web_cache/browser/web_cache_manager.h" |
24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 #include "extensions/browser/guest_view/guest_view_manager.h" | |
27 #include "net/base/load_states.h" | 28 #include "net/base/load_states.h" |
28 #include "net/http/http_request_headers.h" | 29 #include "net/http/http_request_headers.h" |
29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
30 | 31 |
31 using content::WebContents; | 32 using content::WebContents; |
32 | 33 |
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); | 34 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); |
34 | 35 |
35 CoreTabHelper::CoreTabHelper(WebContents* web_contents) | 36 CoreTabHelper::CoreTabHelper(WebContents* web_contents) |
36 : content::WebContentsObserver(web_contents), | 37 : content::WebContentsObserver(web_contents), |
37 delegate_(NULL), | 38 delegate_(NULL), |
38 content_restrictions_(0) { | 39 content_restrictions_(0) { |
39 } | 40 } |
40 | 41 |
41 CoreTabHelper::~CoreTabHelper() { | 42 CoreTabHelper::~CoreTabHelper() { |
42 } | 43 } |
43 | 44 |
44 base::string16 CoreTabHelper::GetDefaultTitle() { | 45 base::string16 CoreTabHelper::GetDefaultTitle() { |
45 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); | 46 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); |
46 } | 47 } |
47 | 48 |
48 base::string16 CoreTabHelper::GetStatusText() const { | 49 base::string16 CoreTabHelper::GetStatusText() const { |
49 if (!web_contents()->IsLoading() || | 50 base::string16 status_text; |
50 web_contents()->GetLoadState().state == net::LOAD_STATE_IDLE) { | 51 GetStatusTextForWebContents(&status_text, web_contents()); |
51 return base::string16(); | 52 return status_text; |
52 } | |
53 | |
54 switch (web_contents()->GetLoadState().state) { | |
55 case net::LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL: | |
56 case net::LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET: | |
57 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_SOCKET_SLOT); | |
58 case net::LOAD_STATE_WAITING_FOR_DELEGATE: | |
59 if (!web_contents()->GetLoadState().param.empty()) { | |
60 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_DELEGATE, | |
61 web_contents()->GetLoadState().param); | |
62 } else { | |
63 return l10n_util::GetStringUTF16( | |
64 IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC); | |
65 } | |
66 case net::LOAD_STATE_WAITING_FOR_CACHE: | |
67 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE); | |
68 case net::LOAD_STATE_WAITING_FOR_APPCACHE: | |
69 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE); | |
70 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL: | |
71 return | |
72 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); | |
73 case net::LOAD_STATE_DOWNLOADING_PROXY_SCRIPT: | |
74 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_DOWNLOADING_PROXY_SCRIPT); | |
75 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL: | |
76 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
77 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT: | |
78 return l10n_util::GetStringUTF16( | |
79 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
80 case net::LOAD_STATE_RESOLVING_HOST: | |
81 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST); | |
82 case net::LOAD_STATE_CONNECTING: | |
83 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING); | |
84 case net::LOAD_STATE_SSL_HANDSHAKE: | |
85 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE); | |
86 case net::LOAD_STATE_SENDING_REQUEST: | |
87 if (web_contents()->GetUploadSize()) { | |
88 return l10n_util::GetStringFUTF16Int( | |
89 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS, | |
90 static_cast<int>((100 * web_contents()->GetUploadPosition()) / | |
91 web_contents()->GetUploadSize())); | |
92 } else { | |
93 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST); | |
94 } | |
95 case net::LOAD_STATE_WAITING_FOR_RESPONSE: | |
96 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE, | |
97 web_contents()->GetLoadStateHost()); | |
98 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE | |
99 case net::LOAD_STATE_IDLE: | |
100 case net::LOAD_STATE_READING_RESPONSE: | |
101 break; | |
102 } | |
103 | |
104 return base::string16(); | |
105 } | 53 } |
106 | 54 |
107 void CoreTabHelper::OnCloseStarted() { | 55 void CoreTabHelper::OnCloseStarted() { |
108 if (close_start_time_.is_null()) | 56 if (close_start_time_.is_null()) |
109 close_start_time_ = base::TimeTicks::Now(); | 57 close_start_time_ = base::TimeTicks::Now(); |
110 } | 58 } |
111 | 59 |
112 void CoreTabHelper::OnCloseCanceled() { | 60 void CoreTabHelper::OnCloseCanceled() { |
113 close_start_time_ = base::TimeTicks(); | 61 close_start_time_ = base::TimeTicks(); |
114 before_unload_end_time_ = base::TimeTicks(); | 62 before_unload_end_time_ = base::TimeTicks(); |
(...skipping 13 matching lines...) Expand all Loading... | |
128 content_restrictions_ = content_restrictions; | 76 content_restrictions_ = content_restrictions; |
129 #if !defined(OS_ANDROID) | 77 #if !defined(OS_ANDROID) |
130 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 78 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
131 if (!browser) | 79 if (!browser) |
132 return; | 80 return; |
133 | 81 |
134 browser->command_controller()->ContentRestrictionsChanged(); | 82 browser->command_controller()->ContentRestrictionsChanged(); |
135 #endif | 83 #endif |
136 } | 84 } |
137 | 85 |
86 // static | |
87 bool CoreTabHelper::GetStatusTextForWebContents( | |
88 base::string16* status_text, content::WebContents* source) { | |
89 auto guest_manager = extensions::GuestViewManager::FromBrowserContext( | |
90 source->GetBrowserContext()); | |
91 if (!source->IsLoading() || | |
92 source->GetLoadState().state == net::LOAD_STATE_IDLE) { | |
93 return guest_manager->ForEachGuest( | |
94 source, base::Bind(&CoreTabHelper::GetStatusTextForWebContents, | |
sky
2015/01/29 23:44:18
This call (and 172ish) looks expensive. How often
Fady Samuel
2015/01/30 05:43:56
GuestViewManagers are created on first access. I g
| |
95 status_text)); | |
96 } | |
97 | |
98 switch (source->GetLoadState().state) { | |
99 case net::LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL: | |
100 case net::LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET: | |
101 *status_text = | |
102 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_SOCKET_SLOT); | |
103 return true; | |
104 case net::LOAD_STATE_WAITING_FOR_DELEGATE: | |
105 if (!source->GetLoadState().param.empty()) { | |
106 *status_text = l10n_util::GetStringFUTF16( | |
107 IDS_LOAD_STATE_WAITING_FOR_DELEGATE, | |
108 source->GetLoadState().param); | |
109 return true; | |
110 } else { | |
111 *status_text = l10n_util::GetStringUTF16( | |
112 IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC); | |
113 return true; | |
114 } | |
115 case net::LOAD_STATE_WAITING_FOR_CACHE: | |
116 *status_text = | |
117 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE); | |
118 return true; | |
119 case net::LOAD_STATE_WAITING_FOR_APPCACHE: | |
120 *status_text = | |
121 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE); | |
122 return true; | |
123 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL: | |
124 *status_text = | |
125 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); | |
126 return true; | |
127 case net::LOAD_STATE_DOWNLOADING_PROXY_SCRIPT: | |
128 *status_text = | |
129 l10n_util::GetStringUTF16(IDS_LOAD_STATE_DOWNLOADING_PROXY_SCRIPT); | |
130 return true; | |
131 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL: | |
132 *status_text = | |
133 l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL); | |
134 return true; | |
135 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT: | |
136 *status_text = l10n_util::GetStringUTF16( | |
137 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | |
138 return true; | |
139 case net::LOAD_STATE_RESOLVING_HOST: | |
140 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST); | |
141 return true; | |
142 case net::LOAD_STATE_CONNECTING: | |
143 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING); | |
144 return true; | |
145 case net::LOAD_STATE_SSL_HANDSHAKE: | |
146 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE); | |
147 return true; | |
148 case net::LOAD_STATE_SENDING_REQUEST: | |
149 if (source->GetUploadSize()) { | |
150 *status_text = l10n_util::GetStringFUTF16Int( | |
151 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS, | |
152 static_cast<int>((100 * source->GetUploadPosition()) / | |
153 source->GetUploadSize())); | |
154 return true; | |
155 } else { | |
156 *status_text = | |
157 l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST); | |
158 return true; | |
159 } | |
160 case net::LOAD_STATE_WAITING_FOR_RESPONSE: | |
161 *status_text = | |
162 l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE, | |
163 source->GetLoadStateHost()); | |
164 return true; | |
165 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE | |
166 case net::LOAD_STATE_IDLE: | |
167 case net::LOAD_STATE_READING_RESPONSE: | |
168 break; | |
169 } | |
170 | |
171 return guest_manager->ForEachGuest( | |
172 source, base::Bind(&CoreTabHelper::GetStatusTextForWebContents, | |
173 status_text)); | |
174 } | |
175 | |
138 //////////////////////////////////////////////////////////////////////////////// | 176 //////////////////////////////////////////////////////////////////////////////// |
139 // WebContentsObserver overrides | 177 // WebContentsObserver overrides |
140 | 178 |
141 void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) { | 179 void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) { |
142 UpdateContentRestrictions(0); | 180 UpdateContentRestrictions(0); |
143 } | 181 } |
144 | 182 |
145 void CoreTabHelper::WasShown() { | 183 void CoreTabHelper::WasShown() { |
146 web_cache::WebCacheManager::GetInstance()->ObserveActivity( | 184 web_cache::WebCacheManager::GetInstance()->ObserveActivity( |
147 web_contents()->GetRenderProcessHost()->GetID()); | 185 web_contents()->GetRenderProcessHost()->GetID()); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 DCHECK(!content_type.empty()); | 278 DCHECK(!content_type.empty()); |
241 open_url_params.uses_post = true; | 279 open_url_params.uses_post = true; |
242 open_url_params.browser_initiated_post_data = | 280 open_url_params.browser_initiated_post_data = |
243 base::RefCountedString::TakeString(post_data); | 281 base::RefCountedString::TakeString(post_data); |
244 open_url_params.extra_headers += base::StringPrintf( | 282 open_url_params.extra_headers += base::StringPrintf( |
245 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 283 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
246 content_type.c_str()); | 284 content_type.c_str()); |
247 } | 285 } |
248 web_contents()->OpenURL(open_url_params); | 286 web_contents()->OpenURL(open_url_params); |
249 } | 287 } |
OLD | NEW |