Home | History | Annotate | Download | only in browser
      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_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
      6 #define CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/gtest_prod_util.h"
     10 #include "base/strings/string16.h"
     11 #include "content/common/content_export.h"
     12 
     13 class GURL;
     14 class WebUIBrowserTest;
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 class ListValue;
     19 }
     20 
     21 namespace content {
     22 
     23 class WebUI;
     24 class WebUIImpl;
     25 
     26 // Messages sent from the DOM are forwarded via the WebUI to handler
     27 // classes. These objects are owned by WebUI and destroyed when the
     28 // host is destroyed.
     29 class CONTENT_EXPORT WebUIMessageHandler {
     30  public:
     31   WebUIMessageHandler() : web_ui_(NULL) {}
     32   virtual ~WebUIMessageHandler() {}
     33 
     34  protected:
     35   FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractIntegerValue);
     36   FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractDoubleValue);
     37   FRIEND_TEST_ALL_PREFIXES(WebUIMessageHandlerTest, ExtractStringValue);
     38 
     39   // Helper methods:
     40 
     41   // Extract an integer value from a list Value.
     42   static bool ExtractIntegerValue(const base::ListValue* value, int* out_int);
     43 
     44   // Extract a floating point (double) value from a list Value.
     45   static bool ExtractDoubleValue(const base::ListValue* value,
     46                                  double* out_value);
     47 
     48   // Extract a string value from a list Value.
     49   static base::string16 ExtractStringValue(const base::ListValue* value);
     50 
     51   // This is where subclasses specify which messages they'd like to handle and
     52   // perform any additional initialization.. At this point web_ui() will return
     53   // the associated WebUI object.
     54   virtual void RegisterMessages() = 0;
     55 
     56   // Returns the attached WebUI for this handler.
     57   WebUI* web_ui() const { return web_ui_; }
     58 
     59   // Sets the attached WebUI - exposed to subclasses for testing purposes.
     60   void set_web_ui(WebUI* web_ui) { web_ui_ = web_ui; }
     61 
     62  private:
     63   // Provide external classes access to web_ui() and set_web_ui().
     64   friend class WebUIImpl;
     65   friend class ::WebUIBrowserTest;
     66 
     67   WebUI* web_ui_;
     68 };
     69 
     70 }  // namespace content
     71 
     72 #endif  // CONTENT_PUBLIC_BROWSER_WEB_UI_MESSAGE_HANDLER_H_
     73