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 "chrome/browser/extensions/api/synced_notifications_private/synced_notifications_private_api.h"
6
7#include "chrome/browser/extensions/api/synced_notifications_private/synced_notifications_shim.h"
8#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
9#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
10#include "chrome/browser/profiles/profile.h"
11
12namespace extensions {
13namespace api {
14
15namespace {
16const char kSyncNotRunningError[] = "Sync Not Running";
17const char kDataConversionError[] = "Data Conversion Error";
18}  // namespace
19
20SyncedNotificationsPrivateGetInitialDataFunction::
21    SyncedNotificationsPrivateGetInitialDataFunction() {
22}
23
24SyncedNotificationsPrivateGetInitialDataFunction::
25    ~SyncedNotificationsPrivateGetInitialDataFunction() {
26}
27
28ExtensionFunction::ResponseAction
29SyncedNotificationsPrivateGetInitialDataFunction::Run() {
30  scoped_ptr<api::synced_notifications_private::GetInitialData::Params> params(
31      api::synced_notifications_private::GetInitialData::Params::Create(
32          *args_));
33  EXTENSION_FUNCTION_VALIDATE(params.get());
34
35  notifier::ChromeNotifierService* notifier_service =
36      notifier::ChromeNotifierServiceFactory::GetForProfile(
37          Profile::FromBrowserContext(browser_context()),
38          Profile::IMPLICIT_ACCESS);
39  if (!notifier_service ||
40      !notifier_service->GetSyncedNotificationsShim()->IsSyncReady()) {
41    return RespondNow(Error(kSyncNotRunningError));
42  }
43
44  std::vector<linked_ptr<synced_notifications_private::SyncData> >
45      sync_data_list;
46  if (!notifier_service->GetSyncedNotificationsShim()->GetInitialData(
47          params->type, &sync_data_list)) {
48    return RespondNow(Error(kSyncNotRunningError));
49  }
50  return RespondNow(ArgumentList(
51      synced_notifications_private::GetInitialData::Results::Create(
52          sync_data_list)));
53}
54
55SyncedNotificationsPrivateUpdateNotificationFunction::
56    SyncedNotificationsPrivateUpdateNotificationFunction() {
57}
58
59SyncedNotificationsPrivateUpdateNotificationFunction::
60    ~SyncedNotificationsPrivateUpdateNotificationFunction() {
61}
62
63ExtensionFunction::ResponseAction
64SyncedNotificationsPrivateUpdateNotificationFunction::Run() {
65  scoped_ptr<api::synced_notifications_private::UpdateNotification::Params>
66      params(
67          api::synced_notifications_private::UpdateNotification::Params::Create(
68              *args_));
69  EXTENSION_FUNCTION_VALIDATE(params.get());
70
71  notifier::ChromeNotifierService* notifier_service =
72      notifier::ChromeNotifierServiceFactory::GetForProfile(
73          Profile::FromBrowserContext(browser_context()),
74          Profile::IMPLICIT_ACCESS);
75  if (!notifier_service ||
76      !notifier_service->GetSyncedNotificationsShim()->IsSyncReady()) {
77    return RespondNow(Error(kSyncNotRunningError));
78  }
79
80  if (!notifier_service->GetSyncedNotificationsShim()->UpdateNotification(
81          params->changed_notification)) {
82    return RespondNow(Error(kDataConversionError));
83  }
84
85  return RespondNow(NoArguments());
86}
87
88SyncedNotificationsPrivateSetRenderContextFunction::
89    SyncedNotificationsPrivateSetRenderContextFunction() {
90}
91
92SyncedNotificationsPrivateSetRenderContextFunction::
93    ~SyncedNotificationsPrivateSetRenderContextFunction() {
94}
95
96ExtensionFunction::ResponseAction
97SyncedNotificationsPrivateSetRenderContextFunction::Run() {
98  scoped_ptr<api::synced_notifications_private::SetRenderContext::Params>
99      params(
100          api::synced_notifications_private::SetRenderContext::Params::Create(
101              *args_));
102  EXTENSION_FUNCTION_VALIDATE(params.get());
103
104  notifier::ChromeNotifierService* notifier_service =
105      notifier::ChromeNotifierServiceFactory::GetForProfile(
106          Profile::FromBrowserContext(browser_context()),
107          Profile::IMPLICIT_ACCESS);
108  if (!notifier_service ||
109      !notifier_service->GetSyncedNotificationsShim()->IsSyncReady()) {
110    return RespondNow(Error(kSyncNotRunningError));
111  }
112
113  if (!notifier_service->GetSyncedNotificationsShim()->SetRenderContext(
114          params->refresh,
115          params->data_type_context)) {
116    return RespondNow(Error(kDataConversionError));
117  }
118  return RespondNow(NoArguments());
119}
120
121}  // namespace api
122}  // namespace extensions
123