// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/guest_view/guest_view_internal_api.h"

#include "chrome/browser/guest_view/guest_view_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/guest_view_internal.h"
#include "extensions/common/permissions/permissions_data.h"

namespace extensions {

GuestViewInternalAllocateInstanceIdFunction::
    GuestViewInternalAllocateInstanceIdFunction() {
}

bool GuestViewInternalAllocateInstanceIdFunction::RunAsync() {
  EXTENSION_FUNCTION_VALIDATE(!args_->GetSize());

  // Check if we have "webview" permission.
  CHECK(GetExtension()->permissions_data()->HasAPIPermission(
            APIPermission::kWebView));

  int instanceId = GuestViewManager::FromBrowserContext(browser_context())
                       ->GetNextInstanceID();
  SetResult(base::Value::CreateIntegerValue(instanceId));
  SendResponse(true);
  return true;
}

}  // namespace extensions

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/guest_view/guest_view_internal_api.h"

#include "chrome/browser/guest_view/guest_view_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/guest_view_internal.h"
#include "extensions/common/permissions/permissions_data.h"

namespace {
const char* kWebViewPermissionRequiredError =
    "\"webview\" permission is required for allocating instance ID.";
}  // namespace

namespace extensions {

GuestViewInternalAllocateInstanceIdFunction::
    GuestViewInternalAllocateInstanceIdFunction() {
}

bool GuestViewInternalAllocateInstanceIdFunction::RunAsync() {
  EXTENSION_FUNCTION_VALIDATE(!args_->GetSize());


  if (!GetExtension()->permissions_data()->HasAPIPermission(
          APIPermission::kWebView)) {
    LOG(ERROR) << kWebViewPermissionRequiredError;
    error_ = kWebViewPermissionRequiredError;
    SendResponse(false);
    return false;
  }

  int instanceId = GuestViewManager::FromBrowserContext(browser_context())
                       ->GetNextInstanceID();
  SetResult(base::Value::CreateIntegerValue(instanceId));
  SendResponse(true);
  return true;
}

}  // namespace extensions

Remove a hard CHECK when allocating instance ID would fail.
Remove a hard CHECK when allocating instance ID would fail.

A CHECK here seems too intrusive to me, I've changed it
to send error response instead.
A compromised renderer should not take down the whole browser process.

BUG=364141
Test=None, internal clean up.

Review URL: https://codereview.chromium.org/345983002

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@278809 0039d316-1c4b-4281-b951-d872f2087c98
