Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 2013 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_RENDERER_HOST_PEPPER_PEPPER_EXTENSIONS_COMMON_MESSAGE_FILTER_H_
      6 #define CHROME_BROWSER_RENDERER_HOST_PEPPER_PEPPER_EXTENSIONS_COMMON_MESSAGE_FILTER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "chrome/browser/extensions/extension_function.h"
     16 #include "ppapi/c/pp_instance.h"
     17 #include "ppapi/host/resource_message_filter.h"
     18 #include "url/gurl.h"
     19 
     20 struct ExtensionHostMsg_Request_Params;
     21 
     22 namespace base {
     23 class ListValue;
     24 }
     25 
     26 namespace content {
     27 class BrowserPpapiHost;
     28 }
     29 
     30 namespace ppapi {
     31 namespace host {
     32 struct HostMessageContext;
     33 }
     34 }
     35 
     36 namespace chrome {
     37 
     38 class PepperExtensionsCommonMessageFilter
     39     : public ppapi::host::ResourceMessageFilter {
     40  public:
     41   static PepperExtensionsCommonMessageFilter* Create(
     42       content::BrowserPpapiHost* host,
     43       PP_Instance instance);
     44 
     45  protected:
     46   PepperExtensionsCommonMessageFilter(int render_process_id,
     47                                       int render_view_id,
     48                                       const base::FilePath& profile_directory,
     49                                       const GURL& document_url);
     50   virtual ~PepperExtensionsCommonMessageFilter();
     51 
     52   // ppapi::host::ResourceMessageFilter overrides.
     53   virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
     54       const IPC::Message& msg) OVERRIDE;
     55   virtual int32_t OnResourceMessageReceived(
     56       const IPC::Message& msg,
     57       ppapi::host::HostMessageContext* context) OVERRIDE;
     58 
     59  private:
     60   // DispatcherOwner holds an ExtensionFunctionDispatcher instance and acts as
     61   // its delegate. It is designed to meet the lifespan requirements of
     62   // ExtensionFunctionDispatcher and its delegate. (Please see the comment of
     63   // ExtensionFunctionDispatcher constructor in
     64   // extension_function_dispatcher.h.)
     65   class DispatcherOwner;
     66 
     67   int32_t OnPost(ppapi::host::HostMessageContext* context,
     68                  const std::string& request_name,
     69                  base::ListValue& args);
     70 
     71   int32_t OnCall(ppapi::host::HostMessageContext* context,
     72                  const std::string& request_name,
     73                  base::ListValue& args);
     74 
     75   // It is possible that |dispatcher_owner_| is still NULL after this method is
     76   // called.
     77   void EnsureDispatcherOwnerInitialized();
     78   // Resets |dispatcher_owner_| to NULL.
     79   void DetachDispatcherOwner();
     80 
     81   void PopulateParams(const std::string& request_name,
     82                       base::ListValue* args,
     83                       bool has_callback,
     84                       ExtensionHostMsg_Request_Params* params);
     85 
     86   void OnExtensionFunctionCompleted(
     87       scoped_ptr<ppapi::host::ReplyMessageContext> reply_context,
     88       ExtensionFunction::ResponseType type,
     89       const base::ListValue& results,
     90       const std::string& error);
     91 
     92   bool HandleRequest(ppapi::host::HostMessageContext* context,
     93                      const std::string& request_name,
     94                      base::ListValue* args,
     95                      bool has_callback);
     96 
     97   // All the members are initialized on the IO thread when the object is
     98   // constructed, and accessed only on the UI thread afterwards.
     99   int render_process_id_;
    100   int render_view_id_;
    101   base::FilePath profile_directory_;
    102   GURL document_url_;
    103 
    104   // Not-owning pointer. It will be set to NULL when it goes away.
    105   DispatcherOwner* dispatcher_owner_;
    106   bool dispatcher_owner_initialized_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(PepperExtensionsCommonMessageFilter);
    109 };
    110 
    111 }  // namespace chrome
    112 
    113 #endif  // CHROME_BROWSER_RENDERER_HOST_PEPPER_PEPPER_EXTENSIONS_COMMON_MESSAGE_FILTER_H_
    114