distiller_page_ios.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2014 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. #ifndef COMPONENTS_DOM_DISTILLER_IOS_DISTILLER_PAGE_IOS_H_
  5. #define COMPONENTS_DOM_DISTILLER_IOS_DISTILLER_PAGE_IOS_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/weak_ptr.h"
  9. #include "components/dom_distiller/core/distiller_page.h"
  10. #include "ios/web/public/web_state_observer.h"
  11. #include "url/gurl.h"
  12. namespace web {
  13. class BrowserState;
  14. class WebState;
  15. }
  16. namespace dom_distiller {
  17. class DistillerPageMediaBlocker;
  18. // Loads URLs and injects JavaScript into a page, extracting the distilled page
  19. // content.
  20. class DistillerPageIOS : public DistillerPage, public web::WebStateObserver {
  21. public:
  22. explicit DistillerPageIOS(web::BrowserState* browser_state);
  23. DistillerPageIOS(const DistillerPageIOS&) = delete;
  24. DistillerPageIOS& operator=(const DistillerPageIOS&) = delete;
  25. ~DistillerPageIOS() override;
  26. protected:
  27. bool StringifyOutput() override;
  28. void DistillPageImpl(const GURL& url, const std::string& script) override;
  29. // Sets the WebState that will be used for the distillation. Do not call
  30. // between |DistillPageImpl| and |OnDistillationDone|.
  31. virtual void AttachWebState(std::unique_ptr<web::WebState> web_state);
  32. // Release the WebState used for distillation. Do not call between
  33. // |DistillPageImpl| and |OnDistillationDone|.
  34. virtual std::unique_ptr<web::WebState> DetachWebState();
  35. // Return the current WebState.
  36. virtual web::WebState* CurrentWebState();
  37. // Called by |web_state_observer_| once the page has finished loading.
  38. virtual void OnLoadURLDone(
  39. web::PageLoadCompletionStatus load_completion_status);
  40. private:
  41. // Called once the |script_| has been evaluated on the page.
  42. void HandleJavaScriptResult(const base::Value* result);
  43. // web::WebStateObserver implementation.
  44. void PageLoaded(
  45. web::WebState* web_state,
  46. web::PageLoadCompletionStatus load_completion_status) override;
  47. void DidStartLoading(web::WebState* web_state) override;
  48. void DidStopLoading(web::WebState* web_state) override;
  49. void WebStateDestroyed(web::WebState* web_state) override;
  50. GURL url_;
  51. std::string script_;
  52. web::BrowserState* browser_state_;
  53. std::unique_ptr<web::WebState> web_state_;
  54. std::unique_ptr<DistillerPageMediaBlocker> media_blocker_;
  55. // Used to store whether the owned WebState is currently loading or not.
  56. // TODO(crbug.com/782159): this is a work-around as WebState::IsLoading()
  57. // is/was not returning the expected value when an SLL interstitial is
  58. // blocked. Remove this and use WebState::IsLoading() when WebState has
  59. // been fixed.
  60. bool loading_ = false;
  61. base::WeakPtrFactory<DistillerPageIOS> weak_ptr_factory_;
  62. };
  63. } // namespace dom_distiller
  64. #endif // COMPONENTS_DOM_DISTILLER_IOS_DISTILLER_PAGE_IOS_H_