summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/web_state_impl.mm
blob: 9cd3386c3ec7b6337536194d3ee4e719efef4544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/web/web_state/web_state_impl.h"

#include <stddef.h>
#include <stdint.h>

#include "base/strings/sys_string_conversions.h"
#include "ios/web/interstitials/web_interstitial_impl.h"
#import "ios/web/navigation/crw_session_controller.h"
#import "ios/web/navigation/crw_session_entry.h"
#include "ios/web/navigation/navigation_item_impl.h"
#include "ios/web/net/request_group_util.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/navigation_item.h"
#include "ios/web/public/url_util.h"
#include "ios/web/public/web_client.h"
#include "ios/web/public/web_state/credential.h"
#include "ios/web/public/web_state/ui/crw_content_view.h"
#include "ios/web/public/web_state/web_state_observer.h"
#include "ios/web/public/web_state/web_state_policy_decider.h"
#include "ios/web/web_state/global_web_state_event_tracker.h"
#import "ios/web/web_state/ui/crw_web_controller.h"
#import "ios/web/web_state/ui/crw_web_controller_container_view.h"
#include "ios/web/web_state/web_state_facade_delegate.h"
#import "ios/web/webui/web_ui_ios_controller_factory_registry.h"
#import "ios/web/webui/web_ui_ios_impl.h"
#include "net/http/http_response_headers.h"

namespace web {

WebStateImpl::WebStateImpl(BrowserState* browser_state)
    : is_loading_(false),
      is_being_destroyed_(false),
      facade_delegate_(nullptr),
      web_controller_(nil),
      navigation_manager_(this, browser_state),
      interstitial_(nullptr),
      cache_mode_(net::RequestTracker::CACHE_NORMAL),
      weak_factory_(this) {
  GlobalWebStateEventTracker::GetInstance()->OnWebStateCreated(this);
}

WebStateImpl::~WebStateImpl() {
  is_being_destroyed_ = true;

  // WebUI depends on web state so it must be destroyed first in case any WebUI
  // implementations depends on accessing web state during destruction.
  ClearWebUI();

  // The facade layer (owned by the delegate) should be destroyed before the web
  // layer.
  DCHECK(!facade_delegate_);

  FOR_EACH_OBSERVER(WebStateObserver, observers_, WebStateDestroyed());
  FOR_EACH_OBSERVER(WebStateObserver, observers_, ResetWebState());
  FOR_EACH_OBSERVER(WebStatePolicyDecider, policy_deciders_,
                    WebStateDestroyed());
  FOR_EACH_OBSERVER(WebStatePolicyDecider, policy_deciders_, ResetWebState());
  DCHECK(script_command_callbacks_.empty());
  if (request_tracker_.get())
    CloseRequestTracker();
}

void WebStateImpl::AddObserver(WebStateObserver* observer) {
  DCHECK(!observers_.HasObserver(observer));
  observers_.AddObserver(observer);
}

void WebStateImpl::RemoveObserver(WebStateObserver* observer) {
  DCHECK(observers_.HasObserver(observer));
  observers_.RemoveObserver(observer);
}

void WebStateImpl::AddPolicyDecider(WebStatePolicyDecider* decider) {
  // Despite the name, ObserverList is actually generic, so it is used for
  // deciders. This makes the call here odd looking, but it's really just
  // managing the list, not setting observers on deciders.
  DCHECK(!policy_deciders_.HasObserver(decider));
  policy_deciders_.AddObserver(decider);
}

void WebStateImpl::RemovePolicyDecider(WebStatePolicyDecider* decider) {
  // Despite the name, ObserverList is actually generic, so it is used for
  // deciders. This makes the call here odd looking, but it's really just
  // managing the list, not setting observers on deciders.
  DCHECK(policy_deciders_.HasObserver(decider));
  policy_deciders_.RemoveObserver(decider);
}

bool WebStateImpl::Configured() const {
  return web_controller_ != nil;
}

void WebStateImpl::SetWebController(CRWWebController* web_controller) {
  DCHECK(!web_controller_);
  web_controller_ = web_controller;
}

WebStateFacadeDelegate* WebStateImpl::GetFacadeDelegate() const {
  return facade_delegate_;
}

void WebStateImpl::SetFacadeDelegate(WebStateFacadeDelegate* facade_delegate) {
  facade_delegate_ = facade_delegate;
}

WebStateImpl* WebStateImpl::CopyForSessionWindow() {
  WebStateImpl* copy = new WebStateImpl(GetBrowserState());
  copy->GetNavigationManagerImpl().CopyState(&navigation_manager_);
  return copy;
}

void WebStateImpl::OnNavigationCommitted(const GURL& url) {
  UpdateHttpResponseHeaders(url);
}

void WebStateImpl::OnUrlHashChanged() {
  FOR_EACH_OBSERVER(WebStateObserver, observers_, UrlHashChanged());
}

void WebStateImpl::OnHistoryStateChanged() {
  FOR_EACH_OBSERVER(WebStateObserver, observers_, HistoryStateChanged());
}

bool WebStateImpl::OnScriptCommandReceived(const std::string& command,
                                           const base::DictionaryValue& value,
                                           const GURL& url,
                                           bool user_is_interacting) {
  size_t dot_position = command.find_first_of('.');
  if (dot_position == 0 || dot_position == std::string::npos)
    return false;

  std::string prefix = command.substr(0, dot_position);
  auto it = script_command_callbacks_.find(prefix);
  if (it == script_command_callbacks_.end())
    return false;

  return it->second.Run(value, url, user_is_interacting);
}

void WebStateImpl::SetIsLoading(bool is_loading) {
  if (is_loading == is_loading_)
    return;

  is_loading_ = is_loading;
  if (facade_delegate_)
    facade_delegate_->OnLoadingStateChanged();

  if (is_loading) {
    FOR_EACH_OBSERVER(WebStateObserver, observers_, DidStartLoading());
  } else {
    FOR_EACH_OBSERVER(WebStateObserver, observers_, DidStopLoading());
  }
}

bool WebStateImpl::IsLoading() const {
  return is_loading_;
}

bool WebStateImpl::IsBeingDestroyed() const {
  return is_being_destroyed_;
}

void WebStateImpl::OnPageLoaded(const GURL& url, bool load_success) {
  if (facade_delegate_)
    facade_delegate_->OnPageLoaded();

  PageLoadCompletionStatus load_completion_status =
      load_success ? PageLoadCompletionStatus::SUCCESS
                   : PageLoadCompletionStatus::FAILURE;
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    PageLoaded(load_completion_status));
}

