Home | History | Annotate | Download | only in webui
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_WEBUI_WEB_UI_IMPL_H_
      6 #define CONTENT_BROWSER_WEBUI_WEB_UI_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_vector.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "content/public/browser/web_ui.h"
     15 #include "ipc/ipc_listener.h"
     16 
     17 namespace content {
     18 class RenderFrameHost;
     19 class RenderViewHost;
     20 
     21 class CONTENT_EXPORT WebUIImpl : public WebUI,
     22                                  public IPC::Listener,
     23                                  public base::SupportsWeakPtr<WebUIImpl> {
     24  public:
     25   explicit WebUIImpl(WebContents* contents);
     26   virtual ~WebUIImpl();
     27 
     28   // Called by WebContentsImpl when the RenderView is first created. This is
     29   // *not* called for every page load because in some cases
     30   // RenderFrameHostManager will reuse RenderView instances.
     31   void RenderViewCreated(RenderViewHost* render_view_host);
     32 
     33   // WebUI implementation:
     34   virtual WebContents* GetWebContents() const OVERRIDE;
     35   virtual WebUIController* GetController() const OVERRIDE;
     36   virtual void SetController(WebUIController* controller) OVERRIDE;
     37   virtual float GetDeviceScaleFactor() const OVERRIDE;
     38   virtual const base::string16& GetOverriddenTitle() const OVERRIDE;
     39   virtual void OverrideTitle(const base::string16& title) OVERRIDE;
     40   virtual PageTransition GetLinkTransitionType() const OVERRIDE;
     41   virtual void SetLinkTransitionType(PageTransition type) OVERRIDE;
     42   virtual int GetBindings() const OVERRIDE;
     43   virtual void SetBindings(int bindings) OVERRIDE;
     44   virtual void OverrideJavaScriptFrame(const std::string& frame_name) OVERRIDE;
     45   virtual void AddMessageHandler(WebUIMessageHandler* handler) OVERRIDE;
     46   typedef base::Callback<void(const base::ListValue*)> MessageCallback;
     47   virtual void RegisterMessageCallback(
     48       const std::string& message,
     49       const MessageCallback& callback) OVERRIDE;
     50   virtual void ProcessWebUIMessage(const GURL& source_url,
     51                                    const std::string& message,
     52                                    const base::ListValue& args) OVERRIDE;
     53   virtual void CallJavascriptFunction(
     54       const std::string& function_name) OVERRIDE;
     55   virtual void CallJavascriptFunction(const std::string& function_name,
     56                                       const base::Value& arg) OVERRIDE;
     57   virtual void CallJavascriptFunction(const std::string& function_name,
     58                                       const base::Value& arg1,
     59                                       const base::Value& arg2) OVERRIDE;
     60   virtual void CallJavascriptFunction(const std::string& function_name,
     61                                       const base::Value& arg1,
     62                                       const base::Value& arg2,
     63                                       const base::Value& arg3) OVERRIDE;
     64   virtual void CallJavascriptFunction(const std::string& function_name,
     65                                       const base::Value& arg1,
     66                                       const base::Value& arg2,
     67                                       const base::Value& arg3,
     68                                       const base::Value& arg4) OVERRIDE;
     69   virtual void CallJavascriptFunction(
     70       const std::string& function_name,
     71       const std::vector<const base::Value*>& args) OVERRIDE;
     72 
     73   // IPC::Listener implementation:
     74   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     75 
     76  private:
     77   // IPC message handling.
     78   void OnWebUISend(const GURL& source_url,
     79                    const std::string& message,
     80                    const base::ListValue& args);
     81 
     82   // Execute a string of raw JavaScript on the page.
     83   void ExecuteJavascript(const base::string16& javascript);
     84 
     85   // Finds the frame in which to execute JavaScript (the one specified by
     86   // OverrideJavaScriptFrame). May return NULL if no frame of the specified name
     87   // exists in the page.
     88   RenderFrameHost* TargetFrame();
     89 
     90   // A helper function for TargetFrame; adds a frame to the specified set if its
     91   // name matches frame_name_.
     92   void AddToSetIfFrameNameMatches(std::set<RenderFrameHost*>* frame_set,
     93                                   RenderFrameHost* host);
     94 
     95   // A map of message name -> message handling callback.
     96   typedef std::map<std::string, MessageCallback> MessageCallbackMap;
     97   MessageCallbackMap message_callbacks_;
     98 
     99   // Options that may be overridden by individual Web UI implementations. The
    100   // bool options default to false. See the public getters for more information.
    101   base::string16 overridden_title_;  // Defaults to empty string.
    102   PageTransition link_transition_type_;  // Defaults to LINK.
    103   int bindings_;  // The bindings from BindingsPolicy that should be enabled for
    104                   // this page.
    105 
    106   // The WebUIMessageHandlers we own.
    107   ScopedVector<WebUIMessageHandler> handlers_;
    108 
    109   // Non-owning pointer to the WebContents this WebUI is associated with.
    110   WebContents* web_contents_;
    111 
    112   // The name of the iframe this WebUI is embedded in (empty if not explicitly
    113   // overridden with OverrideJavaScriptFrame).
    114   std::string frame_name_;
    115 
    116   scoped_ptr<WebUIController> controller_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(WebUIImpl);
    119 };
    120 
    121 }  // namespace content
    122 
    123 #endif  // CONTENT_BROWSER_WEBUI_WEB_UI_IMPL_H_
    124