1 // Copyright 2018 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 
5 #ifndef CHROME_BROWSER_UI_ASH_KEYBOARD_CHROME_KEYBOARD_WEB_CONTENTS_H_
6 #define CHROME_BROWSER_UI_ASH_KEYBOARD_CHROME_KEYBOARD_WEB_CONTENTS_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "ui/aura/window_observer.h"
15 #include "ui/gfx/geometry/size.h"
16 
17 class GURL;
18 
19 namespace content {
20 class WebContents;
21 }
22 
23 class ChromeKeyboardBoundsObserver;
24 
25 // WebContents manager for the virtual keyboard. This observes the web
26 // contents, manages the content::HostZoomMap, and informs the virtual
27 // keyboard controller when the contents have loaded. It also provides a
28 // WebContentsDelegate implementation.
29 class ChromeKeyboardWebContents : public content::WebContentsObserver,
30                                   public aura::WindowObserver {
31  public:
32   using LoadCallback = base::OnceCallback<void()>;
33   using UnembedCallback = base::RepeatingClosure;
34 
35   // Immediately starts loading |url| in a WebContents. |load_callback| is
36   // called when the WebContents finishes loading. |unembed_callback| is only
37   // used when the content is embedded using Window Service and is called when
38   // it gets unembedded (e.g. the hosting window is closed). Note that
39   // |unembed_callback| might end up deleting this.
40   ChromeKeyboardWebContents(content::BrowserContext* context,
41                             const GURL& url,
42                             LoadCallback load_callback,
43                             UnembedCallback unembed_callback);
44   ~ChromeKeyboardWebContents() override;
45 
46   // Updates the keyboard URL if |url| does not match the existing url.
47   void SetKeyboardUrl(const GURL& url);
48 
49   // Called via ash.mojo.KeyboardControllerObserver to provide an initial
50   // size for the keyboard contents, necessary to trigger SetContentsBounds
51   // in the delegate.
52   void SetInitialContentsSize(const gfx::Size& size);
53 
54   // Provide access to the native view (aura::Window) and frame
55   // (RenderWidgetHostView) through WebContents. TODO(stevenjb): Remove this
56   // once host window ownership is moved to ash.
web_contents()57   content::WebContents* web_contents() { return web_contents_.get(); }
58 
59  private:
60   // content::WebContentsObserver overrides
61   void RenderViewCreated(content::RenderViewHost* render_view_host) override;
62   void DidStopLoading() override;
63 
64   // Loads the web contents for the given |url|.
65   void LoadContents(const GURL& url);
66 
67   // aura::WindowObserver
68   void OnWindowBoundsChanged(aura::Window* window,
69                              const gfx::Rect& old_bounds,
70                              const gfx::Rect& new_bounds,
71                              ui::PropertyChangeReason reason) override;
72 
73   std::unique_ptr<content::WebContents> web_contents_;
74   std::unique_ptr<ChromeKeyboardBoundsObserver> window_bounds_observer_;
75 
76   // Called from DidStopLoading().
77   LoadCallback load_callback_;
78 
79   // Called when content is unembedded from Window Service.
80   UnembedCallback unembed_callback_;
81 
82   gfx::Size contents_size_;
83 
84   base::WeakPtrFactory<ChromeKeyboardWebContents> weak_ptr_factory_{this};
85 
86   DISALLOW_COPY_AND_ASSIGN(ChromeKeyboardWebContents);
87 };
88 
89 #endif  // CHROME_BROWSER_UI_ASH_KEYBOARD_CHROME_KEYBOARD_WEB_CONTENTS_H_
90