void WebStateImpl::OnFormActivityRegistered(const std::string& form_name,
                                            const std::string& field_name,
                                            const std::string& type,
                                            const std::string& value,
                                            int key_code,
                                            bool input_missing) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    FormActivityRegistered(form_name, field_name, type, value,
                                           key_code, input_missing));
}

void WebStateImpl::OnFaviconUrlUpdated(
    const std::vector<FaviconURL>& candidates) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    FaviconUrlUpdated(candidates));
}

void WebStateImpl::OnCredentialsRequested(
    int request_id,
    const GURL& source_url,
    bool unmediated,
    const std::vector<std::string>& federations,
    bool user_interaction) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    CredentialsRequested(request_id, source_url, unmediated,
                                         federations, user_interaction));
}

void WebStateImpl::OnSignedIn(int request_id,
                              const GURL& source_url,
                              const web::Credential& credential) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    SignedIn(request_id, source_url, credential));
}

void WebStateImpl::OnSignedIn(int request_id, const GURL& source_url) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    SignedIn(request_id, source_url));
}

void WebStateImpl::OnSignedOut(int request_id, const GURL& source_url) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    SignedOut(request_id, source_url));
}

void WebStateImpl::OnSignInFailed(int request_id,
                                  const GURL& source_url,
                                  const web::Credential& credential) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    SignInFailed(request_id, source_url, credential));
}

void WebStateImpl::OnSignInFailed(int request_id, const GURL& source_url) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    SignInFailed(request_id, source_url));
}

