Home | History | Annotate | Download | only in cloud_print_private
      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 #include "chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.h"
      6 
      7 #include <string>
      8 
      9 #include "base/threading/sequenced_worker_pool.h"
     10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
     11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
     12 #include "chrome/common/extensions/api/cloud_print_private.h"
     13 #include "google_apis/google_api_keys.h"
     14 #include "net/base/net_util.h"
     15 
     16 namespace extensions {
     17 
     18 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance_ = NULL;
     19 
     20 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance() {
     21   return instance_;
     22 }
     23 
     24 CloudPrintTestsDelegate::CloudPrintTestsDelegate() {
     25   instance_ = this;
     26 }
     27 
     28 CloudPrintTestsDelegate::~CloudPrintTestsDelegate() {
     29   instance_ = NULL;
     30 }
     31 
     32 CloudPrintPrivateSetupConnectorFunction::
     33     CloudPrintPrivateSetupConnectorFunction() {
     34 }
     35 
     36 CloudPrintPrivateSetupConnectorFunction::
     37     ~CloudPrintPrivateSetupConnectorFunction() {
     38 }
     39 
     40 bool CloudPrintPrivateSetupConnectorFunction::RunAsync() {
     41 #if defined(ENABLE_FULL_PRINTING)
     42   using api::cloud_print_private::SetupConnector::Params;
     43   scoped_ptr<Params> params(Params::Create(*args_));
     44   if (CloudPrintTestsDelegate::instance()) {
     45     CloudPrintTestsDelegate::instance()->SetupConnector(
     46         params->user_email,
     47         params->robot_email,
     48         params->credentials,
     49         params->user_settings);
     50   } else {
     51     CloudPrintProxyService* service =
     52         CloudPrintProxyServiceFactory::GetForProfile(GetProfile());
     53     if (!service)
     54       return false;
     55     scoped_ptr<base::DictionaryValue> user_setings(
     56         params->user_settings.ToValue());
     57     service->EnableForUserWithRobot(params->credentials,
     58                                     params->robot_email,
     59                                     params->user_email,
     60                                     *user_setings);
     61   }
     62   SendResponse(true);
     63   return true;
     64 #else
     65   return false;
     66 #endif
     67 }
     68 
     69 CloudPrintPrivateGetHostNameFunction::CloudPrintPrivateGetHostNameFunction() {
     70 }
     71 
     72 CloudPrintPrivateGetHostNameFunction::~CloudPrintPrivateGetHostNameFunction() {
     73 }
     74 
     75 bool CloudPrintPrivateGetHostNameFunction::RunAsync() {
     76   SetResult(new base::StringValue(
     77       CloudPrintTestsDelegate::instance() ?
     78       CloudPrintTestsDelegate::instance()->GetHostName() :
     79       net::GetHostName()));
     80   SendResponse(true);
     81   return true;
     82 }
     83 
     84 CloudPrintPrivateGetPrintersFunction::CloudPrintPrivateGetPrintersFunction() {
     85 }
     86 
     87 CloudPrintPrivateGetPrintersFunction::~CloudPrintPrivateGetPrintersFunction() {
     88 }
     89 
     90 void CloudPrintPrivateGetPrintersFunction::SendResults(
     91     const std::vector<std::string>& printers) {
     92   results_ = api::cloud_print_private::GetPrinters::Results::Create(printers);
     93   SendResponse(true);
     94 }
     95 
     96 bool CloudPrintPrivateGetPrintersFunction::RunAsync() {
     97 #if defined(ENABLE_FULL_PRINTING)
     98   std::vector<std::string> result;
     99   if (CloudPrintTestsDelegate::instance()) {
    100     SendResults(CloudPrintTestsDelegate::instance()->GetPrinters());
    101   } else {
    102     CloudPrintProxyService* service =
    103         CloudPrintProxyServiceFactory::GetForProfile(GetProfile());
    104     if (!service)
    105       return false;
    106     service->GetPrinters(
    107         base::Bind(&CloudPrintPrivateGetPrintersFunction::SendResults, this));
    108   }
    109   return true;
    110 #else
    111   return false;
    112 #endif
    113 }
    114 
    115 
    116 CloudPrintPrivateGetClientIdFunction::CloudPrintPrivateGetClientIdFunction() {
    117 }
    118 
    119 CloudPrintPrivateGetClientIdFunction::~CloudPrintPrivateGetClientIdFunction() {
    120 }
    121 
    122 bool CloudPrintPrivateGetClientIdFunction::RunAsync() {
    123   SetResult(new base::StringValue(
    124       CloudPrintTestsDelegate::instance() ?
    125       CloudPrintTestsDelegate::instance()->GetClientId() :
    126       google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT)));
    127   SendResponse(true);
    128   return true;
    129 }
    130 
    131 }  // namespace extensions
    132