Home | History | Annotate | Download | only in renderer
      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 
      5 #include "extensions/renderer/lazy_background_page_native_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "content/public/renderer/render_view.h"
      9 #include "extensions/common/extension_messages.h"
     10 #include "extensions/common/manifest_handlers/background_info.h"
     11 #include "extensions/renderer/extension_helper.h"
     12 #include "extensions/renderer/script_context.h"
     13 
     14 namespace extensions {
     15 
     16 LazyBackgroundPageNativeHandler::LazyBackgroundPageNativeHandler(
     17     ScriptContext* context)
     18     : ObjectBackedNativeHandler(context) {
     19   RouteFunction(
     20       "IncrementKeepaliveCount",
     21       base::Bind(&LazyBackgroundPageNativeHandler::IncrementKeepaliveCount,
     22                  base::Unretained(this)));
     23   RouteFunction(
     24       "DecrementKeepaliveCount",
     25       base::Bind(&LazyBackgroundPageNativeHandler::DecrementKeepaliveCount,
     26                  base::Unretained(this)));
     27 }
     28 
     29 void LazyBackgroundPageNativeHandler::IncrementKeepaliveCount(
     30     const v8::FunctionCallbackInfo<v8::Value>& args) {
     31   if (!context())
     32     return;
     33   content::RenderView* render_view = context()->GetRenderView();
     34   if (IsContextLazyBackgroundPage(render_view, context()->extension())) {
     35     render_view->Send(new ExtensionHostMsg_IncrementLazyKeepaliveCount(
     36         render_view->GetRoutingID()));
     37   }
     38 }
     39 
     40 void LazyBackgroundPageNativeHandler::DecrementKeepaliveCount(
     41     const v8::FunctionCallbackInfo<v8::Value>& args) {
     42   if (!context())
     43     return;
     44   content::RenderView* render_view = context()->GetRenderView();
     45   if (IsContextLazyBackgroundPage(render_view, context()->extension())) {
     46     render_view->Send(new ExtensionHostMsg_DecrementLazyKeepaliveCount(
     47         render_view->GetRoutingID()));
     48   }
     49 }
     50 
     51 bool LazyBackgroundPageNativeHandler::IsContextLazyBackgroundPage(
     52     content::RenderView* render_view,
     53     const Extension* extension) {
     54   if (!render_view)
     55     return false;
     56 
     57   ExtensionHelper* helper = ExtensionHelper::Get(render_view);
     58   return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
     59           helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
     60 }
     61 
     62 }  // namespace extensions
     63