void WebStateImpl::OnDocumentSubmitted(const std::string& form_name,
                                       bool user_initiated) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    DocumentSubmitted(form_name, user_initiated));
}

NavigationManagerImpl& WebStateImpl::GetNavigationManagerImpl() {
  return navigation_manager_;
}

const NavigationManagerImpl& WebStateImpl::GetNavigationManagerImpl() const {
  return navigation_manager_;
}

// There are currently two kinds of WebUI: those that have been adapted to
// web::WebUIIOS, and those that are still using content::WebUI. Try to create
// it as the first, and then fall back to the latter if necessary.
void WebStateImpl::CreateWebUI(const GURL& url) {
  web_ui_.reset(CreateWebUIIOS(url));
}

void WebStateImpl::ClearWebUI() {
  web_ui_.reset();
}

bool WebStateImpl::HasWebUI() {
  return !!web_ui_;
}

void WebStateImpl::ProcessWebUIMessage(const GURL& source_url,
                                       const std::string& message,
                                       const base::ListValue& args) {
  if (web_ui_)
    web_ui_->ProcessWebUIIOSMessage(source_url, message, args);
}

void WebStateImpl::LoadWebUIHtml(const base::string16& html, const GURL& url) {
  CHECK(web::GetWebClient()->IsAppSpecificURL(url));
  [web_controller_ loadHTML:base::SysUTF16ToNSString(html)
          forAppSpecificURL:url];
}

const base::string16& WebStateImpl::GetTitle() const {
  // TODO(stuartmorgan): Implement the NavigationManager logic necessary to
  // match the WebContents implementation of this method.
  DCHECK(Configured());
  web::NavigationItem* item = navigation_manager_.GetLastCommittedItem();
  if (item) {
    return item->GetTitleForDisplay(
        web::GetWebClient()->GetAcceptLangs(GetBrowserState()));
  }
  return empty_string16_;
}

void WebStateImpl::ShowTransientContentView(CRWContentView* content_view) {
  DCHECK(Configured());
  DCHECK(content_view);
  DCHECK(content_view.scrollView);
  [web_controller_ showTransientContentView:content_view];
}

bool WebStateImpl::IsShowingWebInterstitial() const {
  // Technically we could have |interstitial_| set but its view isn't
  // being displayed, but there's no code path where that could occur.
  return interstitial_ != nullptr;
}

WebInterstitial* WebStateImpl::GetWebInterstitial() const {
  return interstitial_;
}

net::HttpResponseHeaders* WebStateImpl::GetHttpResponseHeaders() const {
  return http_response_headers_.get();
}

void WebStateImpl::OnHttpResponseHeadersReceived(
    net::HttpResponseHeaders* response_headers,
    const GURL& resource_url) {
  // Store the headers in a map until the page finishes loading, as we do not
  // know which URL corresponds to the main page yet.
  // Remove the hash (if any) as it is sometimes altered by in-page navigations.
  // TODO(crbug/551677): Simplify all this logic once UIWebView is no longer
  // supported.
  const GURL& url = GURLByRemovingRefFromGURL(resource_url);
  response_headers_map_[url] = response_headers;
}

void WebStateImpl::UpdateHttpResponseHeaders(const GURL& url) {
  // Reset the state.
  http_response_headers_ = NULL;
  mime_type_.clear();
  content_language_header_.clear();

  // Discard all the response headers except the ones for |main_page_url|.
  auto it = response_headers_map_.find(GURLByRemovingRefFromGURL(url));
  if (it != response_headers_map_.end())
    http_response_headers_ = it->second;
  response_headers_map_.clear();

  if (!http_response_headers_.get())
    return;

  // MIME type.
  std::string mime_type;
  http_response_headers_->GetMimeType(&mime_type);
  mime_type_ = mime_type;

  // Content-Language
  std::string content_language;
  http_response_headers_->GetNormalizedHeader("content-language",
                                              &content_language);
  // Remove everything after the comma ',' if any.
  size_t comma_index = content_language.find_first_of(',');
  if (comma_index != std::string::npos)
    content_language.resize(comma_index);
  content_language_header_ = content_language;
}

