OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/lock_screen_data/lock_screen_data_api.h" | 5 #include "extensions/browser/api/lock_screen_data/lock_screen_data_api.h" |
6 | 6 |
7 #include <memory> | 7 #include <string> |
| 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
| 11 #include "extensions/browser/api/lock_screen_data/data_item.h" |
| 12 #include "extensions/browser/api/lock_screen_data/lock_screen_item_storage.h" |
| 13 #include "extensions/browser/api/lock_screen_data/operation_result.h" |
10 #include "extensions/common/api/lock_screen_data.h" | 14 #include "extensions/common/api/lock_screen_data.h" |
11 | 15 |
12 namespace extensions { | 16 namespace extensions { |
13 | 17 |
| 18 namespace { |
| 19 |
| 20 std::string GetErrorString(lock_screen_data::OperationResult result) { |
| 21 switch (result) { |
| 22 case lock_screen_data::OperationResult::kSuccess: |
| 23 NOTREACHED() << "Expected a failure code."; |
| 24 return "Unknown"; |
| 25 case lock_screen_data::OperationResult::kFailed: |
| 26 return "Unknown"; |
| 27 case lock_screen_data::OperationResult::kInvalidKey: |
| 28 case lock_screen_data::OperationResult::kWrongKey: |
| 29 return "Internal - encryption"; |
| 30 case lock_screen_data::OperationResult::kAlreadyRegistered: |
| 31 return "Duplicate item"; |
| 32 case lock_screen_data::OperationResult::kNotFound: |
| 33 case lock_screen_data::OperationResult::kUnknownExtension: |
| 34 return "Not found"; |
| 35 } |
| 36 NOTREACHED() << "Unknown operation status"; |
| 37 return "Unknown"; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
14 LockScreenDataCreateFunction::LockScreenDataCreateFunction() {} | 42 LockScreenDataCreateFunction::LockScreenDataCreateFunction() {} |
15 | 43 |
16 LockScreenDataCreateFunction::~LockScreenDataCreateFunction() {} | 44 LockScreenDataCreateFunction::~LockScreenDataCreateFunction() {} |
17 | 45 |
18 ExtensionFunction::ResponseAction LockScreenDataCreateFunction::Run() { | 46 ExtensionFunction::ResponseAction LockScreenDataCreateFunction::Run() { |
| 47 lock_screen_data::LockScreenItemStorage* storage = |
| 48 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()); |
| 49 if (!storage) { |
| 50 LOG(ERROR) << "Attempt to create data item from context which cannot use " |
| 51 << "lock screen data item storage: " << source_context_type(); |
| 52 return RespondNow(Error("Not available")); |
| 53 } |
| 54 |
| 55 storage->CreateItem(extension_id(), |
| 56 base::Bind(&LockScreenDataCreateFunction::OnDone, this)); |
| 57 return RespondLater(); |
| 58 } |
| 59 |
| 60 void LockScreenDataCreateFunction::OnDone( |
| 61 lock_screen_data::OperationResult result, |
| 62 const lock_screen_data::DataItem* item) { |
| 63 if (result != lock_screen_data::OperationResult::kSuccess) { |
| 64 Respond(Error(GetErrorString(result))); |
| 65 return; |
| 66 } |
| 67 |
19 api::lock_screen_data::DataItemInfo item_info; | 68 api::lock_screen_data::DataItemInfo item_info; |
20 item_info.id = "fake"; | 69 item_info.id = item->id(); |
21 return RespondNow( | 70 Respond( |
22 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); | 71 ArgumentList(api::lock_screen_data::Create::Results::Create(item_info))); |
23 } | 72 } |
24 | 73 |
25 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} | 74 LockScreenDataGetAllFunction::LockScreenDataGetAllFunction() {} |
26 | 75 |
27 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} | 76 LockScreenDataGetAllFunction::~LockScreenDataGetAllFunction() {} |
28 | 77 |
29 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { | 78 ExtensionFunction::ResponseAction LockScreenDataGetAllFunction::Run() { |
| 79 lock_screen_data::LockScreenItemStorage* storage = |
| 80 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()); |
| 81 if (!storage) |
| 82 return RespondNow(Error("Not available")); |
| 83 |
| 84 storage->GetAllForExtension( |
| 85 extension_id(), base::Bind(&LockScreenDataGetAllFunction::OnDone, this)); |
| 86 return RespondLater(); |
| 87 } |
| 88 |
| 89 void LockScreenDataGetAllFunction::OnDone( |
| 90 const std::vector<const lock_screen_data::DataItem*>& items) { |
30 std::vector<api::lock_screen_data::DataItemInfo> items_info; | 91 std::vector<api::lock_screen_data::DataItemInfo> items_info; |
31 return RespondNow( | 92 for (auto* const item : items) { |
| 93 if (!item) |
| 94 continue; |
| 95 api::lock_screen_data::DataItemInfo item_info; |
| 96 item_info.id = item->id(); |
| 97 items_info.emplace_back(std::move(item_info)); |
| 98 } |
| 99 |
| 100 Respond( |
32 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); | 101 ArgumentList(api::lock_screen_data::GetAll::Results::Create(items_info))); |
33 } | 102 } |
34 | 103 |
35 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} | 104 LockScreenDataGetContentFunction::LockScreenDataGetContentFunction() {} |
36 | 105 |
37 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} | 106 LockScreenDataGetContentFunction::~LockScreenDataGetContentFunction() {} |
38 | 107 |
39 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { | 108 ExtensionFunction::ResponseAction LockScreenDataGetContentFunction::Run() { |
| 109 lock_screen_data::LockScreenItemStorage* storage = |
| 110 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()); |
| 111 if (!storage) |
| 112 return RespondNow(Error("Not available")); |
| 113 |
40 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( | 114 std::unique_ptr<api::lock_screen_data::GetContent::Params> params( |
41 api::lock_screen_data::GetContent::Params::Create(*args_)); | 115 api::lock_screen_data::GetContent::Params::Create(*args_)); |
42 EXTENSION_FUNCTION_VALIDATE(params.get()); | 116 EXTENSION_FUNCTION_VALIDATE(params.get()); |
43 | 117 |
44 return RespondNow(Error("Not found")); | 118 storage->GetItemContent( |
| 119 extension_id(), params->id, |
| 120 base::Bind(&LockScreenDataGetContentFunction::OnDone, this)); |
| 121 return RespondLater(); |
| 122 } |
| 123 |
| 124 void LockScreenDataGetContentFunction::OnDone( |
| 125 lock_screen_data::OperationResult result, |
| 126 std::unique_ptr<std::vector<char>> data) { |
| 127 if (result == lock_screen_data::OperationResult::kSuccess) { |
| 128 Respond(ArgumentList( |
| 129 api::lock_screen_data::GetContent::Results::Create(*data))); |
| 130 return; |
| 131 } |
| 132 Respond(Error(GetErrorString(result))); |
45 } | 133 } |
46 | 134 |
47 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} | 135 LockScreenDataSetContentFunction::LockScreenDataSetContentFunction() {} |
48 | 136 |
49 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} | 137 LockScreenDataSetContentFunction::~LockScreenDataSetContentFunction() {} |
50 | 138 |
51 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { | 139 ExtensionFunction::ResponseAction LockScreenDataSetContentFunction::Run() { |
52 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( | 140 std::unique_ptr<api::lock_screen_data::SetContent::Params> params( |
53 api::lock_screen_data::SetContent::Params::Create(*args_)); | 141 api::lock_screen_data::SetContent::Params::Create(*args_)); |
54 EXTENSION_FUNCTION_VALIDATE(params.get()); | 142 EXTENSION_FUNCTION_VALIDATE(params.get()); |
55 | 143 |
56 return RespondNow(Error("Not found")); | 144 lock_screen_data::LockScreenItemStorage* storage = |
| 145 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()); |
| 146 if (!storage) |
| 147 return RespondNow(Error("Not available")); |
| 148 |
| 149 storage->SetItemContent( |
| 150 extension_id(), params->id, params->data, |
| 151 base::Bind(&LockScreenDataSetContentFunction::OnDone, this)); |
| 152 return RespondLater(); |
| 153 } |
| 154 |
| 155 void LockScreenDataSetContentFunction::OnDone( |
| 156 lock_screen_data::OperationResult result) { |
| 157 if (result == lock_screen_data::OperationResult::kSuccess) { |
| 158 Respond(NoArguments()); |
| 159 return; |
| 160 } |
| 161 Respond(Error(GetErrorString(result))); |
57 } | 162 } |
58 | 163 |
59 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} | 164 LockScreenDataDeleteFunction::LockScreenDataDeleteFunction() {} |
60 | 165 |
61 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} | 166 LockScreenDataDeleteFunction::~LockScreenDataDeleteFunction() {} |
62 | 167 |
63 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { | 168 ExtensionFunction::ResponseAction LockScreenDataDeleteFunction::Run() { |
64 std::unique_ptr<api::lock_screen_data::Delete::Params> params( | 169 std::unique_ptr<api::lock_screen_data::Delete::Params> params( |
65 api::lock_screen_data::Delete::Params::Create(*args_)); | 170 api::lock_screen_data::Delete::Params::Create(*args_)); |
66 EXTENSION_FUNCTION_VALIDATE(params.get()); | 171 EXTENSION_FUNCTION_VALIDATE(params.get()); |
67 return RespondNow(Error("Not found.")); | 172 |
| 173 lock_screen_data::LockScreenItemStorage* storage = |
| 174 lock_screen_data::LockScreenItemStorage::GetIfAllowed(browser_context()); |
| 175 if (!storage) |
| 176 return RespondNow(Error("Not available")); |
| 177 |
| 178 storage->DeleteItem(extension_id(), params->id, |
| 179 base::Bind(&LockScreenDataDeleteFunction::OnDone, this)); |
| 180 return RespondLater(); |
| 181 } |
| 182 |
| 183 void LockScreenDataDeleteFunction::OnDone( |
| 184 lock_screen_data::OperationResult result) { |
| 185 if (result == lock_screen_data::OperationResult::kSuccess) { |
| 186 Respond(NoArguments()); |
| 187 return; |
| 188 } |
| 189 Respond(Error(GetErrorString(result))); |
68 } | 190 } |
69 | 191 |
70 } // namespace extensions | 192 } // namespace extensions |
OLD | NEW |