web_view_global_state_util.mm 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2017 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. #include "ios/web_view/internal/web_view_global_state_util.h"
  5. #import <UIKit/UIKit.h>
  6. #include <memory>
  7. #include "ios/web/public/init/web_main.h"
  8. #import "ios/web_view/internal/web_view_web_client.h"
  9. #import "ios/web_view/internal/web_view_web_main_delegate.h"
  10. #import "testing/coverage_util_ios.h"
  11. #if !defined(__has_feature) || !__has_feature(objc_arc)
  12. #error "This file requires ARC support."
  13. #endif
  14. namespace ios_web_view {
  15. void InitializeGlobalState() {
  16. static std::unique_ptr<ios_web_view::WebViewWebClient> web_client;
  17. static std::unique_ptr<ios_web_view::WebViewWebMainDelegate>
  18. web_main_delegate;
  19. static std::unique_ptr<web::WebMain> web_main;
  20. static dispatch_once_t once_token;
  21. dispatch_once(&once_token, ^{
  22. // This is for generating coverage data for tests only.
  23. coverage_util::ConfigureCoverageReportPath();
  24. web_client = std::make_unique<ios_web_view::WebViewWebClient>();
  25. web::SetWebClient(web_client.get());
  26. web_main_delegate =
  27. std::make_unique<ios_web_view::WebViewWebMainDelegate>();
  28. web::WebMainParams params(web_main_delegate.get());
  29. web_main = std::make_unique<web::WebMain>(std::move(params));
  30. [NSNotificationCenter.defaultCenter
  31. addObserverForName:UIApplicationWillTerminateNotification
  32. object:nil
  33. queue:nil
  34. usingBlock:^(NSNotification* _Nonnull note) {
  35. // These global variables should be destructed when the app is
  36. // about to terminate, and in reverse order to construction.
  37. web_main.reset();
  38. web_main_delegate.reset();
  39. web_client.reset();
  40. }];
  41. });
  42. }
  43. } // namespace ios_web_view