void WebStateImpl::ShowWebInterstitial(WebInterstitialImpl* interstitial) {
  DCHECK(Configured());
  DCHECK(interstitial);
  interstitial_ = interstitial;
  ShowTransientContentView(interstitial_->GetContentView());
}

void WebStateImpl::ClearTransientContentView() {
  if (interstitial_) {
    // Store the currently displayed interstitial in a local variable and reset
    // |interstitial_| early.  This is to prevent an infinite loop, as
    // |DontProceed()| internally calls |ClearTransientContentView()|.
    web::WebInterstitial* interstitial = interstitial_;
    interstitial_ = nullptr;
    interstitial->DontProceed();
    // Don't access |interstitial| after calling |DontProceed()|, as it triggers
    // deletion.
    FOR_EACH_OBSERVER(WebStateObserver, observers_, InsterstitialDismissed());
  }
  [web_controller_ clearTransientContentView];
}

WebUIIOS* WebStateImpl::CreateWebUIIOS(const GURL& url) {
  WebUIIOSControllerFactory* factory =
      WebUIIOSControllerFactoryRegistry::GetInstance();
  if (!factory)
    return NULL;
  WebUIIOSImpl* web_ui = new WebUIIOSImpl(this);
  WebUIIOSController* controller =
      factory->CreateWebUIIOSControllerForURL(web_ui, url);
  if (controller) {
    web_ui->SetController(controller);
    return web_ui;
  }

  delete web_ui;
  return NULL;
}

void WebStateImpl::SetContentsMimeType(const std::string& mime_type) {
  mime_type_ = mime_type;
}

void WebStateImpl::ExecuteJavaScriptAsync(const base::string16& javascript) {
  DCHECK(Configured());
  [web_controller_ evaluateJavaScript:base::SysUTF16ToNSString(javascript)
                  stringResultHandler:nil];
}

bool WebStateImpl::ShouldAllowRequest(NSURLRequest* request) {
  base::ObserverListBase<WebStatePolicyDecider>::Iterator it(&policy_deciders_);
  WebStatePolicyDecider* policy_decider = nullptr;
  while ((policy_decider = it.GetNext()) != nullptr) {
    if (!policy_decider->ShouldAllowRequest(request))
      return false;
  }
  return true;
}

bool WebStateImpl::ShouldAllowResponse(NSURLResponse* response) {
  base::ObserverListBase<WebStatePolicyDecider>::Iterator it(&policy_deciders_);
  WebStatePolicyDecider* policy_decider = nullptr;
  while ((policy_decider = it.GetNext()) != nullptr) {
    if (!policy_decider->ShouldAllowResponse(response))
      return false;
  }
  return true;
}

#pragma mark - RequestTracker management

void WebStateImpl::InitializeRequestTracker(
    id<CRWRequestTrackerDelegate> delegate) {
  BrowserState* browser_state = navigation_manager_.GetBrowserState();
  request_tracker_ = RequestTrackerImpl::CreateTrackerForRequestGroupID(
      GetRequestGroupID(), browser_state, browser_state->GetRequestContext(),
      delegate);
}

void WebStateImpl::CloseRequestTracker() {
  request_tracker_->Close();
  request_tracker_ = NULL;
}

RequestTrackerImpl* WebStateImpl::GetRequestTracker() {
  DCHECK(request_tracker_.get());
  return request_tracker_.get();
}

net::RequestTracker::CacheMode WebStateImpl::GetCacheMode() {
  return cache_mode_;
}

void WebStateImpl::SetCacheMode(net::RequestTracker::CacheMode mode) {
  cache_mode_ = mode;
}

NSString* WebStateImpl::GetRequestGroupID() {
  if (request_group_id_.get() == nil)
    request_group_id_.reset([GenerateNewRequestGroupID() copy]);

  return request_group_id_;
}

