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 = |
| 90 extensions::GuestViewManager::FromBrowserContextIfAvailable( |
| 91 source->GetBrowserContext()); |
| 92 if (!source->IsLoading() || |
| 93 source->GetLoadState().state == net::LOAD_STATE_IDLE) { |
| 94 if (!guest_manager) |
| 95 return false; |
| 96 return guest_manager->ForEachGuest( |
| 97 source, base::Bind(&CoreTabHelper::GetStatusTextForWebContents, |
| 98 status_text)); |
| 99 } |
| 100 |
| 101 switch (source->GetLoadState().state) { |
| 102 case net::LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL: |
| 103 case net::LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET: |
| 104 *status_text = |
| 105 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_SOCKET_SLOT); |
| 106 return true; |
| 107 case net::LOAD_STATE_WAITING_FOR_DELEGATE: |
| 108 if (!source->GetLoadState().param.empty()) { |
| 109 *status_text = l10n_util::GetStringFUTF16( |
| 110 IDS_LOAD_STATE_WAITING_FOR_DELEGATE, |
| 111 source->GetLoadState().param); |
| 112 return true; |
| 113 } else { |
| 114 *status_text = l10n_util::GetStringUTF16( |
| 115 IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC); |
| 116 return true; |
| 117 } |
| 118 case net::LOAD_STATE_WAITING_FOR_CACHE: |
| 119 *status_text = |
| 120 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE); |
| 121 return true; |
| 122 case net::LOAD_STATE_WAITING_FOR_APPCACHE: |
| 123 *status_text = |
| 124 l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE); |
| 125 return true; |
| 126 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL: |
| 127 *status_text = |
| 128 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
| 129 return true; |
| 130 case net::LOAD_STATE_DOWNLOADING_PROXY_SCRIPT: |
| 131 *status_text = |
| 132 l10n_util::GetStringUTF16(IDS_LOAD_STATE_DOWNLOADING_PROXY_SCRIPT); |
| 133 return true; |
| 134 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL: |
| 135 *status_text = |
| 136 l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL); |
| 137 return true; |
| 138 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT: |
| 139 *status_text = l10n_util::GetStringUTF16( |
| 140 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); |
| 141 return true; |
| 142 case net::LOAD_STATE_RESOLVING_HOST: |
| 143 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST); |
| 144 return true; |
| 145 case net::LOAD_STATE_CONNECTING: |
| 146 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING); |
| 147 return true; |
| 148 case net::LOAD_STATE_SSL_HANDSHAKE: |
| 149 *status_text = l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE); |
| 150 return true; |
| 151 case net::LOAD_STATE_SENDING_REQUEST: |
| 152 if (source->GetUploadSize()) { |
| 153 *status_text = l10n_util::GetStringFUTF16Int( |
| 154 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS, |
| 155 static_cast<int>((100 * source->GetUploadPosition()) / |
| 156 source->GetUploadSize())); |
| 157 return true; |
| 158 } else { |
| 159 *status_text = |
| 160 l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST); |
| 161 return true; |
| 162 } |
| 163 case net::LOAD_STATE_WAITING_FOR_RESPONSE: |
| 164 *status_text = |
| 165 l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE, |
| 166 source->GetLoadStateHost()); |
| 167 return true; |
| 168 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE |
| 169 case net::LOAD_STATE_IDLE: |
| 170 case net::LOAD_STATE_READING_RESPONSE: |
| 171 break; |
| 172 } |
| 173 if (!guest_manager) |
| 174 return false; |
| 175 return guest_manager->ForEachGuest( |
| 176 source, base::Bind(&CoreTabHelper::GetStatusTextForWebContents, |
| 177 status_text)); |
| 178 } |
| 179 |
138 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
139 // WebContentsObserver overrides | 181 // WebContentsObserver overrides |
140 | 182 |
141 void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) { | 183 void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) { |
142 UpdateContentRestrictions(0); | 184 UpdateContentRestrictions(0); |
143 } | 185 } |
144 | 186 |
145 void CoreTabHelper::WasShown() { | 187 void CoreTabHelper::WasShown() { |
146 web_cache::WebCacheManager::GetInstance()->ObserveActivity( | 188 web_cache::WebCacheManager::GetInstance()->ObserveActivity( |
147 web_contents()->GetRenderProcessHost()->GetID()); | 189 web_contents()->GetRenderProcessHost()->GetID()); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 DCHECK(!content_type.empty()); | 282 DCHECK(!content_type.empty()); |
241 open_url_params.uses_post = true; | 283 open_url_params.uses_post = true; |
242 open_url_params.browser_initiated_post_data = | 284 open_url_params.browser_initiated_post_data = |
243 base::RefCountedString::TakeString(post_data); | 285 base::RefCountedString::TakeString(post_data); |
244 open_url_params.extra_headers += base::StringPrintf( | 286 open_url_params.extra_headers += base::StringPrintf( |
245 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 287 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
246 content_type.c_str()); | 288 content_type.c_str()); |
247 } | 289 } |
248 web_contents()->OpenURL(open_url_params); | 290 web_contents()->OpenURL(open_url_params); |
249 } | 291 } |
OLD | NEW |