int WebStateImpl::DownloadImage(
    const GURL& url,
    bool is_favicon,
    uint32_t max_bitmap_size,
    bool bypass_cache,
    const ImageDownloadCallback& callback) {
  // |is_favicon| specifies whether the download of the image occurs with
  // cookies or not. Currently, only downloads without cookies are supported.
  // |bypass_cache| is ignored since the downloads never go through a cache.
  DCHECK(is_favicon);
  return [[web_controller_ delegate] downloadImageAtUrl:url
                                          maxBitmapSize:max_bitmap_size
                                              callback:callback];
}

base::WeakPtr<WebState> WebStateImpl::AsWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

#pragma mark - WebState implementation

UIView* WebStateImpl::GetView() {
  return [web_controller_ view];
}

WebViewType WebStateImpl::GetWebViewType() const {
  return [web_controller_ webViewType];
}

BrowserState* WebStateImpl::GetBrowserState() const {
  return navigation_manager_.GetBrowserState();
}

void WebStateImpl::OpenURL(const WebState::OpenURLParams& params) {
  DCHECK(Configured());
  ClearTransientContentView();
  [[web_controller_ delegate] openURLWithParams:params];
}

NavigationManager* WebStateImpl::GetNavigationManager() {
  return &GetNavigationManagerImpl();
}

CRWJSInjectionReceiver* WebStateImpl::GetJSInjectionReceiver() const {
  return [web_controller_ jsInjectionReceiver];
}

const std::string& WebStateImpl::GetContentLanguageHeader() const {
  return content_language_header_;
}

const std::string& WebStateImpl::GetContentsMimeType() const {
  return mime_type_;
}

bool WebStateImpl::ContentIsHTML() const {
  return [web_controller_ contentIsHTML];
}

const GURL& WebStateImpl::GetVisibleURL() const {
  web::NavigationItem* item = navigation_manager_.GetVisibleItem();
  return item ? item->GetVirtualURL() : GURL::EmptyGURL();
}

const GURL& WebStateImpl::GetLastCommittedURL() const {
  web::NavigationItem* item = navigation_manager_.GetLastCommittedItem();
  return item ? item->GetVirtualURL() : GURL::EmptyGURL();
}

GURL WebStateImpl::GetCurrentURL(URLVerificationTrustLevel* trust_level) const {
  return [web_controller_ currentURLWithTrustLevel:trust_level];
}

void WebStateImpl::AddScriptCommandCallback(
    const ScriptCommandCallback& callback,
    const std::string& command_prefix) {
  DCHECK(!command_prefix.empty());
  DCHECK(command_prefix.find_first_of('.') == std::string::npos);
  DCHECK(script_command_callbacks_.find(command_prefix) ==
         script_command_callbacks_.end());
  script_command_callbacks_[command_prefix] = callback;
}

void WebStateImpl::RemoveScriptCommandCallback(
    const std::string& command_prefix) {
  DCHECK(script_command_callbacks_.find(command_prefix) !=
         script_command_callbacks_.end());
  script_command_callbacks_.erase(command_prefix);
}

id<CRWWebViewProxy> WebStateImpl::GetWebViewProxy() const {
  return [web_controller_ webViewProxy];
}

void WebStateImpl::OnProvisionalNavigationStarted(const GURL& url) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    ProvisionalNavigationStarted(url));
}

#pragma mark - NavigationManagerDelegate implementation

// Mirror WebContentsImpl::NavigateToPendingEntry() so that
// NavigationControllerIO::GoBack() actually goes back.
void WebStateImpl::NavigateToPendingEntry() {
  [web_controller_ loadCurrentURL];
}

void WebStateImpl::OnNavigationItemsPruned(size_t pruned_item_count) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    NavigationItemsPruned(pruned_item_count));
}

void WebStateImpl::OnNavigationItemChanged() {
  FOR_EACH_OBSERVER(WebStateObserver, observers_, NavigationItemChanged());
}

void WebStateImpl::OnNavigationItemCommitted(
    const LoadCommittedDetails& load_details) {
  FOR_EACH_OBSERVER(WebStateObserver, observers_,
                    NavigationItemCommitted(load_details));
}

WebState* WebStateImpl::GetWebState() {
  return this;
}

}  // namespace web